Chore: Differentiate the ErrOrgNotFound error messages (#64131)

* Better org not found error messages
This commit is contained in:
Sofia Papagiannaki 2023-03-06 09:57:46 +02:00 committed by GitHub
parent 1aadafe7d8
commit fde96c91c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 14 deletions

View File

@ -70,14 +70,14 @@ func (hs *HTTPServer) AdminCreateUser(c *contextmodel.ReqContext) response.Respo
usr, err := hs.userService.Create(c.Req.Context(), &cmd)
if err != nil {
if errors.Is(err, org.ErrOrgNotFound) {
return response.Error(400, err.Error(), nil)
return response.Error(http.StatusBadRequest, err.Error(), nil)
}
if errors.Is(err, user.ErrUserAlreadyExists) {
return response.Error(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", form.Email, form.Login), err)
return response.Error(http.StatusPreconditionFailed, fmt.Sprintf("User with email '%s' or username '%s' already exists", form.Email, form.Login), err)
}
return response.Error(500, "failed to create user", err)
return response.Error(http.StatusInternalServerError, "failed to create user", err)
}
metrics.MApiAdminUserCreate.Inc()

View File

@ -209,7 +209,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
require.NoError(t, err)
assert.Equal(t, "organization not found", respJSON.Get("message").MustString())
assert.Equal(t, org.ErrOrgNotFound.Error(), respJSON.Get("message").MustString())
})
})
})

View File

@ -215,8 +215,12 @@ func UseOrgFromContextParams(c *contextmodel.ReqContext) (int64, error) {
orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
// Special case of macaron handling invalid params
if orgID == 0 || err != nil {
return 0, org.ErrOrgNotFound
if err != nil {
return 0, org.ErrOrgNotFound.Errorf("failed to get organization from context: %w", err)
}
if orgID == 0 {
return 0, org.ErrOrgNotFound.Errorf("empty org ID")
}
return orgID, nil

View File

@ -7,15 +7,16 @@ import (
"github.com/grafana/grafana/pkg/models/roletype"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/util/errutil"
)
// Typed errors
var (
ErrOrgNotFound = errors.New("organization not found")
ErrOrgNameTaken = errors.New("organization name is taken")
ErrLastOrgAdmin = errors.New("cannot remove last organization admin")
ErrOrgUserNotFound = errors.New("cannot find the organization user")
ErrOrgUserAlreadyAdded = errors.New("user is already added to organization")
ErrOrgNotFound = errutil.NewBase(errutil.StatusNotFound, "org.notFound", errutil.WithPublicMessage("organization not found"))
)
type Org struct {

View File

@ -64,7 +64,7 @@ func (ss *sqlStore) Get(ctx context.Context, orgID int64) (*org.Org, error) {
return err
}
if !has {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to get organization with ID: %d", orgID)
}
return nil
})
@ -147,7 +147,7 @@ func (ss *sqlStore) Update(ctx context.Context, cmd *org.UpdateOrgCommand) error
}
if affectedRows == 0 {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to update organization with ID: %d", cmd.OrgId)
}
sess.PublishAfterCommit(&events.OrgUpdated{
@ -210,7 +210,7 @@ func (ss *sqlStore) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) error
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.ID); err != nil {
return err
} else if len(res) != 1 {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to delete organisation with ID: %d", cmd.ID)
}
deletes := []string{
@ -367,7 +367,7 @@ func (ss *sqlStore) AddOrgUser(ctx context.Context, cmd *org.AddOrgUserCommand)
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.OrgID); err != nil {
return err
} else if len(res) != 1 {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to add user to organization with ID: %d", cmd.OrgID)
}
entity := org.OrgUser{
@ -524,7 +524,7 @@ func (ss *sqlStore) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*o
}
if !exists {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to get org by ID: %d", query.ID)
}
return nil
})
@ -638,7 +638,7 @@ func (ss *sqlStore) GetByName(ctx context.Context, query *org.GetOrgByNameQuery)
}
if !exists {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to get org by name: %s", query.Name)
}
return nil
})

View File

@ -139,7 +139,7 @@ func verifyExistingOrg(sess *DBSession, orgId int64) error {
return err
}
if !has {
return org.ErrOrgNotFound
return org.ErrOrgNotFound.Errorf("failed to verify existing org")
}
return nil
}