merr

package
v0.0.0-...-afa1830 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: AGPL-3.0 Imports: 5 Imported by: 0

README

Misakey Error Package

merr package allows us to share same data format for API errors.

It allows us to use internal code to identify some specific behaviour to improve UX or our alerting system.

Misakey Errors are recognized by our error handlers to be treaten and formatted correctly.

Convention

Our error convention is described here

Error Introspection

We use the error cause principles to introspect a Misakey Error.

Documentation

Overview

Package merr is Misakey Error package, it help us to meet contracted error format defined in our convention it also allow us to define error code linked to our domain that would never change and will be used by consumers

Index

Constants

View Source
const (
	DVConflict     string = "conflict"      // unique...
	DVMalformed    string = "malformed"     // email format,  ip address format...
	DVInvalid      string = "invalid"       // minimum/maximum value/length...
	DVRequired     string = "required"      // missing in request...
	DVExpired      string = "expired"       // expired duration...
	DVForbidden    string = "forbidden"     // forbidden to update...
	DVInternal     string = "internal"      // internal error occurred
	DVLocked       string = "locked"        // cannot be updated
	DVNotFound     string = "not_found"     // correspondance has not been found
	DVNotSupported string = "not_supported" // not handled by the running implementation
	DVTimedOut     string = "timed_out"     // something... timed out
	DVUnauthorized string = "unauthorized"  // authorization is missing
	DVUnknown      string = "unknown"       // unknown detail code
	DVNoCode       string = "no_code"       // no specific code
)

detail constants

Variables

View Source
var (
	// ErrBadRequest ...
	ErrBadRequest = errors.New("bad request")
	// ErrUnauthorized ...
	ErrUnauthorized = errors.New("not authorized")
	// ErrForbidden ...
	ErrForbidden = errors.New("forbidden")
	// ErrNotFound ...
	ErrNotFound = errors.New("not found")
	// ErrMethodNotAllowed ...
	ErrMethodNotAllowed = errors.New("method not allowed")
	// ErrConflict ...
	ErrConflict = errors.New("conflict")
	// ErrGone ...
	ErrGone = errors.New("gone")
	// UnsupportedMediaType ...
	ErrUnsupportedMediaType = errors.New("unsupported media type")
	// ErrRequestEntityTooLarge ...
	ErrRequestEntityTooLarge = errors.New("request entity too large")
	// ErrUnprocessableEntity ...
	ErrUnprocessableEntity = errors.New("unprocessable entity")
	// ErrClientClosedRequest ...
	ErrClientClosedRequest = errors.New("client closed request")
	// ErrBadGateway ...
	ErrBadGateway = errors.New("bad gateway")
	// ErrServiceUnavailable ...
	ErrServiceUnavailable = errors.New("service unavailable")
	// ErrInternal ...
	ErrInternal = errors.New("internal server")
)

Declares some classic errors as variables to use it as a base for our error system

View Source
var (
	// StatusClientClosedRequest shall never be returned to consumer because of the context of the code
	StatusClientClosedRequest = 499 // http://lxr.nginx.org/source/src/http/ngx_http_request.h#0120
)

Functions

func AddCodeToURL

func AddCodeToURL(req string, code Code) (string, error)

AddCodeToURL takes a request and adds a merr.Code to it as a query params

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func IsAConflict

func IsAConflict(err error) bool

func IsAForbidden

func IsAForbidden(err error) bool

func IsANotFound

func IsANotFound(err error) bool

func IsAnInternal

func IsAnInternal(err error) bool

func IsUnauthorized

func IsUnauthorized(err error) bool

func ToHTTPCode

func ToHTTPCode(err error) int

ToHTTPCode returns HTTP code corresponding to Domain Misakey Code

Types

type Code

type Code string

Code describes an error code as a string used internally and by clearer consumer for better error identifications & reactions. It represents global codes and corresponds to http status as described [here](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). We use this link as general specifications for errors and not just for http request errors.

const (
	// classic codes
	BadRequestCode            Code = "bad_request"
	UnauthorizedCode          Code = "unauthorized"
	ForbiddenCode             Code = "forbidden"
	NotFoundCode              Code = "not_found"
	MethodNotAllowedCode      Code = "method_not_allowed"
	ConflictCode              Code = "conflict"
	GoneCode                  Code = "gone"
	RequestEntityTooLargeCode Code = "request_entity_too_large"
	UnsupportedMediaTypeCode  Code = "unsupported_media_type_code"
	UnprocessableEntityCode   Code = "unprocessable_entity"
	ClientClosedRequestCode   Code = "client_closed_requiest"
	InternalCode              Code = "internal"
	BadGatewayCode            Code = "bad_gateway"
	ServiceUnavailableCode    Code = "service_unavailable"

	// no_code codes
	UnknownCode Code = "unknown_code"
	NoCodeCode  Code = "no_code"

	// redirect codes
	AuthProcessRequiredCode Code = "auth_process_required"
	ConsentRequiredCode     Code = "consent_required"
	LoginRequiredCode       Code = "login_required"
	InvalidFlowCode         Code = "invalid_flow"
	MissingParameter        Code = "missing_parameter"
)

code constants

func CodeFromHTTPCode

func CodeFromHTTPCode(code int) Code

func ToCode

func ToCode(err error) Code

ToCode takes a default error and return corresponding Code

func (Code) String

func (c Code) String() string

String ...

type Error

type Error struct {
	Co          Code              `json:"code"`
	Origin      Origin            `json:"origin"`
	Description string            `json:"desc"`
	Details     map[string]string `json:"details"`
	// contains filtered or unexported fields
}

Error defines internal errors type we deal with in misakey domain layers

func BadGateway

func BadGateway() Error

BadGateway ...

func BadRequest

func BadRequest() Error

BadRequest ...

func ClientClosedRequest

func ClientClosedRequest() Error

ClientClosedRequest ...

func Conflict

func Conflict() Error

Conflict ...

func Forbidden

func Forbidden() Error

Forbidden ...

func From

func From(err error) Error

From returns a merr.Error using the received err

func FromHTTPCode

func FromHTTPCode(code int) Error

FromHTTPCode returns merr corresponding to HTTP code

func Gone

func Gone() Error

Gone ...

func HandleErr

func HandleErr(err error) (int, Error)

HandleErr tries to interpret an error as a Misakey Error returning HTTTP Code aside it. Its set default value if the error is not a Misakey Error

func Internal

func Internal() Error

Internal ...

func MethodNotAllowed

func MethodNotAllowed() Error

MethodNotAllowed ...

func NotFound

func NotFound() Error

NotFound ...

func RequestEntityTooLarge

func RequestEntityTooLarge() Error

RequestEntityTooLarge ...

func ServiceUnavailable

func ServiceUnavailable() Error

ServiceUnavailable ...

func Unauthorized

func Unauthorized() Error

Unauthorized ...

func UnprocessableEntity

func UnprocessableEntity() Error

UnprocessableEntity ...

func UnsupportedMediaType

func UnsupportedMediaType() Error

UnsupportedMediaType ...

func (Error) Add

func (e Error) Add(k string, v string) Error

Add a key/value detail to the error Details map

func (*Error) Cause

func (e *Error) Cause() error

Cause returns the raw error contained within Error

func (Error) Code

func (e Error) Code(c Code) Error

Code set code attribute

func (Error) Desc

func (e Error) Desc(desc string) Error

Desc the Error (concat with existing one)

func (Error) Descf

func (e Error) Descf(desc string, a ...interface{}) Error

Descfribe the Error using Sprintf (concat with existing one)

func (Error) End

func (e Error) End() Error

End triggers the end of a If method - unfreeze updates of the Error.

func (Error) Error

func (e Error) Error() string

Error returns the error as a printable string

func (Error) Flush

func (e Error) Flush() Error

Flush all details of the Error

func (Error) Ori

func (e Error) Ori(ori Origin) Error

Ori set the Origin attributes - it can be set only if current origin is NotDefined

type Origin

type Origin string

Origin is an information about where the error does come from. Once set, the Origin of an error should not be spoiled.

const (
	OriACR     Origin = "acr"     // the error comes from the authorization token acr
	OriBody    Origin = "body"    // the error comes from body parameter
	OriHeaders Origin = "headers" // the error comes from headers
	OriQuery   Origin = "query"   // the error comes from query parameters
	OriPath    Origin = "path"    // the error comes from path parameters
	OriCookies Origin = "cookies" // the error comes from cookies

	OriNotDefined Origin = "not_defined" // the error has no origin defined yet
)

origin constants

Jump to

Keyboard shortcuts

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