Refactor error system (#33610)
This commit is contained in:
parent
69de5a65c2
commit
f35850f48e
184 changed files with 2100 additions and 2106 deletions
|
@ -25,12 +25,12 @@ func listGPGKeys(ctx *context.APIContext, uid int64, listOptions db.ListOptions)
|
|||
OwnerID: uid,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := asymkey_model.GPGKeyList(keys).LoadSubKeys(ctx); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "ListGPGKeys", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -119,14 +119,14 @@ func GetGPGKey(ctx *context.APIContext) {
|
|||
key, err := asymkey_model.GetGPGKeyForUserByID(ctx, ctx.Doer.ID, ctx.PathParamInt64("id"))
|
||||
if err != nil {
|
||||
if asymkey_model.IsErrGPGKeyNotExist(err) {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetGPGKeyByID", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := key.LoadSubKeys(ctx); err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "LoadSubKeys", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, convert.ToGPGKey(key))
|
||||
|
@ -135,7 +135,7 @@ func GetGPGKey(ctx *context.APIContext) {
|
|||
// CreateUserGPGKey creates new GPG key to given user by ID.
|
||||
func CreateUserGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption, uid int64) {
|
||||
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
|
||||
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
|
||||
ctx.APIErrorNotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -194,7 +194,7 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
|
|||
|
||||
form.KeyID = strings.TrimLeft(form.KeyID, "0")
|
||||
if form.KeyID == "" {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -205,10 +205,10 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
|
|||
|
||||
if err != nil {
|
||||
if asymkey_model.IsErrGPGInvalidTokenSignature(err) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "GPGInvalidSignature", fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
|
||||
return
|
||||
}
|
||||
ctx.Error(http.StatusInternalServerError, "VerifyUserGPGKey", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
keys, err := db.Find[asymkey_model.GPGKey](ctx, asymkey_model.FindGPGKeyOptions{
|
||||
|
@ -217,9 +217,9 @@ func VerifyUserGPGKey(ctx *context.APIContext) {
|
|||
})
|
||||
if err != nil {
|
||||
if asymkey_model.IsErrGPGKeyNotExist(err) {
|
||||
ctx.NotFound()
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetGPGKeysByKeyID", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -276,15 +276,15 @@ func DeleteGPGKey(ctx *context.APIContext) {
|
|||
// "$ref": "#/responses/notFound"
|
||||
|
||||
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureManageGPGKeys) {
|
||||
ctx.NotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
|
||||
ctx.APIErrorNotFound("Not Found", fmt.Errorf("gpg keys setting is not allowed to be visited"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := asymkey_model.DeleteGPGKey(ctx, ctx.Doer, ctx.PathParamInt64("id")); err != nil {
|
||||
if asymkey_model.IsErrGPGKeyAccessDenied(err) {
|
||||
ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
|
||||
ctx.APIError(http.StatusForbidden, "You do not have access to this key")
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteGPGKey", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -296,16 +296,16 @@ func DeleteGPGKey(ctx *context.APIContext) {
|
|||
func HandleAddGPGKeyError(ctx *context.APIContext, err error, token string) {
|
||||
switch {
|
||||
case asymkey_model.IsErrGPGKeyAccessDenied(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyAccessDenied", "You do not have access to this GPG key")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "You do not have access to this GPG key")
|
||||
case asymkey_model.IsErrGPGKeyIDAlreadyUsed(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyIDAlreadyUsed", "A key with the same id already exists")
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "A key with the same id already exists")
|
||||
case asymkey_model.IsErrGPGKeyParsing(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyParsing", err)
|
||||
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||
case asymkey_model.IsErrGPGNoEmailFound(err):
|
||||
ctx.Error(http.StatusNotFound, "GPGNoEmailFound", fmt.Sprintf("None of the emails attached to the GPG key could be found. It may still be added if you provide a valid signature for the token: %s", token))
|
||||
ctx.APIError(http.StatusNotFound, fmt.Sprintf("None of the emails attached to the GPG key could be found. It may still be added if you provide a valid signature for the token: %s", token))
|
||||
case asymkey_model.IsErrGPGInvalidTokenSignature(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "GPGInvalidSignature", fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The provided GPG key, signature and token do not match or token is out of date. Provide a valid signature for the token: %s", token))
|
||||
default:
|
||||
ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
|
||||
ctx.APIError(http.StatusInternalServerError, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue