mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Differentiate the ErrOrgNotFound error messages (#64131)
* Better org not found error messages
This commit is contained in:
parent
1aadafe7d8
commit
fde96c91c1
@ -70,14 +70,14 @@ func (hs *HTTPServer) AdminCreateUser(c *contextmodel.ReqContext) response.Respo
|
|||||||
usr, err := hs.userService.Create(c.Req.Context(), &cmd)
|
usr, err := hs.userService.Create(c.Req.Context(), &cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, org.ErrOrgNotFound) {
|
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) {
|
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()
|
metrics.MApiAdminUserCreate.Inc()
|
||||||
|
@ -209,7 +209,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
|||||||
|
|
||||||
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "organization not found", respJSON.Get("message").MustString())
|
assert.Equal(t, org.ErrOrgNotFound.Error(), respJSON.Get("message").MustString())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -215,8 +215,12 @@ func UseOrgFromContextParams(c *contextmodel.ReqContext) (int64, error) {
|
|||||||
orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
||||||
|
|
||||||
// Special case of macaron handling invalid params
|
// Special case of macaron handling invalid params
|
||||||
if orgID == 0 || err != nil {
|
if err != nil {
|
||||||
return 0, org.ErrOrgNotFound
|
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
|
return orgID, nil
|
||||||
|
@ -7,15 +7,16 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models/roletype"
|
"github.com/grafana/grafana/pkg/models/roletype"
|
||||||
"github.com/grafana/grafana/pkg/services/user"
|
"github.com/grafana/grafana/pkg/services/user"
|
||||||
|
"github.com/grafana/grafana/pkg/util/errutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Typed errors
|
// Typed errors
|
||||||
var (
|
var (
|
||||||
ErrOrgNotFound = errors.New("organization not found")
|
|
||||||
ErrOrgNameTaken = errors.New("organization name is taken")
|
ErrOrgNameTaken = errors.New("organization name is taken")
|
||||||
ErrLastOrgAdmin = errors.New("cannot remove last organization admin")
|
ErrLastOrgAdmin = errors.New("cannot remove last organization admin")
|
||||||
ErrOrgUserNotFound = errors.New("cannot find the organization user")
|
ErrOrgUserNotFound = errors.New("cannot find the organization user")
|
||||||
ErrOrgUserAlreadyAdded = errors.New("user is already added to organization")
|
ErrOrgUserAlreadyAdded = errors.New("user is already added to organization")
|
||||||
|
ErrOrgNotFound = errutil.NewBase(errutil.StatusNotFound, "org.notFound", errutil.WithPublicMessage("organization not found"))
|
||||||
)
|
)
|
||||||
|
|
||||||
type Org struct {
|
type Org struct {
|
||||||
|
@ -64,7 +64,7 @@ func (ss *sqlStore) Get(ctx context.Context, orgID int64) (*org.Org, error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !has {
|
if !has {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to get organization with ID: %d", orgID)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -147,7 +147,7 @@ func (ss *sqlStore) Update(ctx context.Context, cmd *org.UpdateOrgCommand) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
if affectedRows == 0 {
|
if affectedRows == 0 {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to update organization with ID: %d", cmd.OrgId)
|
||||||
}
|
}
|
||||||
|
|
||||||
sess.PublishAfterCommit(&events.OrgUpdated{
|
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 {
|
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.ID); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(res) != 1 {
|
} else if len(res) != 1 {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to delete organisation with ID: %d", cmd.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
deletes := []string{
|
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 {
|
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.OrgID); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(res) != 1 {
|
} 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{
|
entity := org.OrgUser{
|
||||||
@ -524,7 +524,7 @@ func (ss *sqlStore) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*o
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to get org by ID: %d", query.ID)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -638,7 +638,7 @@ func (ss *sqlStore) GetByName(ctx context.Context, query *org.GetOrgByNameQuery)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to get org by name: %s", query.Name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
@ -139,7 +139,7 @@ func verifyExistingOrg(sess *DBSession, orgId int64) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !has {
|
if !has {
|
||||||
return org.ErrOrgNotFound
|
return org.ErrOrgNotFound.Errorf("failed to verify existing org")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user