本文整理汇总了Golang中github.com/almighty/almighty-core/jsonapi.JSONErrorResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang JSONErrorResponse函数的具体用法?Golang JSONErrorResponse怎么用?Golang JSONErrorResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了JSONErrorResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Update
// Update does PATCH comment
func (c *CommentsController) Update(ctx *app.UpdateCommentsContext) error {
identity, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
cm, err := appl.Comments().Load(ctx.Context, ctx.CommentID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
if identity != cm.CreatedBy.String() {
// need to use the goa.NewErrorClass() func as there is no native support for 403 in goa
// and it is not planned to be supported yet: https://github.com/goadesign/goa/pull/1030
return jsonapi.JSONErrorResponse(ctx, goa.NewErrorClass("forbidden", 403)("User is not the comment author"))
}
cm.Body = *ctx.Payload.Data.Attributes.Body
cm.Markup = rendering.NilSafeGetMarkup(ctx.Payload.Data.Attributes.Markup)
cm, err = appl.Comments().Save(ctx.Context, cm)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.CommentSingle{
Data: ConvertComment(ctx.RequestData, cm, CommentIncludeParentWorkItem()),
}
return ctx.OK(res)
})
}
示例2: Refresh
// Refresh obtain a new access token using the refresh token.
func (c *LoginController) Refresh(ctx *app.RefreshLoginContext) error {
refreshToken := ctx.Payload.RefreshToken
if refreshToken == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("refresh_token", nil).Expected("not nil"))
}
client := &http.Client{Timeout: 10 * time.Second}
res, err := client.PostForm(configuration.GetKeycloakEndpointToken(), url.Values{
"client_id": {configuration.GetKeycloakClientID()},
"client_secret": {configuration.GetKeycloakSecret()},
"refresh_token": {*refreshToken},
"grant_type": {"refresh_token"},
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewInternalError("Error when obtaining token "+err.Error()))
}
switch res.StatusCode {
case 200:
// OK
case 401:
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError(res.Status+" "+readBody(res.Body)))
case 400:
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError(readBody(res.Body), nil))
default:
return jsonapi.JSONErrorResponse(ctx, errors.NewInternalError(res.Status+" "+readBody(res.Body)))
}
token, err := readToken(res, ctx)
if err != nil {
return err
}
return ctx.OK(&app.AuthToken{Token: token})
}
示例3: Update
// Update does PATCH workitem
func (c *WorkitemController) Update(ctx *app.UpdateWorkitemContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
if ctx.Payload == nil || ctx.Payload.Data == nil || ctx.Payload.Data.ID == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("missing data.ID element in request", nil))
}
wi, err := appl.WorkItems().Load(ctx, *ctx.Payload.Data.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, fmt.Sprintf("Failed to load work item with id %v", *ctx.Payload.Data.ID)))
}
// Type changes of WI are not allowed which is why we overwrite it the
// type with the old one after the WI has been converted.
oldType := wi.Type
err = ConvertJSONAPIToWorkItem(appl, *ctx.Payload.Data, wi)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
wi.Type = oldType
wi, err = appl.WorkItems().Save(ctx, *wi)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, "Error updating work item"))
}
wi2 := ConvertWorkItem(ctx.RequestData, wi)
resp := &app.WorkItem2Single{
Data: wi2,
Links: &app.WorkItemLinks{
Self: buildAbsoluteURL(ctx.RequestData),
},
}
return ctx.OK(resp)
})
}
示例4: Create
// Create runs the create action.
func (c *SpaceController) Create(ctx *app.CreateSpaceContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
err = validateCreateSpace(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
return application.Transactional(c.db, func(appl application.Application) error {
reqSpace := ctx.Payload.Data
newSpace := space.Space{
Name: *reqSpace.Attributes.Name,
}
if reqSpace.Attributes.Description != nil {
newSpace.Description = *reqSpace.Attributes.Description
}
space, err := appl.Spaces().Create(ctx, &newSpace)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.SpaceSingle{
Data: ConvertSpace(ctx.RequestData, space),
}
ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.RequestData, app.SpaceHref(res.Data.ID)))
return ctx.Created(res)
})
}
示例5: List
// List runs the list action.
func (c *SpaceIterationsController) List(ctx *app.ListSpaceIterationsContext) error {
spaceID, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
_, err = appl.Spaces().Load(ctx, spaceID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
iterations, err := appl.Iterations().List(ctx, spaceID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
// fetch extra information(counts of WI in each iteration of the space) to be added in response
wiCounts, err := appl.WorkItems().GetCountsPerIteration(ctx, spaceID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.IterationList{}
res.Data = ConvertIterations(ctx.RequestData, iterations, UpdateIterationsWithCounts(wiCounts))
return ctx.OK(res)
})
}
示例6: Delete
// Delete does DELETE workitem
func (c *WorkitemController) Delete(ctx *app.DeleteWorkitemContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
err := appl.WorkItems().Delete(ctx, ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "error deleting work item %s", ctx.ID))
}
if err := appl.WorkItemLinks().DeleteRelatedLinks(ctx, ctx.ID); err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "failed to delete work item links related to work item %s", ctx.ID))
}
return ctx.OK([]byte{})
})
}
示例7: Update
// Update runs the update action.
func (c *IterationController) Update(ctx *app.UpdateIterationContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
id, err := uuid.FromString(ctx.IterationID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
itr, err := appl.Iterations().Load(ctx.Context, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
if ctx.Payload.Data.Attributes.Name != nil {
itr.Name = *ctx.Payload.Data.Attributes.Name
}
if ctx.Payload.Data.Attributes.StartAt != nil {
itr.StartAt = ctx.Payload.Data.Attributes.StartAt
}
if ctx.Payload.Data.Attributes.EndAt != nil {
itr.EndAt = ctx.Payload.Data.Attributes.EndAt
}
if ctx.Payload.Data.Attributes.Description != nil {
itr.Description = ctx.Payload.Data.Attributes.Description
}
if ctx.Payload.Data.Attributes.State != nil {
if *ctx.Payload.Data.Attributes.State == iteration.IterationStateStart {
res, err := appl.Iterations().CanStartIteration(ctx, itr)
if res == false && err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
}
itr.State = *ctx.Payload.Data.Attributes.State
}
itr, err = appl.Iterations().Save(ctx.Context, *itr)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
response := app.IterationSingle{
Data: ConvertIteration(ctx.RequestData, itr),
}
return ctx.OK(&response)
})
}
示例8: Show
// Show returns the authorized user based on the provided Token
func (c *UserController) Show(ctx *app.ShowUserContext) error {
id, err := c.tokenManager.Locate(ctx)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrBadRequest(err.Error()))
return ctx.BadRequest(jerrors)
}
return application.Transactional(c.db, func(appl application.Application) error {
identity, err := appl.Identities().Load(ctx, id)
if err != nil || identity == nil {
log.Error(ctx, map[string]interface{}{
"identityID": id,
}, "auth token containers id %s of unknown Identity", id)
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrUnauthorized(fmt.Sprintf("Auth token contains id %s of unknown Identity\n", id)))
return ctx.Unauthorized(jerrors)
}
var user *account.User
userID := identity.UserID
if userID.Valid {
user, err = appl.Users().Load(ctx.Context, userID.UUID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.Wrap(err, fmt.Sprintf("Can't load user with id %s", userID.UUID)))
}
}
return ctx.OK(ConvertUser(ctx.RequestData, identity, user))
})
}
示例9: Show
// Show runs the show action.
func (c *AreaController) Show(ctx *app.ShowAreaContext) error {
id, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
a, err := appl.Areas().Load(ctx, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.AreaSingle{}
res.Data = ConvertArea(appl, ctx.RequestData, a, addResolvedPath)
return ctx.OK(res)
})
}
示例10: Show
// Show runs the show action.
func (c *WorkitemtypeController) Show(ctx *app.ShowWorkitemtypeContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
res, err := appl.WorkItemTypes().Load(ctx.Context, ctx.Name)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
return ctx.OK(res)
})
}
示例11: Delete
// Delete runs the delete action.
func (c *SpaceController) Delete(ctx *app.DeleteSpaceContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
id, err := satoriuuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
err = appl.Spaces().Delete(ctx.Context, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
return ctx.OK([]byte{})
})
}
示例12: Spaces
// Spaces runs the space search action.
func (c *SearchController) Spaces(ctx *app.SpacesSearchContext) error {
q := ctx.Q
if q == "" {
return jsonapi.JSONErrorResponse(ctx, goa.ErrBadRequest(fmt.Errorf("Empty search query not allowed")))
} else if q == "*" {
q = "" // Allow empty query if * specified
}
var result []*space.Space
var count int
var err error
offset, limit := computePagingLimts(ctx.PageOffset, ctx.PageLimit)
return application.Transactional(c.db, func(appl application.Application) error {
var resultCount uint64
result, resultCount, err = appl.Spaces().Search(ctx, &q, &offset, &limit)
count = int(resultCount)
if err != nil {
cause := errs.Cause(err)
switch cause.(type) {
case errors.BadParameterError:
return jsonapi.JSONErrorResponse(ctx, goa.ErrBadRequest(fmt.Sprintf("Error listing spaces: %s", err.Error())))
default:
log.Error(ctx, map[string]interface{}{
"query": q,
"offset": offset,
"limit": limit,
"err": err,
}, "unable to list spaces")
return jsonapi.JSONErrorResponse(ctx, goa.ErrInternal(err.Error()))
}
}
response := app.SearchSpaceList{
Links: &app.PagingLinks{},
Meta: &app.SpaceListMeta{TotalCount: count},
Data: ConvertSpaces(ctx.RequestData, result),
}
setPagingLinks(response.Links, buildAbsoluteURL(ctx.RequestData), len(result), offset, limit, count, "q="+q)
return ctx.OK(&response)
})
}
示例13: CreateChild
// CreateChild runs the create-child action.
func (c *AreaController) CreateChild(ctx *app.CreateChildAreaContext) error {
_, err := login.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
}
parentID, err := uuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
parent, err := appl.Areas().Load(ctx, parentID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
reqArea := ctx.Payload.Data
if reqArea.Attributes.Name == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("data.attributes.name", nil).Expected("not nil"))
}
childPath := area.ConvertToLtreeFormat(parentID.String())
if parent.Path != "" {
childPath = parent.Path + pathSepInDatabase + childPath
}
newArea := area.Area{
SpaceID: parent.SpaceID,
Path: childPath,
Name: *reqArea.Attributes.Name,
}
err = appl.Areas().Create(ctx, &newArea)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
res := &app.AreaSingle{
Data: ConvertArea(appl, ctx.RequestData, &newArea, addResolvedPath),
}
ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.RequestData, app.AreaHref(res.Data.ID)))
return ctx.Created(res)
})
}
示例14: Generate
// Generate obtain the access token from Keycloak for the test user
func (c *LoginController) Generate(ctx *app.GenerateLoginContext) error {
if !configuration.IsPostgresDeveloperModeEnabled() {
log.Error(ctx, map[string]interface{}{
"method": "Generate",
}, "Postgres developer mode not enabled")
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrUnauthorized("Postgres developer mode not enabled"))
return ctx.Unauthorized(jerrors)
}
var scopes []account.Identity
scopes = append(scopes, test.TestIdentity)
scopes = append(scopes, test.TestObserverIdentity)
client := &http.Client{Timeout: 10 * time.Second}
username := configuration.GetKeycloakTestUserName()
res, err := client.PostForm(configuration.GetKeycloakEndpointToken(), url.Values{
"client_id": {configuration.GetKeycloakClientID()},
"client_secret": {configuration.GetKeycloakSecret()},
"username": {username},
"password": {configuration.GetKeycloakTestUserSecret()},
"grant_type": {"password"},
})
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewInternalError("error when obtaining token "+err.Error()))
}
token, err := readToken(res, ctx)
if err != nil {
log.Error(ctx, map[string]interface{}{
"tokenEndpoint": res,
"err": err,
}, "Error when unmarshal json with access token")
return jsonapi.JSONErrorResponse(ctx, e.Wrap(err, "Error when unmarshal json with access token"))
}
var tokens app.AuthTokenCollection
tokens = append(tokens, &app.AuthToken{Token: token})
// Creates the testuser user and identity if they don't yet exist
c.auth.CreateKeycloakUser(*token.AccessToken, ctx)
return ctx.OK(tokens)
}
示例15: Show
// Show runs the show action.
func (c *SpaceController) Show(ctx *app.ShowSpaceContext) error {
id, err := satoriuuid.FromString(ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, goa.ErrNotFound(err.Error()))
}
return application.Transactional(c.db, func(appl application.Application) error {
s, err := appl.Spaces().Load(ctx.Context, id)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
resp := app.SpaceSingle{
Data: ConvertSpace(ctx.RequestData, s),
}
return ctx.OK(&resp)
})
}