...

Package spanerrors

import "github.com/illuscio-dev/spantools-go/spanerrors"
Overview
Index

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.

Variables

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

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

Request Exceeds API limit.

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

List of default SpanError definitions.

var ErrorList = [7]*SpanErrorType{
    APIError,
    InvalidMethodError,
    NothingToReturnError,
    RequestValidationError,
    APILimitError,
    ResponseValidationError,
    ServerError,
}

ApiCode:*ErrorType indexing of default errors.

var ErrorTypeCodeIndex = makeDefaultErrorCodeIndex()

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

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

No media to return.

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

Error Occurred when Reading / validating Request Data.

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

Error occurred when writing Response.

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

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

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

type SpanError

Used to return a specific error instance.

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
}

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

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()

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

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.