merror

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2020 License: AGPL-3.0 Imports: 6 Imported by: 0

README

Misakey Error Package

merror 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

TO COPY - TO ADAPT merror 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"       // minumum/maximum value/lenght...
	DVRequired     string = "required"      // missing in request...
	DVExpired      string = "expired"       // expired duration...
	DVForbidden    string = "forbidden"     // forbidden to update...
	DVInternal     string = "internal"      // internal error occured
	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
)

Variables

View Source
var (
	BadRequestError            = errors.New("bad request")
	UnauthorizedError          = errors.New("not authorized")
	ForbiddenError             = errors.New("forbidden")
	NotFoundError              = errors.New("not found")
	MethodNotAllowedError      = errors.New("method not allowed")
	ConflictError              = errors.New("conflict")
	GoneError                  = errors.New("gone")
	RequestEntityTooLargeError = errors.New("request entity too large")
	UnprocessableEntityError   = errors.New("unprocessable entity")
	ClientClosedRequestError   = errors.New("client closed request")
	BadGatewayError            = errors.New("bad gateway")
	ServiceUnavailableError    = errors.New("service unavailable")
	InternalError              = errors.New("internal server")
)

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

View Source
var (
	// 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 merror.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 HasCode

func HasCode(err error, code Code) bool

HasCode transforms input error into merror and checks if code are matching

func ToHTTPCode

func ToHTTPCode(err error) int

Conversion function from Misakey Error to HTTP and vice-versa. ToCode 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"              // 400 general bad request
	UnauthorizedCode          Code = "unauthorized"             // 401 a required valid token is missing/malformed/expired
	ForbiddenCode             Code = "forbidden"                // 403 accesses checks failed
	NotFoundCode              Code = "not_found"                // 404 the resource/route has been not found
	MethodNotAllowedCode      Code = "method_not_allowed"       // 405 the method is not supported at a resource
	ConflictCode              Code = "conflict"                 // 409 the action cannot be perform of the resource
	GoneCode                  Code = "gone"                     // 410 ressource does not exist any more
	RequestEntityTooLargeCode Code = "request_entity_too_large" // 413 the requested entity is too large
	UnprocessableEntityCode   Code = "unprocessable_entity"     // 422 the received entity is unprocessable
	ClientClosedRequestCode   Code = "client_closed_requiest"   // 499 client closed the request so context has been canceled
	InternalCode              Code = "internal"                 // 500 something internal to the service has failed
	BadGatewayCode            Code = "bad_gateway"              // 502 invalid response from the server
	ServiceUnavailableCode    Code = "service_unavailable"      // 503 there service unavailable to handle at this oment

	// no_code codes
	UnknownCode Code = "unknown_code" // 500 something internal to the service has failed
	NoCodeCode  Code = "no_code"      // xxx no specific code defined

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

func ToCode

func ToCode(err error) Code

ToCode takes a default error and return corresponding Code

func (Code) String added in v1.2.2

func (c Code) String() string

type Error

type Error struct {
	Co      Code              `json:"code"`
	Ori     Origin            `json:"origin"`
	Desc    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 added in v1.7.0

func BadGateway() Error

func BadRequest

func BadRequest() Error

func ClientClosedRequest

func ClientClosedRequest() Error

func Conflict

func Conflict() Error

func Forbidden

func Forbidden() Error

func Gone added in v1.13.0

func Gone() Error

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

func MethodNotAllowed

func MethodNotAllowed() Error

func NotFound

func NotFound() Error

func RequestEntityTooLarge

func RequestEntityTooLarge() Error

func ServiceUnavailable added in v1.4.0

func ServiceUnavailable() Error

func Transform

func Transform(err error) Error

Transform transforms err into merror

func TransformHTTPCode

func TransformHTTPCode(code int) Error

Transform returns merror corresponding to HTTP code

func Unauthorized

func Unauthorized() Error

func UnprocessableEntity

func UnprocessableEntity() Error

func (*Error) Cause

func (e *Error) Cause() error

Cause returns the raw error contained within Error

func (*Error) Clear

func (e *Error) Clear() bool

Clear returns a boolean establishing if Error state is clear or not

func (Error) Code

func (e Error) Code(c Code) Error

From set Origin attributes

func (Error) Describe

func (e Error) Describe(desc string) Error

Add desc to the Error (concat with existing one)

func (Error) Describef

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

Set desc to the Error using Sprintf (concat with existing one)

func (Error) Detail

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

Add a key/value detail to the error Details map

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 added in v1.10.0

func (e Error) Flush() Error

Flush all details of the Error

func (Error) FlushDesc

func (e Error) FlushDesc() Error

FlushDesc of the Error

func (Error) From

func (e Error) From(ori Origin) Error

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

func (Error) If

func (e Error) If(match error) Error

Freeze possible updates if the Error has not same cause as sent error. Unfreeze is possible using End or this same method.

func (Error) ResetDetails

func (e Error) ResetDetails() Error

Reset Detail values

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
	OriInternal Origin = "internal" // the error comes from internal logic
	OriQuery    Origin = "query"    // the error comes from query parameters
	OriPath     Origin = "path"     // the error comes from path parameters

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

Jump to

Keyboard shortcuts

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