generic_error

package
v0.0.0-...-7edcfce Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorCodeUnknown                    string = "unknown_error"
	ErrorCodeInternalServerError        string = "internal_server_error"
	ErrorCodeFormat                     string = "invalid_format"
	ErrorCodeFieldValue                 string = "invalid_field_value"
	ErrorCodeValidation                 string = "validation_failed"
	ErrorCodeForbidden                  string = "forbidden"
	ErrorCodeNotFound                   string = "resource_not_found"
	ErrorCodeExternalServiceUnavailable string = "external_service_unavailable"
	ErrorCodeExternalServiceError       string = "external_service_error"
	ErrorCodeUnsupported                string = "operation_unsupported"
	ErrorCodeExpired                    string = "operation_expired"
	ErrorCodeRetryLater                 string = "retry_later"
	ErrorCodeOperationNotPermitted      string = "operation_not_permitteds"
	ErrorCodeResourceBusy               string = "resource_busyr"
)

Variables

View Source
var CommonErrorDescriptions = map[string]string{
	ErrorCodeUnknown:                    "Unknown error",
	ErrorCodeInternalServerError:        "Internal server error",
	ErrorCodeFormat:                     "Invalid format of request",
	ErrorCodeFieldValue:                 "Invalid value of request field",
	ErrorCodeValidation:                 "Validation failed",
	ErrorCodeForbidden:                  "Access denied",
	ErrorCodeNotFound:                   "Resource not found",
	ErrorCodeExternalServiceUnavailable: "External service is temporarily unavailable",
	ErrorCodeExternalServiceError:       "External service reported error",
	ErrorCodeUnsupported:                "Operation unsupported",
	ErrorCodeExpired:                    "Operation expired",
	ErrorCodeOperationNotPermitted:      "Operation not permitted",
	ErrorCodeRetryLater:                 "Service is temporarily unavailable, please retry later",
	ErrorCodeResourceBusy:               "Resource is busy, please retry later",
}

Functions

func Code

func Code(e error) string

Extract code from the error. If error is not of Error type then code is unknown_error.

func Details

func Details(e error) string

Extract details from the error.

func MapErrorData

func MapErrorData(e Error, obj interface{}) error

func Message

func Message(e error) string

Extract message from the error. If error is not of Error type then error as string is used.

func Original

func Original(e error) error

Extract original error from the error. If error is not of Error type then the argument is returned as is.

Types

type Error

type Error interface {
	error
	Code() string
	Message() string
	Details() string
	Original() error
	Data() interface{}

	SetMessage(msg string)
	SetDetails(details string)
	SetOriginal(err error)

	SetData(data interface{})
}

Generic error that can be forwarded from place of arising to place of user reporting.

type ErrorBase

type ErrorBase struct {
	ErrorHolder
}

func New

func New(code string, message ...string) *ErrorBase

Create new error from code and message.

func NewEmpty

func NewEmpty() *ErrorBase

func NewFromErr

func NewFromErr(err error, code ...string) *ErrorBase

Create new error from code and message taken from other "native error".

func NewFromMessage

func NewFromMessage(message string) *ErrorBase

Create new error from message.

func NewFromOriginal

func NewFromOriginal(code string, message string, original error) *ErrorBase

Create new error from code, message and some other "original error" with keeping native error.

func (*ErrorBase) Code

func (e *ErrorBase) Code() string

Get error code.

func (*ErrorBase) Data

func (e *ErrorBase) Data() interface{}

Get error data.

func (*ErrorBase) Details

func (e *ErrorBase) Details() string

Get error details.

func (*ErrorBase) Error

func (e *ErrorBase) Error() string

Convert error to string for error interface.

func (*ErrorBase) Message

func (e *ErrorBase) Message() string

Convert error message.

func (*ErrorBase) Original

func (e *ErrorBase) Original() error

Get original error.

func (*ErrorBase) SetData

func (e *ErrorBase) SetData(data interface{})

Set error data.

func (*ErrorBase) SetDetails

func (e *ErrorBase) SetDetails(details string)

Set error details.

func (*ErrorBase) SetMessage

func (e *ErrorBase) SetMessage(message string)

Set error message.

func (*ErrorBase) SetOriginal

func (e *ErrorBase) SetOriginal(err error)

Set original error.

type ErrorDefinitions

type ErrorDefinitions interface {
	AttachToErrorManager(manager ErrorManager)
}

type ErrorHolder

type ErrorHolder struct {
	Code     string      `json:"code" validate:"omitempty,alphanum_,max=64" vmessage:"Invalid error code"`
	Message  string      `json:"message"`
	Details  string      `json:"details,omitempty"`
	Original error       `json:"-"`
	Data     interface{} `json:"data,omitempty"`
}

type ErrorManager

type ErrorManager interface {
	ErrorDescription(code string, tr ...TranslationHandler) string
	ErrorProtocolCode(code string) int

	MakeGenericError(code string, tr ...TranslationHandler) Error

	AddErrorDescriptions(m map[string]string)
	AddErrorProtocolCodes(m map[string]int)

	SetDefaultErrorProtocolCode(code int)
	DefaultErrorProtocolCode() int
}

type ErrorManagerBase

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

func (*ErrorManagerBase) AddErrorDescriptions

func (e *ErrorManagerBase) AddErrorDescriptions(m map[string]string)

func (*ErrorManagerBase) AddErrorProtocolCodes

func (e *ErrorManagerBase) AddErrorProtocolCodes(m map[string]int)

func (*ErrorManagerBase) DefaultErrorProtocolCode

func (e *ErrorManagerBase) DefaultErrorProtocolCode() int

func (*ErrorManagerBase) ErrorDescription

func (e *ErrorManagerBase) ErrorDescription(code string, tr ...TranslationHandler) string

func (*ErrorManagerBase) ErrorProtocolCode

func (e *ErrorManagerBase) ErrorProtocolCode(code string) int

func (*ErrorManagerBase) Init

func (e *ErrorManagerBase) Init(defaultProtocolCode int)

func (*ErrorManagerBase) MakeGenericError

func (e *ErrorManagerBase) MakeGenericError(code string, tr ...TranslationHandler) Error

func (*ErrorManagerBase) SetDefaultErrorProtocolCode

func (e *ErrorManagerBase) SetDefaultErrorProtocolCode(code int)

type ErrorManagerBaseHttp

type ErrorManagerBaseHttp struct {
	ErrorManagerBase
}

func (*ErrorManagerBaseHttp) Init

func (e *ErrorManagerBaseHttp) Init()

type ErrorsExtender

type ErrorsExtender interface {
	ErrorDefinitions
	AppendErrorExtender(extender ErrorsExtender)
	Descriptions() map[string]string
	Codes() map[string]int
}

type ErrorsExtenderBase

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

func (*ErrorsExtenderBase) AddErrors

func (e *ErrorsExtenderBase) AddErrors(errorDescriptions map[string]string, errorProtocolCodes ...map[string]int)

func (*ErrorsExtenderBase) AppendErrorExtender

func (e *ErrorsExtenderBase) AppendErrorExtender(extender ErrorsExtender)

func (*ErrorsExtenderBase) AttachToErrorManager

func (e *ErrorsExtenderBase) AttachToErrorManager(manager ErrorManager)

func (*ErrorsExtenderBase) Codes

func (e *ErrorsExtenderBase) Codes() map[string]int

func (*ErrorsExtenderBase) Descriptions

func (e *ErrorsExtenderBase) Descriptions() map[string]string

func (*ErrorsExtenderBase) Init

func (e *ErrorsExtenderBase) Init(errorDescriptions map[string]string, errorProtocolCodes ...map[string]int)

type ErrorsExtenderStub

type ErrorsExtenderStub struct {
}

func (*ErrorsExtenderStub) AddErrors

func (e *ErrorsExtenderStub) AddErrors(errorDescriptions map[string]string, errorProtocolCodes ...map[string]int)

func (*ErrorsExtenderStub) AppendErrorExtender

func (e *ErrorsExtenderStub) AppendErrorExtender(extender ErrorsExtender)

func (*ErrorsExtenderStub) AttachToErrorManager

func (e *ErrorsExtenderStub) AttachToErrorManager(manager ErrorManager)

func (*ErrorsExtenderStub) Codes

func (e *ErrorsExtenderStub) Codes() map[string]int

func (*ErrorsExtenderStub) Descriptions

func (e *ErrorsExtenderStub) Descriptions() map[string]string

type TranslationHandler

type TranslationHandler = func(string) string

Jump to

Keyboard shortcuts

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