errors

package
v2.75.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 11 Imported by: 22

Documentation

Overview

Package errors implements errors defined in the Conjure specification.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultUnauthorized          = ErrorType{Unauthorized, errorNameUnauthorized}
	DefaultPermissionDenied      = ErrorType{PermissionDenied, errorNamePermissionDenied}
	DefaultInvalidArgument       = ErrorType{InvalidArgument, errorNameInvalidArgument}
	DefaultNotFound              = ErrorType{NotFound, errorNameNotFound}
	DefaultConflict              = ErrorType{Conflict, errorNameConflict}
	DefaultRequestEntityTooLarge = ErrorType{RequestEntityTooLarge, errorNameRequestEntityTooLarge}
	DefaultFailedPrecondition    = ErrorType{FailedPrecondition, errorNameFailedPrecondition}
	DefaultInternal              = ErrorType{Internal, errorNameInternal}
	DefaultTimeout               = ErrorType{Timeout, errorNameTimeout}
)

Functions

func IsConflict added in v2.27.0

func IsConflict(err error) bool

IsConflict returns true if an error is an instance of default conflict type.

func IsFailedPrecondition added in v2.27.0

func IsFailedPrecondition(err error) bool

IsFailedPrecondition returns true if an error is an instance of default failed precondition type.

func IsInternal added in v2.27.0

func IsInternal(err error) bool

IsInternal returns true if an error is an instance of default internal type.

func IsInvalidArgument added in v2.27.0

func IsInvalidArgument(err error) bool

IsInvalidArgument returns true if an error is an instance of default invalid argument type.

func IsNotFound added in v2.27.0

func IsNotFound(err error) bool

IsNotFound returns true if an error is an instance of default not found type.

func IsPermissionDenied added in v2.27.0

func IsPermissionDenied(err error) bool

IsPermissionDenied returns true if an error is an instance of default permission denied type.

func IsRequestEntityTooLarge added in v2.27.0

func IsRequestEntityTooLarge(err error) bool

IsRequestEntityTooLarge returns true if an error is an instance of default request entity too large type.

func IsTimeout added in v2.27.0

func IsTimeout(err error) bool

IsTimeout returns true if an error is an instance of default timeout type.

func IsUnauthorized added in v2.27.0

func IsUnauthorized(err error) bool

IsUnauthorized returns true if an error is an instance of default unauthorized type.

func NewWrappedError added in v2.1.0

func NewWrappedError(conjureErr Error, err error) error

NewWrappedError is a convenience function for adding an underlying error to a conjure error as additional context. This exists so that the conjure error becomes the RootCause, which is used to extract the conjure error for serialization when returned by a server handler.

The conjure error is wrapped using err's Error() message, and params if ParamStorer is implemented. All other context is discarded, including cause stack (i.e. stacktrace) and type information.

DEPRECATED: Use WrapWithNewError for generic errors or WrapWithMyErrorType for conjure-generated errors.

func RegisterErrorType

func RegisterErrorType(name string, typ reflect.Type)

RegisterErrorType registers an error name and its go type in a global registry. The type should be a struct type whose pointer implements Error. Panics if name is already registered or *type does not implement Error.

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, e Error)

WriteErrorResponse writes error to the response writer.

TODO This function is subject to change.

Types

type Error

type Error interface {
	error
	// Code returns an enum describing error category.
	Code() ErrorCode
	// Name returns an error name identifying error type.
	Name() string
	// InstanceID returns unique identifier of this particular error instance.
	InstanceID() uuid.UUID

	wparams.ParamStorer
}

Error is an error intended for transport through RPC channels such as HTTP responses.

Error is represented by its error code, an error name identifying the type of error and an optional set of named parameters detailing the error.

func GetConjureError added in v2.4.0

func GetConjureError(err error) Error

GetConjureError recursively searches for an error of type Error in a chain of causes. It returns the first instance that it finds, or nil if one is not found.

func NewConflict

func NewConflict(parameters ...wparams.ParamStorer) Error

NewConflict returns new error instance of default conflict type.

func NewError

func NewError(errorType ErrorType, parameters ...wparams.ParamStorer) Error

NewError returns new instance of an error of the specified type with provided parameters.

func NewFailedPrecondition

func NewFailedPrecondition(parameters ...wparams.ParamStorer) Error

NewFailedPrecondition returns new error instance of default failed precondition type.

func NewInternal

func NewInternal(parameters ...wparams.ParamStorer) Error

NewInternal returns new error instance of default internal type.

func NewInvalidArgument

func NewInvalidArgument(parameters ...wparams.ParamStorer) Error

NewInvalidArgument returns new error instance of default invalid argument type.

func NewNotFound

func NewNotFound(parameters ...wparams.ParamStorer) Error

NewNotFound returns new error instance of default not found type.

func NewPermissionDenied

func NewPermissionDenied(parameters ...wparams.ParamStorer) Error

NewPermissionDenied returns new error instance of default permission denied type.

func NewRequestEntityTooLarge

func NewRequestEntityTooLarge(parameters ...wparams.ParamStorer) Error

NewRequestEntityTooLarge returns new error instance of default request entity too large type.

func NewTimeout

func NewTimeout(parameters ...wparams.ParamStorer) Error

NewTimeout returns new error instance of default timeout type.

func NewUnauthorized added in v2.21.0

func NewUnauthorized(parameters ...wparams.ParamStorer) Error

NewUnauthorized returns new error instance of default unauthorized type.

func UnmarshalError

func UnmarshalError(body []byte) (Error, error)

UnmarshalError attempts to deserialize the message to a known implementation of Error. Custom error types should be registered using RegisterErrorType. If the ErrorName is not recognized, a genericError is returned with all params marked unsafe. If we fail to unmarshal to a generic SerializableError or to the type specified by ErrorName, an error is returned.

func WrapWithConflict added in v2.4.0

func WrapWithConflict(cause error, parameters ...wparams.ParamStorer) Error

WrapWithConflict returns new error instance of default conflict type wrapping an existing error.

func WrapWithFailedPrecondition added in v2.4.0

func WrapWithFailedPrecondition(cause error, parameters ...wparams.ParamStorer) Error

WrapWithFailedPrecondition returns new error instance of default failed precondition type wrapping an existing error.

func WrapWithInternal added in v2.4.0

func WrapWithInternal(cause error, parameters ...wparams.ParamStorer) Error

WrapWithInternal returns new error instance of default internal type wrapping an existing error.

func WrapWithInvalidArgument added in v2.4.0

func WrapWithInvalidArgument(cause error, parameters ...wparams.ParamStorer) Error

WrapWithInvalidArgument returns new error instance of default invalid argument type wrapping an existing error.

func WrapWithNewError added in v2.4.0

func WrapWithNewError(cause error, errorType ErrorType, parameters ...wparams.ParamStorer) Error

WrapWithNewError returns new instance of an error of the specified type with provided parameters wrapping an existing error.

func WrapWithNotFound added in v2.4.0

func WrapWithNotFound(cause error, parameters ...wparams.ParamStorer) Error

WrapWithNotFound returns new error instance of default not found type wrapping an existing error.

func WrapWithPermissionDenied added in v2.4.0

func WrapWithPermissionDenied(cause error, parameters ...wparams.ParamStorer) Error

WrapWithPermissionDenied returns new error instance of default permission denied type wrapping an existing error.

func WrapWithRequestEntityTooLarge added in v2.4.0

func WrapWithRequestEntityTooLarge(cause error, parameters ...wparams.ParamStorer) Error

WrapWithRequestEntityTooLarge returns new error instance of default request entity too large type wrapping an existing error.

func WrapWithTimeout added in v2.4.0

func WrapWithTimeout(cause error, parameters ...wparams.ParamStorer) Error

WrapWithTimeout returns new error instance of default timeout type wrapping an existing error.

func WrapWithUnauthorized added in v2.21.0

func WrapWithUnauthorized(cause error, parameters ...wparams.ParamStorer) Error

WrapWithUnauthorized returns new error instance of default unauthorized type wrapping an existing error.

type ErrorCode

type ErrorCode int16

ErrorCode is an enum describing error category.

Each error code has associated HTTP status codes.

const (

	// Unauthorized has status code 401 Unauthorized.
	Unauthorized ErrorCode
	// PermissionDenied has status code 403 Forbidden.
	PermissionDenied
	// InvalidArgument has status code 400 BadRequest.
	InvalidArgument
	// NotFound  has status code 404 NotFound.
	NotFound
	// Conflict has status code 409 Conflict.
	Conflict
	// RequestEntityTooLarge has status code 413 RequestEntityTooLarge.
	RequestEntityTooLarge
	// FailedPrecondition has status code 500 InternalServerError.
	FailedPrecondition
	// Internal has status code 500 InternalServerError.
	Internal
	// Timeout has status code 500 InternalServerError.
	Timeout
	// CustomClient has status code 400 BadRequest.
	CustomClient
	// CustomServer has status code 500 InternalServerError.
	CustomServer
)

func (ErrorCode) MarshalText

func (ec ErrorCode) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (ErrorCode) StatusCode

func (ec ErrorCode) StatusCode() int

StatusCode returns HTTP status code associated with this error code.

func (ErrorCode) String

func (ec ErrorCode) String() string

String representation of this error code.

For example "NOT_FOUND", "CONFLICT", "PERMISSION_DENIED" or "TIMEOUT".

func (*ErrorCode) UnmarshalText

func (ec *ErrorCode) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type ErrorType

type ErrorType struct {
	// contains filtered or unexported fields
}

ErrorType represents certain class of errors. Each error type is uniquely identified by an error name and has assigned one of possible error codes.

Error type should be a compile-time constant and is considered part of the API of a service that produces error of such type.

func MustErrorType

func MustErrorType(code ErrorCode, name string) ErrorType

MustErrorType is panicking equivalent of NewErrorType.

func NewErrorType

func NewErrorType(code ErrorCode, name string) (ErrorType, error)

NewErrorType returns error type with the provided error code and name, or returns an error if error of such type cannot be created.

Error name must be in the "PascalCase:PascalCase" format, for example "Default:PermissionDenied", "Facebook:LikeAlreadyGiven" or "MyApplication:ErrorSpecificToMyBusinessDomain". The first part of an error name is a namespace and the second part contains cause on an error. "Default" namespace is reserved for error types defined in this package.

For example:

var ErrorLikeAlreadyGiven = errors.MustErrorType(
  errors.ErrorCodeConflict,
  "Facebook:LikeAlreadyGiven",
)

func (ErrorType) Code

func (et ErrorType) Code() ErrorCode

func (ErrorType) Name

func (et ErrorType) Name() string

func (ErrorType) String

func (et ErrorType) String() string

String representation of an error type.

For example:

"NOT_FOUND MyApplication:MissingData"

type SerializableError

type SerializableError struct {
	ErrorCode       ErrorCode       `json:"errorCode"`
	ErrorName       string          `json:"errorName"`
	ErrorInstanceID uuid.UUID       `json:"errorInstanceId"`
	Parameters      json.RawMessage `json:"parameters,omitempty"`
}

SerializableError is serializable representation of an error, it includes error code, name, instance id and parameters. It can be used to implement error marshaling & unmarshaling of concrete types implementing an Error interface.

This type does not marshal & unmarshal parameters - that should be responsibility of a type implementing an Error.

This is an example of a valid JSON object representing an error:

{
  "errorCode": "CONFLICT",
  "errorName": "Facebook:LikeAlreadyGiven",
  "errorInstanceId": "00010203-0405-0607-0809-0a0b0c0d0e0f",
  "parameters": {
    "postId": "5aa734gs3579",
    "userId": 642764872364
  }
}

Jump to

Keyboard shortcuts

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