From 55e63debaa49fe7302e04851c3f64dc6d0efaf8d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 13:32:16 +0200 Subject: [PATCH 1/4] handle GPG Parse & Email Errors --- models/gpg_key.go | 2 +- routers/api/v1/user/gpg_key.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/models/gpg_key.go b/models/gpg_key.go index a32312a12dd6b..bebd33191aa13 100644 --- a/models/gpg_key.go +++ b/models/gpg_key.go @@ -273,7 +273,7 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) { for i, k := range e.Subkeys { subs, err := parseSubGPGKey(ownerID, pubkey.KeyIdString(), k.PublicKey, expiry) if err != nil { - return nil, err + return nil, ErrGPGKeyParsing{ParseError: err} } subkeys[i] = subs } diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index a4f6d0e92445f..f7c5018a2cae9 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -169,6 +169,8 @@ func DeleteGPGKey(ctx *context.APIContext) { // "$ref": "#/responses/empty" // "403": // "$ref": "#/responses/forbidden" + // "404": + // "$ref": "#/responses/notFound" if err := models.DeleteGPGKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { if models.IsErrGPGKeyAccessDenied(err) { @@ -186,9 +188,13 @@ func DeleteGPGKey(ctx *context.APIContext) { func HandleAddGPGKeyError(ctx *context.APIContext, err error) { switch { case models.IsErrGPGKeyAccessDenied(err): - ctx.Error(http.StatusUnprocessableEntity, "", "You do not have access to this GPG key") + ctx.Error(http.StatusUnprocessableEntity, "GPGKeyAccessDenied", "You do not have access to this GPG key") case models.IsErrGPGKeyIDAlreadyUsed(err): - ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same id already exists") + ctx.Error(http.StatusUnprocessableEntity, "GPGKeyIDAlreadyUsed", "A key with the same id already exists") + case models.IsErrGPGKeyParsing(err): + ctx.Error(http.StatusUnprocessableEntity, "GPGKeyParsing", err) + case models.IsErrGPGNoEmailFound(err): + ctx.Error(http.StatusNotFound, "GPGNoEmailFound", err) default: ctx.Error(http.StatusInternalServerError, "AddGPGKey", err) } From 5964198fcb4301bdc84ceb8fb8be1c5568fcea45 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 13:32:27 +0200 Subject: [PATCH 2/4] correct TEST --- integrations/api_gpg_keys_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integrations/api_gpg_keys_test.go b/integrations/api_gpg_keys_test.go index edae22205e70a..e664c3c256553 100644 --- a/integrations/api_gpg_keys_test.go +++ b/integrations/api_gpg_keys_test.go @@ -32,7 +32,7 @@ func TestGPGKeys(t *testing.T) { results: []int{http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized, http.StatusUnauthorized}, }, {name: "LoggedAsUser2", makeRequest: session.MakeRequest, token: token, - results: []int{http.StatusOK, http.StatusOK, http.StatusNotFound, http.StatusNoContent, http.StatusInternalServerError, http.StatusInternalServerError, http.StatusCreated, http.StatusCreated}}, + results: []int{http.StatusOK, http.StatusOK, http.StatusNotFound, http.StatusNoContent, http.StatusUnprocessableEntity, http.StatusNotFound, http.StatusCreated, http.StatusCreated}}, } for _, tc := range tt { From 728a376f742ab07aa17f3489faea0233199916eb Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 13:36:24 +0200 Subject: [PATCH 3/4] update Swagger --- templates/swagger/v1_json.tmpl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index b7fa714a64650..1401b18b1e05f 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -9165,6 +9165,9 @@ }, "403": { "$ref": "#/responses/forbidden" + }, + "404": { + "$ref": "#/responses/notFound" } } } From 15946c58f2a6b4144a21c42a01365863b1030792 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 May 2020 13:41:25 +0200 Subject: [PATCH 4/4] more Docu --- routers/api/v1/user/gpg_key.go | 2 ++ templates/swagger/v1_json.tmpl | 3 +++ 2 files changed, 5 insertions(+) diff --git a/routers/api/v1/user/gpg_key.go b/routers/api/v1/user/gpg_key.go index f7c5018a2cae9..d5fed96df1710 100644 --- a/routers/api/v1/user/gpg_key.go +++ b/routers/api/v1/user/gpg_key.go @@ -144,6 +144,8 @@ func CreateGPGKey(ctx *context.APIContext, form api.CreateGPGKeyOption) { // responses: // "201": // "$ref": "#/responses/GPGKey" + // "404": + // "$ref": "#/responses/notFound" // "422": // "$ref": "#/responses/validationError" diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 1401b18b1e05f..0cbe33bd247ba 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -9105,6 +9105,9 @@ "201": { "$ref": "#/responses/GPGKey" }, + "404": { + "$ref": "#/responses/notFound" + }, "422": { "$ref": "#/responses/validationError" }