Skip to content

Commit

Permalink
Improve error feedback for duplicate deploy keys (#13112)
Browse files Browse the repository at this point in the history
Instead of a generic HTTP 500 error page, a flash message is rendered with the deploy key page template to inform the user that a key with the intended title already exists. 

Fixes #13110
  • Loading branch information
chrisshyi authored Oct 12, 2020
1 parent 0e4f663 commit c752cce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func IsErrDeployKeyNameAlreadyUsed(err error) bool {
}

func (err ErrDeployKeyNameAlreadyUsed) Error() string {
return fmt.Sprintf("public key already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
return fmt.Sprintf("public key with name already exists [repo_id: %d, name: %s]", err.RepoID, err.Name)
}

// _____ ___________ __
Expand Down
2 changes: 2 additions & 0 deletions routers/api/v1/repo/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ func HandleAddKeyError(ctx *context.APIContext, err error) {
ctx.Error(http.StatusUnprocessableEntity, "", "Key content has been used as non-deploy key")
case models.IsErrKeyNameAlreadyUsed(err):
ctx.Error(http.StatusUnprocessableEntity, "", "Key title has been used")
case models.IsErrDeployKeyNameAlreadyUsed(err):
ctx.Error(http.StatusUnprocessableEntity, "", "A key with the same name already exists")
default:
ctx.Error(http.StatusInternalServerError, "AddKey", err)
}
Expand Down
3 changes: 3 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,9 @@ func DeployKeysPost(ctx *context.Context, form auth.AddKeyForm) {
case models.IsErrKeyNameAlreadyUsed(err):
ctx.Data["Err_Title"] = true
ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form)
case models.IsErrDeployKeyNameAlreadyUsed(err):
ctx.Data["Err_Title"] = true
ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form)
default:
ctx.ServerError("AddDeployKey", err)
}
Expand Down

0 comments on commit c752cce

Please sign in to comment.