mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Handle expected errors in AddGPGkey API (#11644)
* handle GPG Parse & Email Errors * correct TEST * update Swagger * more Docu
This commit is contained in:
		@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -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"
 | 
			
		||||
 | 
			
		||||
@@ -169,6 +171,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 +190,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)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -9105,6 +9105,9 @@
 | 
			
		||||
          "201": {
 | 
			
		||||
            "$ref": "#/responses/GPGKey"
 | 
			
		||||
          },
 | 
			
		||||
          "404": {
 | 
			
		||||
            "$ref": "#/responses/notFound"
 | 
			
		||||
          },
 | 
			
		||||
          "422": {
 | 
			
		||||
            "$ref": "#/responses/validationError"
 | 
			
		||||
          }
 | 
			
		||||
@@ -9165,6 +9168,9 @@
 | 
			
		||||
          },
 | 
			
		||||
          "403": {
 | 
			
		||||
            "$ref": "#/responses/forbidden"
 | 
			
		||||
          },
 | 
			
		||||
          "404": {
 | 
			
		||||
            "$ref": "#/responses/notFound"
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user