errors

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MPL-2.0 Imports: 9 Imported by: 0

README

errors

Module for standardising error formats

  • Defines a standard format for errors
  • Defines a collection type for reporting multiple errors
  • Defines methods for converting database and json schema errors to a standard format
  • Defines methods to convert errors to an api response format

Documentation

Index

Constants

View Source
const (
	TitleBadRequest          = "Bad Request"
	TitleConflict            = "Conflict Error"
	TitleForbidden           = "Forbidden"
	TitleInternalServerError = "Internal Server Error"
	TitleMethodNotAllowed    = "Method Not Allowed"
	TitleNotFound            = "Not Found"
	TitleServiceUnavailable  = "Service Unavailable"
	TitleSystem              = "System Error"
	TitleUnauthorized        = "Unauthorized"
)

package constants - standard error responses

View Source
const (
	TitleDatabaseAccess   string = "Access Denied"
	TitleDatabaseConflict string = "Conflict Error"
	TitleDatabaseInvalid  string = "Invalid Request"
	TitleDatabaseNoData   string = "Data Not Found"
	TitleDatabaseQuery    string = "Query Error"
	TitleDatabaseService  string = "Service Error"

	MessageNoRows string = "Query returned no records"
	NilRecord     string = "Nil record supplied"
)

Database errors -

View Source
const (
	SchemaValidation = "Schema Validation Error"
)

package constants

Variables

DBErrorCodeMap - Database error to response code mapping

JSONErrorCodeMap - JSON error to response code mapping

StdErrorCodeMap - Standard error to response code mapping

Functions

func GetErrorTitle

func GetErrorTitle(err error) string

GetErrorTitle -

func InvalidSchema

func InvalidSchema(err error, context string) error

InvalidSchema -

func IsErrNoRows

func IsErrNoRows(err error) bool

IsErrNoRows -

Types

type Collection

type Collection []error

Collection - holds a collection of errors

func NewCollection

func NewCollection() Collection

NewCollection returns a new Error Collection variable.

func (*Collection) Add

func (c *Collection) Add(err error)

Add adds an Error or Collection to the Errors slice.

func (Collection) AsResponseError

func (c Collection) AsResponseError(titleCodeMap ErrorStatusCode) *ResponseError

AsResponseError - returns an error for an api response

func (Collection) Error

func (c Collection) Error() string

Error implements the error interface for Err.

func (Collection) IsFatal

func (c Collection) IsFatal() bool

IsFatal -

type Error

type Error struct {
	Title    string `json:"-"`
	Type     string `json:"type,omitempty"`
	Property string `json:"property,omitempty"`
	Value    string `json:"value,omitempty"`
	Context  string `json:"context,omitempty"`
	Detail   string `json:"detail"`
	// Fatal - Internal flag to indicate all processing should stop when other errors may continue
	Fatal bool `json:"-"` // default false
	// contains filtered or unexported fields
}

Error - Intended to be end user friendly error text messages, used in APIs etc

func ErrConstraintViolation

func ErrConstraintViolation(pqerr *pq.Error) Error

ErrConstraintViolation -

func ErrNoRows

func ErrNoRows(context string) Error

ErrNoRows -

func Forbidden

func Forbidden(context string) Error

Forbidden -

func InternalServerError

func InternalServerError(context string) Error

InternalServerError -

func MethodNotAllowed

func MethodNotAllowed(context, method string) Error

MethodNotAllowed -

func MethodNotAllowedReason

func MethodNotAllowedReason(context, method, detail string) Error

MethodNotAllowedReason -

func MissingContext

func MissingContext(context string) Error

MissingContext -

func NewConflictError

func NewConflictError(errContext, errProperty, errDetail string, errValue interface{}) Error

NewConflictError -

func NewDataConflictError

func NewDataConflictError(errContext, errProperty, errDetail string, errValue interface{}) Error

NewDataConflictError -

func NewDatabaseError

func NewDatabaseError(errTitle, context, message string) Error

NewDatabaseError -

func NewError

func NewError(errTitle, errType, errContext, errProperty, errDetail string, errValue interface{}) Error

NewError -

func NewFromDatabaseError

func NewFromDatabaseError(err error, defaultContext string) Error

NewFromDatabaseError - convert a pq.Error to a Error

func NewFromJSONError

func NewFromJSONError(err error, context string) Error

NewFromJSONError -

func NewFromJSONSchemaError

func NewFromJSONSchemaError(err gojsonschema.ResultError, context string) Error

NewFromJSONSchemaError -

func NewNoDataError

func NewNoDataError(errContext, errProperty, errDetail string, errValue interface{}) Error

NewNoDataError -

func NewSchemaError

func NewSchemaError(errType, errContext, errProperty, errDetail string, errValue interface{}) Error

NewSchemaError -

func NewSystemError

func NewSystemError(errContext, errProperty, errDetail string, errValue interface{}) Error

NewSystemError -

func NotFound

func NotFound(context string) Error

NotFound -

func ServiceUnavailableError

func ServiceUnavailableError(context string) Error

ServiceUnavailableError -

func Unauthorized

func Unauthorized(context string) Error

Unauthorized -

func UnauthorizedWithDetail

func UnauthorizedWithDetail(context, property, detail string) Error

UnauthorizedWithDetail -

func (Error) AsResponse

func (e Error) AsResponse(titleCodeMap ErrorStatusCode) *Response

AsResponse -

func (Error) AsResponseError

func (e Error) AsResponseError(titleCodeMap ErrorStatusCode) *ResponseError

AsResponseError -

func (Error) Error

func (e Error) Error() string

Error -

func (Error) IsFatal

func (e Error) IsFatal() bool

IsFatal -

func (Error) IsSystemError

func (e Error) IsSystemError() bool

IsSystemError -

func (*Error) SetFatal

func (e *Error) SetFatal()

SetFatal -

type ErrorConstants

type ErrorConstants map[string]string

ErrorConstants -

func (ErrorConstants) Add

func (ec ErrorConstants) Add(name, value string)

Add - add an error constant

func (ErrorConstants) Merge

func (ec ErrorConstants) Merge(constants map[string]string)

Merge - merge a set of error constants

type ErrorStatusCode

type ErrorStatusCode map[string]int

ErrorStatusCode - map an error constant to an error status code

func (ErrorStatusCode) Add

func (esc ErrorStatusCode) Add(name string, value int)

Add - add an error status code

func (ErrorStatusCode) Merge

func (esc ErrorStatusCode) Merge(constants map[string]int)

Merge - add a set of error status codes

type Response

type Response struct {
	Error *ResponseError `json:"error"`
}

Response -

type ResponseError

type ResponseError struct {
	Code   int     `json:"code"`
	Title  string  `json:"title"`
	Detail string  `json:"detail,omitempty"` // TODO: possibly remove - keeping for backward compatibility during transition
	Errors []Error `json:"errors,omitempty"`
}

ResponseError -

func (ResponseError) Error

func (re ResponseError) Error() string

Jump to

Keyboard shortcuts

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