Refactor error system (#33610)
This commit is contained in:
parent
69de5a65c2
commit
f35850f48e
184 changed files with 2100 additions and 2106 deletions
|
@ -30,7 +30,7 @@ func ListOwnerHooks(ctx *context.APIContext, owner *user_model.User) {
|
|||
|
||||
hooks, count, err := db.FindAndCount[webhook.Webhook](ctx, opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ func ListOwnerHooks(ctx *context.APIContext, owner *user_model.User) {
|
|||
for i, hook := range hooks {
|
||||
apiHooks[i], err = webhook_service.ToHook(owner.HomeLink(), hook)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -52,9 +52,9 @@ func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webh
|
|||
w, err := webhook.GetWebhookByOwnerID(ctx, ownerID, hookID)
|
||||
if err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetWebhookByOwnerID", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
@ -67,9 +67,9 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhoo
|
|||
w, err := webhook.GetWebhookByRepoID(ctx, repoID, hookID)
|
||||
if err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
@ -80,17 +80,17 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhoo
|
|||
// write the appropriate error to `ctx`. Return whether the form is valid
|
||||
func checkCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
|
||||
if !webhook_service.IsValidHookTaskType(form.Type) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("Invalid hook type: %s", form.Type))
|
||||
return false
|
||||
}
|
||||
for _, name := range []string{"url", "content_type"} {
|
||||
if _, ok := form.Config[name]; !ok {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: "+name)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Missing config option: "+name)
|
||||
return false
|
||||
}
|
||||
}
|
||||
if !webhook.IsValidHookContentType(form.Config["content_type"]) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Invalid content type")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -102,7 +102,7 @@ func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
|||
if ok {
|
||||
h, err := webhook_service.ToHook(setting.AppSubURL+"/-/admin", hook)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusCreated, h)
|
||||
|
@ -141,7 +141,7 @@ func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
|||
func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) {
|
||||
apiHook, err := webhook_service.ToHook(repoLink, hook)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ToHook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return nil, false
|
||||
}
|
||||
return apiHook, true
|
||||
|
@ -169,7 +169,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
|
|||
if form.Config["is_system_webhook"] != "" {
|
||||
sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid is_system_webhook value")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Invalid is_system_webhook value")
|
||||
return nil, false
|
||||
}
|
||||
isSystemWebhook = sw
|
||||
|
@ -215,19 +215,19 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
|
|||
}
|
||||
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return nil, false
|
||||
}
|
||||
if w.Type == webhook_module.SLACK {
|
||||
channel, ok := form.Config["channel"]
|
||||
if !ok {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Missing config option: channel")
|
||||
return nil, false
|
||||
}
|
||||
channel = strings.TrimSpace(channel)
|
||||
|
||||
if !webhook_service.IsValidSlackChannel(channel) {
|
||||
ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
|
||||
ctx.APIError(http.StatusBadRequest, "Invalid slack channel name")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
|
@ -238,17 +238,17 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
|
|||
Color: form.Config["color"],
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return nil, false
|
||||
}
|
||||
w.Meta = string(meta)
|
||||
}
|
||||
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return nil, false
|
||||
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return nil, false
|
||||
}
|
||||
return w, true
|
||||
|
@ -258,21 +258,21 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
|
|||
func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
||||
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if !editHook(ctx, form, hook) {
|
||||
ctx.Error(http.StatusInternalServerError, "editHook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
h, err := webhook_service.ToHook(setting.AppURL+"/-/admin", updated)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, h)
|
||||
|
@ -328,7 +328,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
}
|
||||
if ct, ok := form.Config["content_type"]; ok {
|
||||
if !webhook.IsValidHookContentType(ct) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "Invalid content type")
|
||||
return false
|
||||
}
|
||||
w.ContentType = webhook.ToHookContentType(ct)
|
||||
|
@ -343,7 +343,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
Color: form.Config["color"],
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return false
|
||||
}
|
||||
w.Meta = string(meta)
|
||||
|
@ -369,7 +369,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
|
||||
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -391,7 +391,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
w.HookEvents[webhook_module.HookEventPullRequestSync] = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
|
||||
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -400,7 +400,7 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
}
|
||||
|
||||
if err := webhook.UpdateWebhook(ctx, w); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -410,9 +410,9 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
|||
func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int64) {
|
||||
if err := webhook.DeleteWebhookByOwnerID(ctx, owner.ID, hookID); err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOwnerID", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue