spanerrors

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Spanreed error model definition and default span errors.

The Spanreed family strives to have a consistent set of errors (and error communication) conventions shared between all services and clients.

This module defines two main objects for handing errors:

• SpanErrorType defines an error type.

• SpanError is an instance of an error which contains a SpanErrorType.

Default SpanErrorType Variables

Several pointers to SpanErrorType definitions are included in this package.

Index

Constants

This section is empty.

Variables

View Source
var APIError = NewSpanErrorType(
	"APIError",
	1000,
	502,
)

Base Error. Used when generic error is returned by route handler.

View Source
var APILimitError = NewSpanErrorType(
	"APILimitError",
	1004,
	400,
)

Request Exceeds API limit.

List of default SpanError definitions.

View Source
var ErrorTypeCodeIndex = makeDefaultErrorCodeIndex()

ApiCode:*ErrorType indexing of default errors.

View Source
var InvalidMethodError = NewSpanErrorType(
	"InvalidMethodError",
	1001,
	405,
)

Route does not implement HTTP method (GET, POST, PUT, etc.)

View Source
var NothingToReturnError = NewSpanErrorType(
	"NothingToReturnError",
	1002,
	400,
)

No media to return.

View Source
var RequestValidationError = NewSpanErrorType(
	"RequestValidationError",
	1003,
	400,
)

Error Occurred when Reading / validating Request Data.

View Source
var ResponseValidationError = NewSpanErrorType(
	"ResponseValidationError",
	1005,
	400,
)

Error occurred when writing Response.

View Source
var ServerError = NewSpanErrorType(
	"ServerError",
	1006,
	-1,
)

Sent back when the server framework raises an error that SpanServer does not handle. This type SHOULD NOT be invoked by app logic.

Functions

This section is empty.

Types

type SpanError

type SpanError struct {
	// The type of error we are returning.
	*SpanErrorType

	// A message detailing what caused the error.
	Message string

	// An id for the error being returned.
	Id uuid.UUID

	// A string / any mapping of data related to the error.
	ErrorData map[string]interface{}
	// contains filtered or unexported fields
}

Used to return a specific error instance.

func ErrorFromHeaders

func ErrorFromHeaders(
	headers headerFetcher,
	dataEngine encoding.ContentEngine,
	errorTypeCodeIndex map[int]*SpanErrorType,
) (spanError *SpanError, hasError bool, err error)

ErrorFromHeaders generates error object from headers of HTTP response. If a spanError object can be made from the header data, a pointer to it is returned. If a spanError code is detected in the headers, but the header data is malformed and cannot be loaded, then hasError is returned as True, and a description of the parsing issue is returned in err.

If the headers do not contain an error and hasError will be False, spanError will be returned as a nil pointer, and err will specify that no error was found.

func (*SpanError) Error

func (spanError *SpanError) Error() string

Error string to conform to builtin error interface.

func (*SpanError) IsType

func (spanError *SpanError) IsType(errorType *SpanErrorType) bool

Returns true if the underlying type of this error is the same as errorType. Some errors may have multiple http codes possible, se we can't just compare ErrorType field equality directly.

func (*SpanError) LogMessage

func (spanError *SpanError) LogMessage() string

More verbose error message that includes a debug.Stack() and source error information. This is not part of the Error(), Message, or ErrorData by default since it may contain sensitive information that is not desirable to return to the client.

func (*SpanError) ToHeader

func (spanError *SpanError) ToHeader(
	setter headerSetter, dataEngine encoding.ContentEngine,
) error

Writes error to an object which implements a Set(key string, value string) method like http.Request or http.Response.

func (*SpanError) Unwrap

func (spanError *SpanError) Unwrap() error

Implements xerrors.Wrapper interface. Part of how errors are being considered for implementation in future GO versions with more traceback support.

type SpanErrorType

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

Used to define a type of error that a service can return. Think of to define a TYPE of error that CAN be returned by your ecosystem, but

Each SpanErrorType for a given ecosystem should have a unique Name and ApiCode.

Codes 1000-1999 are reserved for Spanreeds default error definitions.

Since types are declared as pointers, to protect against accidental mutation of the error type by other packages, the underlying fields of this struct are private and accessed through functions. Define new error types using NewSpanErrorType()

func NewSpanErrorType

func NewSpanErrorType(
	name string,
	apiCode int,
	httpCode int,
) *SpanErrorType

Returns a span error type definition. Each definition should only need to be declared once in a shared library for any given ecosystem, ensuring consistent error codes and names for the error type across all services / libraries of a given language.

func (*SpanErrorType) ApiCode

func (errorType *SpanErrorType) ApiCode() int

Unique number to identify the error type in the API ecosystem.

func (*SpanErrorType) Error

func (errorType *SpanErrorType) Error() string

Allows the error type definition itself to also be a valid error for things like testing error equality.

func (*SpanErrorType) HttpCode

func (errorType *SpanErrorType) HttpCode() int

HTTP code that should be returned when this error type is returned. Set to -1 if the http error is determined dynamically.

func (*SpanErrorType) Name

func (errorType *SpanErrorType) Name() string

Unique human-readable name of the error type for the API ecosystem.

func (*SpanErrorType) New

func (errorType *SpanErrorType) New(
	message string,
	errorData map[string]interface{},
	source error,
) *SpanError

Returns a new span error to be returned by the route handler or panicked.

func (*SpanErrorType) Panic

func (errorType *SpanErrorType) Panic(
	message string,
	errorData map[string]interface{},
	source error,
)

Creates a new error that is immediately passed to a panic. Expected to be recovered by the SpanError middleware. Allows for errors_api to be generated from anywhere inside the route handle without need to explicitly pass them up a chain of nested function returns.

func (*SpanErrorType) WithHttpCode

func (errorType *SpanErrorType) WithHttpCode(newHTTPCode int) *SpanErrorType

Returns a copy of the error type with the given http code replaced.

Jump to

Keyboard shortcuts

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