Skip to content

Commit

Permalink
fix(errors): improve HandleError to handle rest_errors.Error and fix …
Browse files Browse the repository at this point in the history
…Unauthenticated error handling (#7818)

* Update HandleError to process pointer to rest_errors.Error
  as an argument. This allows us to use errors.As directly,
  which simplifies the code and slightly improves performance.

* Fix a bug in HandleError where it was incorrectly assigning
  err to itself in the case of an Unauthenticated error.

Signed-off-by: Bart Smykla <[email protected]>
  • Loading branch information
bartsmykla authored Sep 22, 2023
1 parent 25726ff commit a4e04d2
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions pkg/core/rest/errors/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,42 @@ import (

func HandleError(ctx context.Context, response *restful.Response, err error, title string) {
log := kuma_log.AddFieldsFromCtx(core.Log.WithName("error"), ctx, context.Background())
var kumaErr types.Error
var kumaErr *types.Error
switch {
case store.IsResourceNotFound(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 404,
Title: title,
Detail: "Not found",
}
case errors.Is(err, &rest.InvalidResourceError{}):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: "Bad Request",
Detail: err.Error(),
}
case errors.Is(err, &registry.InvalidResourceType{}):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: "Bad Request",
Detail: err.Error(),
}
case store.IsResourcePreconditionFailed(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 412,
Title: title,
Detail: "Precondition Failed",
}
case errors.Is(err, &store.PreconditionError{}):
var err2 *store.PreconditionError
errors.As(err, &err2)
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: "Bad Request",
Detail: err2.Reason,
}
case err == store.ErrorInvalidOffset:
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: "Invalid offset",
Expand All @@ -71,7 +71,7 @@ func HandleError(ctx context.Context, response *restful.Response, err error, tit
},
}
case manager.IsMeshNotFound(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: "Mesh is not found",
Expand All @@ -83,7 +83,7 @@ func HandleError(ctx context.Context, response *restful.Response, err error, tit
},
}
case validators.IsValidationError(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: "Resource is not valid",
Expand All @@ -95,7 +95,7 @@ func HandleError(ctx context.Context, response *restful.Response, err error, tit
})
}
case api_server_types.IsMaxPageSizeExceeded(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: "Invalid page size",
Expand All @@ -107,7 +107,7 @@ func HandleError(ctx context.Context, response *restful.Response, err error, tit
},
}
case err == api_server_types.InvalidPageSize:
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: "Invalid page size",
Expand All @@ -119,60 +119,61 @@ func HandleError(ctx context.Context, response *restful.Response, err error, tit
},
}
case tokens.IsSigningKeyNotFound(err):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 404,
Title: "Signing Key not found",
Detail: err.Error(),
}
case errors.Is(err, &MethodNotAllowed{}):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 405,
Title: "Method not Allowed",
Detail: err.Error(),
}
case errors.Is(err, &Conflict{}):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 409,
Title: "Conflict",
Detail: err.Error(),
}
case errors.Is(err, &ServiceUnavailable{}):
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 503,
Title: "Service unavailable",
Detail: err.Error(),
}
case errors.Is(err, &access.AccessDeniedError{}):
var accessErr *access.AccessDeniedError
errors.As(err, &accessErr)
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 403,
Title: "Access Denied",
Detail: accessErr.Reason,
}
case errors.Is(err, &Unauthenticated{}):
var unauthenticated *Unauthenticated
errors.As(err, &err)
kumaErr = types.Error{
errors.As(err, &unauthenticated)
kumaErr = &types.Error{
Status: 401,
Title: title,
Detail: unauthenticated.Error(),
}
case err == tokens.IssuerDisabled:
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: err.Error(),
}
case err == multitenant.TenantMissingErr:
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 400,
Title: title,
Detail: err.Error(),
}
case errors.As(err, &kumaErr):
default:
log.Error(err, title)
kumaErr = types.Error{
kumaErr = &types.Error{
Status: 500,
Title: title,
Detail: "Internal Server Error",
Expand Down

0 comments on commit a4e04d2

Please sign in to comment.