gateway: improve ErrFromRemote handling

Don't create an ErrFromRemote wrapping the returned error but
wrap the ErrFromRemote

Also use xerrors Is/As to get the underlying error to return to api clients
while maintaining context for logging
This commit is contained in:
Simone Gotti 2019-05-23 12:59:11 +02:00
parent 9b2ce717c7
commit 4d7605a86b
12 changed files with 152 additions and 90 deletions

View File

@ -30,11 +30,29 @@ type ErrorResponse struct {
} }
func ErrorResponseFromError(err error) *ErrorResponse { func ErrorResponseFromError(err error) *ErrorResponse {
var aerr error
// use inner errors if of these types
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
fallthrough var cerr *util.ErrBadRequest
case util.IsErrNotFound(err): errors.As(err, &cerr)
return &ErrorResponse{Message: err.Error()} aerr = cerr
case errors.Is(err, &util.ErrNotFound{}):
var cerr *util.ErrNotFound
errors.As(err, &cerr)
aerr = cerr
case errors.Is(err, &util.ErrForbidden{}):
var cerr *util.ErrForbidden
errors.As(err, &cerr)
aerr = cerr
case errors.Is(err, &util.ErrUnauthorized{}):
var cerr *util.ErrUnauthorized
errors.As(err, &cerr)
aerr = cerr
}
if aerr != nil {
return &ErrorResponse{Message: aerr.Error()}
} }
// on generic error return an generic message to not leak the real error // on generic error return an generic message to not leak the real error
@ -53,12 +71,18 @@ func httpError(w http.ResponseWriter, err error) bool {
return true return true
} }
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write(resj) w.Write(resj)
case util.IsErrNotFound(err): case errors.Is(err, &util.ErrNotFound{}):
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write(resj) w.Write(resj)
case errors.Is(err, &util.ErrForbidden{}):
w.WriteHeader(http.StatusForbidden)
w.Write(resj)
case errors.Is(err, &util.ErrUnauthorized{}):
w.WriteHeader(http.StatusUnauthorized)
w.Write(resj)
default: default:
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write(resj) w.Write(resj)

View File

@ -61,7 +61,7 @@ func (h *ActionHandler) IsOrgOwner(ctx context.Context, orgID string) (bool, err
userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID) userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID)
if err != nil { if err != nil {
return false, ErrFromRemote(resp, errors.Errorf("failed to get user orgs: %w", err)) return false, errors.Errorf("failed to get user orgs: %w", ErrFromRemote(resp, err))
} }
for _, userOrg := range userOrgs { for _, userOrg := range userOrgs {
@ -96,7 +96,7 @@ func (h *ActionHandler) IsProjectOwner(ctx context.Context, ownerType types.Conf
if ownerType == types.ConfigTypeOrg { if ownerType == types.ConfigTypeOrg {
userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID) userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID)
if err != nil { if err != nil {
return false, ErrFromRemote(resp, errors.Errorf("failed to get user orgs: %w", err)) return false, errors.Errorf("failed to get user orgs: %w", ErrFromRemote(resp, err))
} }
for _, userOrg := range userOrgs { for _, userOrg := range userOrgs {
@ -132,7 +132,7 @@ func (h *ActionHandler) IsProjectMember(ctx context.Context, ownerType types.Con
if ownerType == types.ConfigTypeOrg { if ownerType == types.ConfigTypeOrg {
userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID) userOrgs, resp, err := h.configstoreClient.GetUserOrgs(ctx, userID)
if err != nil { if err != nil {
return false, ErrFromRemote(resp, errors.Errorf("failed to get user orgs: %w", err)) return false, errors.Errorf("failed to get user orgs: %w", ErrFromRemote(resp, err))
} }
for _, userOrg := range userOrgs { for _, userOrg := range userOrgs {
@ -153,14 +153,14 @@ func (h *ActionHandler) IsVariableOwner(ctx context.Context, parentType types.Co
case types.ConfigTypeProjectGroup: case types.ConfigTypeProjectGroup:
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, parentRef) pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, parentRef)
if err != nil { if err != nil {
return false, ErrFromRemote(resp, errors.Errorf("failed to get project group %q: %w", parentRef, err)) return false, errors.Errorf("failed to get project group %q: %w", parentRef, ErrFromRemote(resp, err))
} }
ownerType = pg.OwnerType ownerType = pg.OwnerType
ownerID = pg.OwnerID ownerID = pg.OwnerID
case types.ConfigTypeProject: case types.ConfigTypeProject:
p, resp, err := h.configstoreClient.GetProject(ctx, parentRef) p, resp, err := h.configstoreClient.GetProject(ctx, parentRef)
if err != nil { if err != nil {
return false, ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", parentRef, err)) return false, errors.Errorf("failed to get project %q: %w", parentRef, ErrFromRemote(resp, err))
} }
ownerType = p.OwnerType ownerType = p.OwnerType
ownerID = p.OwnerID ownerID = p.OwnerID

View File

@ -109,7 +109,7 @@ func (h *ActionHandler) CreateOrg(ctx context.Context, req *CreateOrgRequest) (*
h.log.Infof("creating organization") h.log.Infof("creating organization")
org, resp, err := h.configstoreClient.CreateOrg(ctx, org) org, resp, err := h.configstoreClient.CreateOrg(ctx, org)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create organization: %w", err)) return nil, errors.Errorf("failed to create organization: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("organization %s created, ID: %s", org.Name, org.ID) h.log.Infof("organization %s created, ID: %s", org.Name, org.ID)
@ -132,7 +132,7 @@ func (h *ActionHandler) DeleteOrg(ctx context.Context, orgRef string) error {
resp, err = h.configstoreClient.DeleteOrg(ctx, orgRef) resp, err = h.configstoreClient.DeleteOrg(ctx, orgRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete org: %w", err)) return errors.Errorf("failed to delete org: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }
@ -163,7 +163,7 @@ func (h *ActionHandler) AddOrgMember(ctx context.Context, orgRef, userRef string
orgmember, resp, err := h.configstoreClient.AddOrgMember(ctx, orgRef, userRef, role) orgmember, resp, err := h.configstoreClient.AddOrgMember(ctx, orgRef, userRef, role)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to add/update organization member: %w", err)) return nil, errors.Errorf("failed to add/update organization member: %w", ErrFromRemote(resp, err))
} }
return &AddOrgMemberResponse{ return &AddOrgMemberResponse{
@ -189,7 +189,7 @@ func (h *ActionHandler) RemoveOrgMember(ctx context.Context, orgRef, userRef str
resp, err = h.configstoreClient.RemoveOrgMember(ctx, orgRef, userRef) resp, err = h.configstoreClient.RemoveOrgMember(ctx, orgRef, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to remove organization member: %w", err)) return errors.Errorf("failed to remove organization member: %w", ErrFromRemote(resp, err))
} }
return nil return nil

View File

@ -62,7 +62,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
user, resp, err := h.configstoreClient.GetUser(ctx, curUserID) user, resp, err := h.configstoreClient.GetUser(ctx, curUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", curUserID, err)) return nil, errors.Errorf("failed to get user %q: %w", curUserID, ErrFromRemote(resp, err))
} }
parentRef := req.ParentRef parentRef := req.ParentRef
if parentRef == "" { if parentRef == "" {
@ -72,7 +72,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, parentRef) pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, parentRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project group %q: %w", parentRef, err)) return nil, errors.Errorf("failed to get project group %q: %w", parentRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
@ -97,7 +97,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
_, resp, err = h.configstoreClient.GetProject(ctx, projectPath) _, resp, err = h.configstoreClient.GetProject(ctx, projectPath)
if err != nil { if err != nil {
if resp != nil && resp.StatusCode != http.StatusNotFound { if resp != nil && resp.StatusCode != http.StatusNotFound {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", req.Name, err)) return nil, errors.Errorf("failed to get project %q: %w", req.Name, ErrFromRemote(resp, err))
} }
} else { } else {
return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath)) return nil, util.NewErrBadRequest(errors.Errorf("project %q already exists", projectPath))
@ -105,7 +105,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
var la *types.LinkedAccount var la *types.LinkedAccount
@ -155,7 +155,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
h.log.Infof("creating project") h.log.Infof("creating project")
rp, resp, err := h.configstoreClient.CreateProject(ctx, p) rp, resp, err := h.configstoreClient.CreateProject(ctx, p)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create project: %w", err)) return nil, errors.Errorf("failed to create project: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("project %s created, ID: %s", p.Name, p.ID) h.log.Infof("project %s created, ID: %s", p.Name, p.ID)
@ -170,7 +170,7 @@ type UpdateProjectRequest struct {
func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, req *UpdateProjectRequest) (*csapi.Project, error) { func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, req *UpdateProjectRequest) (*csapi.Project, error) {
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef) p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", projectRef, err)) return nil, errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
@ -187,7 +187,7 @@ func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, re
h.log.Infof("updating project") h.log.Infof("updating project")
rp, resp, err := h.configstoreClient.UpdateProject(ctx, p.ID, p.Project) rp, resp, err := h.configstoreClient.UpdateProject(ctx, p.ID, p.Project)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to update project: %w", err)) return nil, errors.Errorf("failed to update project: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("project %s updated, ID: %s", p.Name, p.ID) h.log.Infof("project %s updated, ID: %s", p.Name, p.ID)
@ -199,12 +199,12 @@ func (h *ActionHandler) ProjectUpdateRepoLinkedAccount(ctx context.Context, proj
user, resp, err := h.configstoreClient.GetUser(ctx, curUserID) user, resp, err := h.configstoreClient.GetUser(ctx, curUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", curUserID, err)) return nil, errors.Errorf("failed to get user %q: %w", curUserID, ErrFromRemote(resp, err))
} }
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef) p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", projectRef, err)) return nil, errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
@ -217,7 +217,7 @@ func (h *ActionHandler) ProjectUpdateRepoLinkedAccount(ctx context.Context, proj
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, p.RemoteSourceID) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, p.RemoteSourceID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", p.RemoteSourceID, err)) return nil, errors.Errorf("failed to get remote source %q: %w", p.RemoteSourceID, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
var la *types.LinkedAccount var la *types.LinkedAccount
@ -248,7 +248,7 @@ func (h *ActionHandler) ProjectUpdateRepoLinkedAccount(ctx context.Context, proj
h.log.Infof("updating project") h.log.Infof("updating project")
rp, resp, err := h.configstoreClient.UpdateProject(ctx, p.ID, p.Project) rp, resp, err := h.configstoreClient.UpdateProject(ctx, p.ID, p.Project)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to update project: %w", err)) return nil, errors.Errorf("failed to update project: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("project %s updated, ID: %s", p.Name, p.ID) h.log.Infof("project %s updated, ID: %s", p.Name, p.ID)
@ -298,7 +298,7 @@ func (h *ActionHandler) SetupProject(ctx context.Context, rs *types.RemoteSource
func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string) error { func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string) error {
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef) p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", projectRef, err)) return errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)
@ -311,7 +311,7 @@ func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string)
user, resp, err := h.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID) user, resp, err := h.configstoreClient.GetUserByLinkedAccount(ctx, p.LinkedAccountID)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get user with linked account id %q: %w", p.LinkedAccountID, err)) return errors.Errorf("failed to get user with linked account id %q: %w", p.LinkedAccountID, ErrFromRemote(resp, err))
} }
la := user.LinkedAccounts[p.LinkedAccountID] la := user.LinkedAccounts[p.LinkedAccountID]
@ -322,7 +322,7 @@ func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string)
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, la.RemoteSourceID)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", la.RemoteSourceID, err)) return errors.Errorf("failed to get remote source %q: %w", la.RemoteSourceID, ErrFromRemote(resp, err))
} }
// TODO(sgotti) update project repo path if the remote let us query by repository id // TODO(sgotti) update project repo path if the remote let us query by repository id
@ -333,7 +333,7 @@ func (h *ActionHandler) ReconfigProject(ctx context.Context, projectRef string)
func (h *ActionHandler) DeleteProject(ctx context.Context, projectRef string) error { func (h *ActionHandler) DeleteProject(ctx context.Context, projectRef string) error {
p, resp, err := h.configstoreClient.GetProject(ctx, projectRef) p, resp, err := h.configstoreClient.GetProject(ctx, projectRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", projectRef, err)) return errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)

View File

@ -63,7 +63,7 @@ func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProje
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentRef) pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, req.ParentRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project group %q: %w", req.ParentRef, err)) return nil, errors.Errorf("failed to get project group %q: %w", req.ParentRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
@ -76,7 +76,7 @@ func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProje
user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID) user, resp, err := h.configstoreClient.GetUser(ctx, req.CurrentUserID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", req.CurrentUserID, err)) return nil, errors.Errorf("failed to get user %q: %w", req.CurrentUserID, ErrFromRemote(resp, err))
} }
parentRef := req.ParentRef parentRef := req.ParentRef
@ -97,7 +97,7 @@ func (h *ActionHandler) CreateProjectGroup(ctx context.Context, req *CreateProje
h.log.Infof("creating projectGroup") h.log.Infof("creating projectGroup")
rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p) rp, resp, err := h.configstoreClient.CreateProjectGroup(ctx, p)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create projectGroup: %w", err)) return nil, errors.Errorf("failed to create projectGroup: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID) h.log.Infof("projectGroup %s created, ID: %s", rp.Name, rp.ID)
@ -112,7 +112,7 @@ type UpdateProjectGroupRequest struct {
func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *UpdateProjectGroupRequest) (*csapi.ProjectGroup, error) { func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef string, req *UpdateProjectGroupRequest) (*csapi.ProjectGroup, error) {
pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef) pg, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectGroupRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get project group %q: %w", projectGroupRef, err)) return nil, errors.Errorf("failed to get project group %q: %w", projectGroupRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, pg.OwnerType, pg.OwnerID)
@ -129,7 +129,7 @@ func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef
h.log.Infof("updating project group") h.log.Infof("updating project group")
rp, resp, err := h.configstoreClient.UpdateProjectGroup(ctx, pg.ID, pg.ProjectGroup) rp, resp, err := h.configstoreClient.UpdateProjectGroup(ctx, pg.ID, pg.ProjectGroup)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to update project group: %w", err)) return nil, errors.Errorf("failed to update project group: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("project group %q updated, ID: %s", pg.Name, pg.ID) h.log.Infof("project group %q updated, ID: %s", pg.Name, pg.ID)
@ -139,7 +139,7 @@ func (h *ActionHandler) UpdateProjectGroup(ctx context.Context, projectGroupRef
func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectRef string) error { func (h *ActionHandler) DeleteProjectGroup(ctx context.Context, projectRef string) error {
p, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectRef) p, resp, err := h.configstoreClient.GetProjectGroup(ctx, projectRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get project %q: %w", projectRef, err)) return errors.Errorf("failed to get project %q: %w", projectRef, ErrFromRemote(resp, err))
} }
isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID) isProjectOwner, err := h.IsProjectOwner(ctx, p.OwnerType, p.OwnerID)

View File

@ -108,7 +108,7 @@ func (h *ActionHandler) CreateRemoteSource(ctx context.Context, req *CreateRemot
h.log.Infof("creating remotesource") h.log.Infof("creating remotesource")
rs, resp, err := h.configstoreClient.CreateRemoteSource(ctx, rs) rs, resp, err := h.configstoreClient.CreateRemoteSource(ctx, rs)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create remotesource: %w", err)) return nil, errors.Errorf("failed to create remotesource: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID) h.log.Infof("remotesource %s created, ID: %s", rs.Name, rs.ID)
@ -162,7 +162,7 @@ func (h *ActionHandler) UpdateRemoteSource(ctx context.Context, req *UpdateRemot
h.log.Infof("updating remotesource") h.log.Infof("updating remotesource")
rs, resp, err = h.configstoreClient.UpdateRemoteSource(ctx, req.RemoteSourceRef, rs) rs, resp, err = h.configstoreClient.UpdateRemoteSource(ctx, req.RemoteSourceRef, rs)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to update remotesource: %w", err)) return nil, errors.Errorf("failed to update remotesource: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("remotesource %s updated", rs.Name) h.log.Infof("remotesource %s updated", rs.Name)
@ -176,7 +176,7 @@ func (h *ActionHandler) DeleteRemoteSource(ctx context.Context, rsRef string) er
resp, err := h.configstoreClient.DeleteRemoteSource(ctx, rsRef) resp, err := h.configstoreClient.DeleteRemoteSource(ctx, rsRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete remote source: %w", err)) return errors.Errorf("failed to delete remote source: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }

View File

@ -101,7 +101,7 @@ func (h *ActionHandler) CreateSecret(ctx context.Context, req *CreateSecretReque
rs, resp, err = h.configstoreClient.CreateProjectSecret(ctx, req.ParentRef, s) rs, resp, err = h.configstoreClient.CreateProjectSecret(ctx, req.ParentRef, s)
} }
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create secret: %w", err)) return nil, errors.Errorf("failed to create secret: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("secret %s created, ID: %s", rs.Name, rs.ID) h.log.Infof("secret %s created, ID: %s", rs.Name, rs.ID)
@ -127,7 +127,7 @@ func (h *ActionHandler) DeleteSecret(ctx context.Context, parentType types.Confi
resp, err = h.configstoreClient.DeleteProjectSecret(ctx, parentRef, name) resp, err = h.configstoreClient.DeleteProjectSecret(ctx, parentRef, name)
} }
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete secret: %w", err)) return errors.Errorf("failed to delete secret: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }

View File

@ -93,7 +93,7 @@ func (h *ActionHandler) CreateUser(ctx context.Context, req *CreateUserRequest)
h.log.Infof("creating user") h.log.Infof("creating user")
u, resp, err := h.configstoreClient.CreateUser(ctx, creq) u, resp, err := h.configstoreClient.CreateUser(ctx, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create user: %w", err)) return nil, errors.Errorf("failed to create user: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("user %s created, ID: %s", u.Name, u.ID) h.log.Infof("user %s created, ID: %s", u.Name, u.ID)
@ -121,7 +121,7 @@ func (h *ActionHandler) CreateUserToken(ctx context.Context, req *CreateUserToke
userRef := req.UserRef userRef := req.UserRef
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return "", ErrFromRemote(resp, errors.Errorf("failed to get user: %w", err)) return "", errors.Errorf("failed to get user: %w", ErrFromRemote(resp, err))
} }
// only admin or the same logged user can create a token // only admin or the same logged user can create a token
@ -138,7 +138,7 @@ func (h *ActionHandler) CreateUserToken(ctx context.Context, req *CreateUserToke
} }
res, resp, err := h.configstoreClient.CreateUserToken(ctx, userRef, creq) res, resp, err := h.configstoreClient.CreateUserToken(ctx, userRef, creq)
if err != nil { if err != nil {
return "", ErrFromRemote(resp, errors.Errorf("failed to create user token: %w", err)) return "", errors.Errorf("failed to create user token: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("token %q for user %q created", req.TokenName, userRef) h.log.Infof("token %q for user %q created", req.TokenName, userRef)
@ -159,11 +159,11 @@ func (h *ActionHandler) CreateUserLA(ctx context.Context, req *CreateUserLAReque
userRef := req.UserRef userRef := req.UserRef
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", userRef, err)) return nil, errors.Errorf("failed to get user %q: %w", userRef, ErrFromRemote(resp, err))
} }
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
var la *types.LinkedAccount var la *types.LinkedAccount
@ -208,7 +208,7 @@ func (h *ActionHandler) CreateUserLA(ctx context.Context, req *CreateUserLAReque
h.log.Infof("creating linked account") h.log.Infof("creating linked account")
la, resp, err = h.configstoreClient.CreateUserLA(ctx, userRef, creq) la, resp, err = h.configstoreClient.CreateUserLA(ctx, userRef, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create linked account: %w", err)) return nil, errors.Errorf("failed to create linked account: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("linked account %q for user %q created", la.ID, userRef) h.log.Infof("linked account %q for user %q created", la.ID, userRef)
@ -218,7 +218,7 @@ func (h *ActionHandler) CreateUserLA(ctx context.Context, req *CreateUserLAReque
func (h *ActionHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error { func (h *ActionHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error {
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", userRef, err)) return errors.Errorf("failed to get user %q: %w", userRef, ErrFromRemote(resp, err))
} }
laFound := false laFound := false
for _, ula := range user.LinkedAccounts { for _, ula := range user.LinkedAccounts {
@ -244,7 +244,7 @@ func (h *ActionHandler) UpdateUserLA(ctx context.Context, userRef string, la *ty
h.log.Infof("updating user %q linked account", userRef) h.log.Infof("updating user %q linked account", userRef)
la, resp, err = h.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq) la, resp, err = h.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to update user: %w", err)) return errors.Errorf("failed to update user: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("linked account %q for user %q updated", la.ID, userRef) h.log.Infof("linked account %q for user %q updated", la.ID, userRef)
@ -309,7 +309,7 @@ func (h *ActionHandler) RegisterUser(ctx context.Context, req *RegisterUserReque
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
@ -346,7 +346,7 @@ func (h *ActionHandler) RegisterUser(ctx context.Context, req *RegisterUserReque
h.log.Infof("creating user account") h.log.Infof("creating user account")
u, resp, err := h.configstoreClient.CreateUser(ctx, creq) u, resp, err := h.configstoreClient.CreateUser(ctx, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to create linked account: %w", err)) return nil, errors.Errorf("failed to create linked account: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("user %q created", req.UserName) h.log.Infof("user %q created", req.UserName)
@ -369,7 +369,7 @@ type LoginUserResponse struct {
func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) { func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
@ -392,7 +392,7 @@ func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*
user, resp, err := h.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID) user, resp, err := h.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user for remote user id %q and remote source %q: %w", remoteUserInfo.ID, rs.ID, err)) return nil, errors.Errorf("failed to get user for remote user id %q and remote source %q: %w", remoteUserInfo.ID, rs.ID, ErrFromRemote(resp, err))
} }
var la *types.LinkedAccount var la *types.LinkedAccount
@ -428,7 +428,7 @@ func (h *ActionHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*
h.log.Infof("updating user %q linked account", user.Name) h.log.Infof("updating user %q linked account", user.Name)
la, resp, err = h.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq) la, resp, err = h.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to update user: %w", err)) return nil, errors.Errorf("failed to update user: %w", ErrFromRemote(resp, err))
} }
h.log.Infof("linked account %q for user %q updated", la.ID, user.Name) h.log.Infof("linked account %q for user %q updated", la.ID, user.Name)
} }
@ -460,7 +460,7 @@ type AuthorizeResponse struct {
func (h *ActionHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) { func (h *ActionHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) {
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", req.RemoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
@ -495,7 +495,7 @@ type RemoteSourceAuthResponse struct {
func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) { func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) {
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", remoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", remoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
@ -505,7 +505,7 @@ func (h *ActionHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSource
user, resp, err := h.configstoreClient.GetUser(ctx, req.UserRef) user, resp, err := h.configstoreClient.GetUser(ctx, req.UserRef)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", req.UserRef, err)) return nil, errors.Errorf("failed to get user %q: %w", req.UserRef, ErrFromRemote(resp, err))
} }
curUserID := h.CurrentUserID(ctx) curUserID := h.CurrentUserID(ctx)
@ -736,7 +736,7 @@ func (h *ActionHandler) HandleOauth2Callback(ctx context.Context, code, state st
rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName) rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
if err != nil { if err != nil {
return nil, ErrFromRemote(resp, errors.Errorf("failed to get remote source %q: %w", remoteSourceName, err)) return nil, errors.Errorf("failed to get remote source %q: %w", remoteSourceName, ErrFromRemote(resp, err))
} }
h.log.Infof("rs: %s", util.Dump(rs)) h.log.Infof("rs: %s", util.Dump(rs))
@ -760,7 +760,7 @@ func (h *ActionHandler) DeleteUser(ctx context.Context, userRef string) error {
resp, err := h.configstoreClient.DeleteUser(ctx, userRef) resp, err := h.configstoreClient.DeleteUser(ctx, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete user: %w", err)) return errors.Errorf("failed to delete user: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }
@ -775,7 +775,7 @@ func (h *ActionHandler) DeleteUserLA(ctx context.Context, userRef, laID string)
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", userRef, err)) return errors.Errorf("failed to get user %q: %w", userRef, ErrFromRemote(resp, err))
} }
// only admin or the same logged user can create a token // only admin or the same logged user can create a token
@ -785,7 +785,7 @@ func (h *ActionHandler) DeleteUserLA(ctx context.Context, userRef, laID string)
resp, err = h.configstoreClient.DeleteUserLA(ctx, userRef, laID) resp, err = h.configstoreClient.DeleteUserLA(ctx, userRef, laID)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete user linked account: %w", err)) return errors.Errorf("failed to delete user linked account: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }
@ -800,7 +800,7 @@ func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName
user, resp, err := h.configstoreClient.GetUser(ctx, userRef) user, resp, err := h.configstoreClient.GetUser(ctx, userRef)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to get user %q: %w", userRef, err)) return errors.Errorf("failed to get user %q: %w", userRef, ErrFromRemote(resp, err))
} }
// only admin or the same logged user can create a token // only admin or the same logged user can create a token
@ -810,7 +810,7 @@ func (h *ActionHandler) DeleteUserToken(ctx context.Context, userRef, tokenName
resp, err = h.configstoreClient.DeleteUserToken(ctx, userRef, tokenName) resp, err = h.configstoreClient.DeleteUserToken(ctx, userRef, tokenName)
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete user token: %w", err)) return errors.Errorf("failed to delete user token: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }

View File

@ -114,26 +114,26 @@ func (h *ActionHandler) CreateVariable(ctx context.Context, req *CreateVariableR
var resp *http.Response var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true) cssecrets, resp, err = h.configstoreClient.GetProjectGroupSecrets(ctx, req.ParentRef, true)
if err != nil { if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Errorf("failed to get project group %q secrets: %w", req.ParentRef, err)) return nil, nil, errors.Errorf("failed to get project group %q secrets: %w", req.ParentRef, ErrFromRemote(resp, err))
} }
h.log.Infof("creating project group variable") h.log.Infof("creating project group variable")
rv, resp, err = h.configstoreClient.CreateProjectGroupVariable(ctx, req.ParentRef, v) rv, resp, err = h.configstoreClient.CreateProjectGroupVariable(ctx, req.ParentRef, v)
if err != nil { if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Errorf("failed to create variable: %w", err)) return nil, nil, errors.Errorf("failed to create variable: %w", ErrFromRemote(resp, err))
} }
case types.ConfigTypeProject: case types.ConfigTypeProject:
var err error var err error
var resp *http.Response var resp *http.Response
cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true) cssecrets, resp, err = h.configstoreClient.GetProjectSecrets(ctx, req.ParentRef, true)
if err != nil { if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Errorf("failed to get project %q secrets: %w", req.ParentRef, err)) return nil, nil, errors.Errorf("failed to get project %q secrets: %w", req.ParentRef, ErrFromRemote(resp, err))
} }
h.log.Infof("creating project variable") h.log.Infof("creating project variable")
rv, resp, err = h.configstoreClient.CreateProjectVariable(ctx, req.ParentRef, v) rv, resp, err = h.configstoreClient.CreateProjectVariable(ctx, req.ParentRef, v)
if err != nil { if err != nil {
return nil, nil, ErrFromRemote(resp, errors.Errorf("failed to create variable: %w", err)) return nil, nil, errors.Errorf("failed to create variable: %w", ErrFromRemote(resp, err))
} }
} }
h.log.Infof("variable %s created, ID: %s", rv.Name, rv.ID) h.log.Infof("variable %s created, ID: %s", rv.Name, rv.ID)
@ -160,7 +160,7 @@ func (h *ActionHandler) DeleteVariable(ctx context.Context, parentType types.Con
resp, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, name) resp, err = h.configstoreClient.DeleteProjectVariable(ctx, parentRef, name)
} }
if err != nil { if err != nil {
return ErrFromRemote(resp, errors.Errorf("failed to delete variable: %w", err)) return errors.Errorf("failed to delete variable: %w", ErrFromRemote(resp, err))
} }
return nil return nil
} }

View File

@ -31,15 +31,29 @@ type ErrorResponse struct {
} }
func ErrorResponseFromError(err error) *ErrorResponse { func ErrorResponseFromError(err error) *ErrorResponse {
var aerr error
// use inner errors if of these types
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
fallthrough var cerr *util.ErrBadRequest
case util.IsErrNotFound(err): errors.As(err, &cerr)
fallthrough aerr = cerr
case util.IsErrForbidden(err): case errors.Is(err, &util.ErrNotFound{}):
fallthrough var cerr *util.ErrNotFound
case util.IsErrUnauthorized(err): errors.As(err, &cerr)
return &ErrorResponse{Message: err.Error()} aerr = cerr
case errors.Is(err, &util.ErrForbidden{}):
var cerr *util.ErrForbidden
errors.As(err, &cerr)
aerr = cerr
case errors.Is(err, &util.ErrUnauthorized{}):
var cerr *util.ErrUnauthorized
errors.As(err, &cerr)
aerr = cerr
}
if aerr != nil {
return &ErrorResponse{Message: aerr.Error()}
} }
// on generic error return an generic message to not leak the real error // on generic error return an generic message to not leak the real error
@ -58,16 +72,16 @@ func httpError(w http.ResponseWriter, err error) bool {
return true return true
} }
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write(resj) w.Write(resj)
case util.IsErrNotFound(err): case errors.Is(err, &util.ErrNotFound{}):
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write(resj) w.Write(resj)
case util.IsErrForbidden(err): case errors.Is(err, &util.ErrForbidden{}):
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
w.Write(resj) w.Write(resj)
case util.IsErrUnauthorized(err): case errors.Is(err, &util.ErrUnauthorized{}):
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
w.Write(resj) w.Write(resj)
default: default:

View File

@ -47,11 +47,29 @@ type ErrorResponse struct {
} }
func ErrorResponseFromError(err error) *ErrorResponse { func ErrorResponseFromError(err error) *ErrorResponse {
var aerr error
// use inner errors if of these types
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
fallthrough var cerr *util.ErrBadRequest
case util.IsErrNotFound(err): errors.As(err, &cerr)
return &ErrorResponse{Message: err.Error()} aerr = cerr
case errors.Is(err, &util.ErrNotFound{}):
var cerr *util.ErrNotFound
errors.As(err, &cerr)
aerr = cerr
case errors.Is(err, &util.ErrForbidden{}):
var cerr *util.ErrForbidden
errors.As(err, &cerr)
aerr = cerr
case errors.Is(err, &util.ErrUnauthorized{}):
var cerr *util.ErrUnauthorized
errors.As(err, &cerr)
aerr = cerr
}
if aerr != nil {
return &ErrorResponse{Message: aerr.Error()}
} }
// on generic error return an generic message to not leak the real error // on generic error return an generic message to not leak the real error
@ -70,12 +88,18 @@ func httpError(w http.ResponseWriter, err error) bool {
return true return true
} }
switch { switch {
case util.IsErrBadRequest(err): case errors.Is(err, &util.ErrBadRequest{}):
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Write(resj) w.Write(resj)
case util.IsErrNotFound(err): case errors.Is(err, &util.ErrNotFound{}):
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
w.Write(resj) w.Write(resj)
case errors.Is(err, &util.ErrForbidden{}):
w.WriteHeader(http.StatusForbidden)
w.Write(resj)
case errors.Is(err, &util.ErrUnauthorized{}):
w.WriteHeader(http.StatusUnauthorized)
w.Write(resj)
default: default:
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write(resj) w.Write(resj)

View File

@ -70,7 +70,7 @@ func NewErrBadRequest(err error) *ErrBadRequest {
return &ErrBadRequest{Err: err} return &ErrBadRequest{Err: err}
} }
func IsErrBadRequest(err error) bool { func (*ErrBadRequest) Is(err error) bool {
_, ok := err.(*ErrBadRequest) _, ok := err.(*ErrBadRequest)
return ok return ok
} }
@ -89,7 +89,7 @@ func NewErrNotFound(err error) *ErrNotFound {
return &ErrNotFound{Err: err} return &ErrNotFound{Err: err}
} }
func IsErrNotFound(err error) bool { func (*ErrNotFound) Is(err error) bool {
_, ok := err.(*ErrNotFound) _, ok := err.(*ErrNotFound)
return ok return ok
} }
@ -108,7 +108,7 @@ func NewErrForbidden(err error) *ErrForbidden {
return &ErrForbidden{Err: err} return &ErrForbidden{Err: err}
} }
func IsErrForbidden(err error) bool { func (*ErrForbidden) Is(err error) bool {
_, ok := err.(*ErrForbidden) _, ok := err.(*ErrForbidden)
return ok return ok
} }
@ -127,7 +127,7 @@ func NewErrUnauthorized(err error) *ErrUnauthorized {
return &ErrUnauthorized{Err: err} return &ErrUnauthorized{Err: err}
} }
func IsErrUnauthorized(err error) bool { func (*ErrUnauthorized) Is(err error) bool {
_, ok := err.(*ErrUnauthorized) _, ok := err.(*ErrUnauthorized)
return ok return ok
} }