errors

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2019 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package errors implements errors defined in the Conjure specification.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPermissionDenied      = ErrorType{PermissionDenied, "Default:PermissionDenied"}
	DefaultInvalidArgument       = ErrorType{InvalidArgument, "Default:InvalidArgument"}
	DefaultNotFound              = ErrorType{NotFound, "Default:NotFound"}
	DefaultConflict              = ErrorType{Conflict, "Default:Conflict"}
	DefaultRequestEntityTooLarge = ErrorType{RequestEntityTooLarge, "Default:RequestEntityTooLarge"}
	DefaultFailedPrecondition    = ErrorType{FailedPrecondition, "Default:FailedPrecondition"}
	DefaultInternal              = ErrorType{Internal, "Default:Internal"}
	DefaultTimeout               = ErrorType{Timeout, "Default:Timeout"}
)

Functions

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, e SerializableError)

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
	// Parameters returns a set of named parameters detailing this particular error instance,
	// for example error message.
	Parameters() map[string]interface{}
}

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 NewConflict

func NewConflict(parameters ...Param) Error

NewConflict returns new error instance of default conflict type.

func NewError

func NewError(errorType ErrorType, parameters ...Param) Error

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

func NewFailedPrecondition

func NewFailedPrecondition(parameters ...Param) Error

NewFailedPrecondition returns new error instance of default failed precondition type.

func NewInternal

func NewInternal(parameters ...Param) Error

NewInternal returns new error instance of default internal type.

func NewInvalidArgument

func NewInvalidArgument(parameters ...Param) Error

NewInvalidArgument returns new error instance of default invalid argument type.

func NewNotFound

func NewNotFound(parameters ...Param) Error

NewNotFound returns new error instance of default not found type.

func NewPermissionDenied

func NewPermissionDenied(parameters ...Param) Error

NewPermissionDenied returns new error instance of default permission denied type.

func NewRequestEntityTooLarge

func NewRequestEntityTooLarge(parameters ...Param) Error

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

func NewTimeout

func NewTimeout(parameters ...Param) Error

NewTimeout returns new error instance of default timeout type.

func UnpackError

func UnpackError(se SerializableError) (e Error, err error)

UnpackError recreates an error instance from the given serializable error.

type ErrorCode

type ErrorCode int16

ErrorCode is an enum describing error category.

Each error code has associated HTTP status codes.

const (

	// PermissionDenied has status code 403 Forbidden.
	PermissionDenied ErrorCode
	// 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 Param

type Param interface {
	// contains filtered or unexported methods
}

Param represents error parameter.

func SafeParam

func SafeParam(key string, val interface{}) Param

SafeParam creates safe error parameter with the given key and value.

func UnsafeParam

func UnsafeParam(key string, val interface{}) Param

UnsafeParam creates unsafe error parameter with the given key and value.

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 marshalling & unmarshalling of concrete types implementing an Error interface.

This type does not marshall & unmarshall 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
  }
}

func ErrorFromResponse

func ErrorFromResponse(response *http.Response) (SerializableError, error)

ErrorFromResponse extract serializable error from the given response.

TODO This function is subject to change.

Jump to

Keyboard shortcuts

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