svcerr

package
v0.13.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: BSD-3-Clause Imports: 14 Imported by: 1

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

View Source
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:

  1. 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.

  2. 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.

  3. 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

func BusinessException(ctx context.Context, msg string) *common.Exception

BusinessException creates a protobuf business exception.

func InfrastructureException

func InfrastructureException(ctx context.Context, msg string) *common.Exception

InfrastructureException creates a protobuf infrastructure exception.

func SecurityException

func SecurityException(ctx context.Context, msg string) *common.Exception

SecurityException creates a protobuf security exception.

func ServiceException

func ServiceException(ctx context.Context, msg string) *common.Exception

ServiceException creates a protobuf service exception.

func UnexpectedException

func UnexpectedException(ctx context.Context, msg string) *common.Exception

UnexpectedException creates a protobuf unexpected 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.

func (*BusinessError) Error added in v0.4.0

func (s *BusinessError) Error() string

Error implements error.

type HTTPErrorHandler

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.

func (*InfrastructureError) Error added in v0.4.0

func (s *InfrastructureError) Error() string

Error implements 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.

func (*SecurityError) Error added in v0.4.0

func (s *SecurityError) Error() string

Error implements 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.

func (*ServiceError) Error added in v0.4.0

func (s *ServiceError) Error() string

Error implements 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.

func (*UnexpectedError) Error added in v0.4.0

func (s *UnexpectedError) Error() string

Error implements error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL