]> source.dussan.org Git - gitea.git/commitdiff
Fix DELETE request for non-existent public key (#19443)
authorGusted <williamzijl7@hotmail.com>
Thu, 21 Apr 2022 01:08:30 +0000 (01:08 +0000)
committerGitHub <noreply@github.com>
Thu, 21 Apr 2022 01:08:30 +0000 (03:08 +0200)
- Add a return for the first "block" of errors, which fixes the double
error messages.
- Add a return for `externallyManaged`.
- Resolves #19398

routers/api/v1/user/key.go

index cc7ee739ce3a8718929907b0609cccccaee1080f..71a2c910a6a313c436d4f3a082d4afdab7265604 100644 (file)
@@ -262,16 +262,21 @@ func DeletePublicKey(ctx *context.APIContext) {
        id := ctx.ParamsInt64(":id")
        externallyManaged, err := asymkey_model.PublicKeyIsExternallyManaged(id)
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
+               if asymkey_model.IsErrKeyNotExist(err) {
+                       ctx.NotFound()
+               } else {
+                       ctx.Error(http.StatusInternalServerError, "PublicKeyIsExternallyManaged", err)
+               }
+               return
        }
+
        if externallyManaged {
                ctx.Error(http.StatusForbidden, "", "SSH Key is externally managed for this user")
+               return
        }
 
        if err := asymkey_service.DeletePublicKey(ctx.Doer, id); err != nil {
-               if asymkey_model.IsErrKeyNotExist(err) {
-                       ctx.NotFound()
-               } else if asymkey_model.IsErrKeyAccessDenied(err) {
+               if asymkey_model.IsErrKeyAccessDenied(err) {
                        ctx.Error(http.StatusForbidden, "", "You do not have access to this key")
                } else {
                        ctx.Error(http.StatusInternalServerError, "DeletePublicKey", err)