Skip to content

Commit 762c046

Browse files
authored
Handle expected errors in AddGPGkey API (#11644) (#11661)
* handle GPG Parse & Email Errors * correct TEST * update Swagger * more Docu
1 parent bf1dbd7 commit 762c046

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

integrations/api_gpg_keys_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestGPGKeys(t *testing.T) {
3232
results: []int{http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized},
3333
},
3434
{name: "LoggedAsUser2", makeRequest: session.MakeRequest, token: token,
35-
results: []int{http.StatusOK, http.StatusOK, http.StatusNotFound, http.StatusNoContent, http.StatusInternalServerError, http.StatusInternalServerError, http.StatusCreated, http.StatusCreated}},
35+
results: []int{http.StatusOK, http.StatusOK, http.StatusNotFound, http.StatusNoContent, http.StatusUnprocessableEntity, http.StatusNotFound, http.StatusCreated, http.StatusCreated}},
3636
}
3737

3838
for _, tc := range tt {

models/gpg_key.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
273273
for i, k := range e.Subkeys {
274274
subs, err := parseSubGPGKey(ownerID, pubkey.KeyIdString(), k.PublicKey, expiry)
275275
if err != nil {
276-
return nil, err
276+
return nil, ErrGPGKeyParsing{ParseError: err}
277277
}
278278
subkeys[i] = subs
279279
}

routers/api/v1/user/gpg_key.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) {
144144
// responses:
145145
// "201":
146146
// "$ref": "#/responses/GPGKey"
147+
// "404":
148+
// "$ref": "#/responses/notFound"
147149
// "422":
148150
// "$ref": "#/responses/validationError"
149151

@@ -169,6 +171,8 @@ func DeleteGPGKey(ctx *context.APIContext) {
169171
// "$ref": "#/responses/empty"
170172
// "403":
171173
// "$ref": "#/responses/forbidden"
174+
// "404":
175+
// "$ref": "#/responses/notFound"
172176

173177
if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil {
174178
if models.IsErrGPGKeyAccessDenied(err) {
@@ -186,9 +190,13 @@ func DeleteGPGKey(ctx *context.APIContext) {
186190
func HandleAddGPGKeyError(ctx *context.APIContext, err error) {
187191
switch {
188192
case models.IsErrGPGKeyAccessDenied(err):
189-
ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key")
193+
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyAccessDenied", "You do not have access to this GPG key")
190194
case models.IsErrGPGKeyIDAlreadyUsed(err):
191-
ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists")
195+
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyIDAlreadyUsed", "A key with the same id already exists")
196+
case models.IsErrGPGKeyParsing(err):
197+
ctx.Error(http.StatusUnprocessableEntity, "GPGKeyParsing", err)
198+
case models.IsErrGPGNoEmailFound(err):
199+
ctx.Error(http.StatusNotFound, "GPGNoEmailFound", err)
192200
default:
193201
ctx.Error(http.StatusInternalServerError, "AddGPGKey", err)
194202
}

templates/swagger/v1_json.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9105,6 +9105,9 @@
91059105
"201": {
91069106
"$ref": "#/responses/GPGKey"
91079107
},
9108+
"404": {
9109+
"$ref": "#/responses/notFound"
9110+
},
91089111
"422": {
91099112
"$ref": "#/responses/validationError"
91109113
}
@@ -9165,6 +9168,9 @@
91659168
},
91669169
"403": {
91679170
"$ref": "#/responses/forbidden"
9171+
},
9172+
"404": {
9173+
"$ref": "#/responses/notFound"
91689174
}
91699175
}
91709176
}

0 commit comments

Comments
 (0)