Documentation
¶
Overview ¶
Package svcerr is a library to register error handling HTTP middleware and convert various error types defined in api/ into proper HTTP status codes.
Index ¶
- Constants
- func AppErrorUnaryInterceptor(log grpclogging.ServiceLogger) ...
- func BusinessException(ctx context.Context, msg string) *common.Exception
- func InfrastructureException(ctx context.Context, msg string) *common.Exception
- func SecurityException(ctx context.Context, msg string) *common.Exception
- func ServiceException(ctx context.Context, msg string) *common.Exception
- func UnexpectedException(ctx context.Context, msg string) *common.Exception
- type BusinessError
- type HTTPErrorHandler
- type InfrastructureError
- type SecurityError
- type ServiceError
- type UnexpectedError
Constants ¶
const ( // TimestampFormat uses RFC3339 for all timestamps. TimestampFormat = time.RFC3339 )
Variables ¶
This section is empty.
Functions ¶
func AppErrorUnaryInterceptor ¶
func AppErrorUnaryInterceptor(log grpclogging.ServiceLogger) func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
AppErrorUnaryInterceptor intercepts all gRPC responses right before they're returned to the caller, and processes errors. Errors come in several different types, all of which are coerced into a gRPC Status error, with a details field set to a single element array with an element of type common.ExceptionResponse.
By convention, the application should only return errors that fall into the following handled cases:
a response without an error has body with a populated `exception` field. We inspect the exception object and construct a grpc error with the appropriate status code and include the original exception proto message in the gRPC error `details` field.
A response without a response body and with a gRPC error, where the gRPC error has a `details` field populated containing a single element of type common.Exception.
A response without a response body and with a gRPC error, where the gRPC error does not have the `details` field populated.
All other cases are a convention failure and indicate a bug in the error handling logic itself, which must be made conventional. Non-conventional errors must not be displayed to the user, as they indicate a bug that contains information not explicilty treated as presentable to the caller. Non-conventional errors are replaced with a generic "Internal server error" error, and must log the original error so that we can debug and remove them.
func BusinessException ¶
BusinessException creates a protobuf business exception.
func InfrastructureException ¶
InfrastructureException creates a protobuf infrastructure exception.
func SecurityException ¶
SecurityException creates a protobuf security exception.
func ServiceException ¶
ServiceException creates a protobuf service exception.
Types ¶
type BusinessError ¶ added in v0.4.0
type BusinessError struct {
// contains filtered or unexported fields
}
BusinessError is a raw Luther expected business logic error.
func NewBusinessError ¶ added in v0.4.0
func NewBusinessError(message string) *BusinessError
NewBusinessError constructs a business error.
type HTTPErrorHandler ¶
type HTTPErrorHandler = func(context.Context, *runtime.ServeMux, runtime.Marshaler, http.ResponseWriter, *http.Request, error)
HTTPErrorHandler is an interface for intercepting errors.
func ErrIntercept ¶
func ErrIntercept(log grpclogging.ServiceLogger, handlers ...HTTPErrorHandler) HTTPErrorHandler
ErrIntercept intercepts error messages generated by the REST/JSON HTTP server. This includes errors already processed by AppErrorUnaryInterceptor, as well as errors generated by other endpoints. This is the very last chance to process the error before it is presented to the caller!
type InfrastructureError ¶ added in v0.4.0
type InfrastructureError struct {
// contains filtered or unexported fields
}
InfrastructureError is a raw Luther infrastructure error.
func NewInfrastructureError ¶ added in v0.4.0
func NewInfrastructureError(message string) *InfrastructureError
NewInfrastructureError constructs a infrastructure error.
type SecurityError ¶ added in v0.4.0
type SecurityError struct {
// contains filtered or unexported fields
}
SecurityError is a raw Luther security error.
func NewSecurityError ¶ added in v0.4.0
func NewSecurityError(message string) *SecurityError
NewSecurityError constructs a security error.
type ServiceError ¶ added in v0.4.0
type ServiceError struct {
// contains filtered or unexported fields
}
ServiceError is a raw Luther service error.
func NewServiceError ¶ added in v0.4.0
func NewServiceError(message string) *ServiceError
NewServiceError constructs a service error.
type UnexpectedError ¶ added in v0.4.0
type UnexpectedError struct {
// contains filtered or unexported fields
}
UnexpectedError is a raw Luther expected business logic error.
func NewUnexpectedError ¶ added in v0.4.0
func NewUnexpectedError(message string) *UnexpectedError
NewUnexpectedError constructs an unexpected error.