API: Duplicate API Key Name Handle With Useful HTTP Code

This commit is contained in:
Anthony Templeton 2019-07-03 12:03:47 -04:00
parent 164fb13d99
commit da6df0ee07
2 changed files with 5 additions and 1 deletions

View File

@ -68,7 +68,10 @@ func (hs *HTTPServer) AddAPIKey(c *models.ReqContext, cmd models.AddApiKeyComman
if err == models.ErrInvalidApiKeyExpiration {
return Error(400, err.Error(), nil)
}
return Error(500, "Failed to add API key", err)
if err.Error() == models.ErrDuplicateApiKey.Error() {
return Error(409, "API Key Name Must Be Unique", nil)
}
return Error(500, "Failed to add API", err)
}
result := &dtos.NewApiKeyResult{

View File

@ -7,6 +7,7 @@ import (
var ErrInvalidApiKey = errors.New("Invalid API Key")
var ErrInvalidApiKeyExpiration = errors.New("Negative value for SecondsToLive")
var ErrDuplicateApiKey = errors.New("UNIQUE constraint failed: api_key.org_id, api_key.name")
type ApiKey struct {
Id int64