errors

package
v1.1.2-23 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 1 Imported by: 70

Documentation

Overview

Portable and localizable Exceptions classes. Each Exception, in addition to a description and stack trace has a unique string code, details array (which can be used for creating localized strings).

Way to use:

An existing exception class can be used. A child class that extends ApplicationException can we written. A exception can be wrapped around (into?) an existing application exception. Exceptions are serializable. The exception classes themselves are not serializable, but they can be converted to ErrorDescriptions, which are serializable in one language, transferred to the receiving side, and deserialized in another language. After deserialization, the initial exception class can be restored.

Additionally: when transferring an exception from one language to another, the exception type that is closest to the initial exception type is chosen from the exceptions available in the target language.

Index

Constants

View Source
const (
	Unknown          = "Unknown"
	Internal         = "Internal"
	Misconfiguration = "Misconfiguration"
	InvalidState     = "InvalidState"
	NoResponse       = "NoResponse"
	FailedInvocation = "FailedInvocation"
	FileError        = "FileError"
	BadRequest       = "BadRequest"
	Unauthorized     = "Unauthorized"
	NotFound         = "NotFound"
	Conflict         = "Conflict"
	Unsupported      = "Unsupported"
)

Defines standard error categories to application exceptions supported by PipServices toolkit.

BadRequest - Errors due to incorrectly specified invocation parameters. For example: missing or incorrect parameters.

Conflict - Errors raised by conflicts between object versions that were posted by the user and those that are stored on the server.

FailedInvocation - Errors caused by remote calls failed due to unidenfied reasons.

FileError - Errors in read/write local disk operations.

Internal - Internal errors caused by programming mistakes.

InvalidState - Errors caused by incorrect object state.. For example: business calls when the component is not ready.

Misconfiguration - Errors related to mistakes in user-defined configurations.

NoResponse - Errors caused by remote calls timeouted and not returning results. It allows to clearly separate communication related problems from other application errors.

NotFound - Errors caused by attempts to access missing objects.

Unauthorized - Access errors caused by missing user identity (authentication error) or incorrect security permissions (authorization error).

Unknown - Unknown or unexpected errors.

Unsupported - Errors caused by calls to unsupported or not yet implemented functionality.

Variables

View Source
var ErrorDescriptionFactory = &TErrorDescriptionFactory{}

Functions

This section is empty.

Types

type ApplicationError

type ApplicationError struct {
	Message       string                 `json:"message"`
	Category      string                 `json:"category"`
	Status        int                    `json:"status"`
	Code          string                 `json:"code"`
	Details       map[string]interface{} `json:"details"`
	CorrelationId string                 `json:"correlation_id"`
	StackTrace    string                 `json:"stack_trace"`
	Cause         string                 `json:"cause"`
}

Defines a base class to defive various application exceptions.

Most languages have own definition of base exception (error) types. However, this class is implemented symmetrically in all languages supported by PipServices toolkit. It allows to create portable implementations and support proper error propagation in microservices calls.

Error propagation means that when microservice implemented in one language calls microservice(s) implemented in a different language(s), errors are returned throught the entire call chain and restored in their original (or close) type.

Since number of potential exception types is endless, PipServices toolkit supports only 12 standard categories of exceptions defined in ErrorCategory. This ApplicationException class acts as a basis for all other 12 standard exception types.

Most exceptions have just free-form message that describes occured error. That may not be sufficient to create meaninful error descriptions. The ApplicationException class proposes an extended error definition that has more standard fields:

message: is a human-readable error description category: one of 12 standard error categories of errors status: numeric HTTP status code for REST invocations code: a unique error code, usually defined as "MY_ERROR_CODE" correlationId: a unique transaction id to trace execution through a call chain details: map with error parameters that can help to recreate meaningful error description in other languages stack_trace: a stack trace cause: original error that is wrapped by this exception ApplicationException class is not serializable. To pass errors through the wire it is converted into ErrorDescription object and restored on receiving end into identical exception type.

see ErrorCategory

see ErrorDescription

func NewBadRequestError

func NewBadRequestError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewConfigError

func NewConfigError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewConflictError

func NewConflictError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewConnectionError

func NewConnectionError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewError

func NewError(message string) *ApplicationError

Creates a new instance of application error and assigns its message. Parameters:

  • message string an error message

Return *ApplicationError generated new ApplicationError

func NewErrorFromDescription

func NewErrorFromDescription(description *ErrorDescription) *ApplicationError

Returns *ApplicationError

func NewFileError

func NewFileError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewInternalError

func NewInternalError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewInvalidStateError

func NewInvalidStateError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewInvocationError

func NewInvocationError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewNotFoundError

func NewNotFoundError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewUnauthorizedError

func NewUnauthorizedError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewUnknownError

func NewUnknownError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func NewUnsupportedError

func NewUnsupportedError(correlationId, code, message string) *ApplicationError

Creates an error instance and assigns its values. see ErrorCategory Parameters:

  • correlationId string a unique transaction id to trace execution through call chain.
  • code string a unique error code.
  • message string a human-readable description of the error.

Returns *ApplicationError

func WrapError

func WrapError(err error, message string) *ApplicationError

Wrap error by ApplicationError struct and sets message Parameters: - err error an error what neet to wrap - message string error message Return *ApplicationError

func (*ApplicationError) Error

func (e *ApplicationError) Error() string

Return string error message

func (*ApplicationError) WithCause

func (e *ApplicationError) WithCause(cause error) *ApplicationError

Add cause to ApplicationError Parameters: - cause error a cause error object Return *ApplicationError

func (*ApplicationError) WithCauseString

func (e *ApplicationError) WithCauseString(cause string) *ApplicationError

Add cause to ApplicationError Parameters: - cause string a cause string describe an error Return *ApplicationError

func (*ApplicationError) WithCode

func (e *ApplicationError) WithCode(code string) *ApplicationError

Add code to ApplicationError Parameters: - code string a error code Return *ApplicationError

func (*ApplicationError) WithCorrelationId

func (e *ApplicationError) WithCorrelationId(correlationId string) *ApplicationError

Add Correlation Id to ApplicationError Parameters: - correlationId string a correlation string Return *ApplicationError

func (*ApplicationError) WithDetails

func (e *ApplicationError) WithDetails(key string, value interface{}) *ApplicationError

Add error details to ApplicationError Parameters: - key string a detail key word - value interface{} an value of detail object Return *ApplicationError

func (*ApplicationError) WithStatus

func (e *ApplicationError) WithStatus(status int) *ApplicationError

Add status to ApplicationError Parameters: - status int a status code Return *ApplicationError

func (*ApplicationError) Wrap

func (e *ApplicationError) Wrap(err error) *ApplicationError

Wrap error by ApplicationError struct Parameters:

  • err error an error what neet to wrap

Return *ApplicationError

type ErrorDescription

type ErrorDescription struct {
	Type          string                 `json:"type"`
	Category      string                 `json:"category"`
	Status        int                    `json:"status"`
	Code          string                 `json:"code"`
	Message       string                 `json:"message"`
	Details       map[string]interface{} `json:"details"`
	CorrelationId string                 `json:"correlation_id"`
	Cause         string                 `json:"cause"`
	StackTrace    string                 `json:"stack_trace"`
}

Seializeable error description. It is use to pass information about errors between microservices implemented in different languages. On the receiving side ErrorDescription is used to recreate exception object close to its original type without missing additional details.

category - Standard error category cause - Original error wrapped by this exception code - A unique error code correlationId - A unique transaction id to trace execution throug call chain details - A map with additional details that can be used to restore error description in other languages message - A human-readable error description (usually written in English) stack_trace - Stack trace of the exception status - HTTP status code associated with this error type type - Data type of the original error

func NewErrorDescription

func NewErrorDescription(err interface{}) *ErrorDescription

Creates a serializable ErrorDescription from error object. Parameters:

  • err interface{} an error object

Returns *ErrorDescription a serializeable ErrorDescription object that describes the error.

type TApplicationErrorFactory

type TApplicationErrorFactory struct{}

Factory to recreate exceptions from ErrorDescription values passed through the wire. see ErrorDescription see ApplicationError

var ApplicationErrorFactory *TApplicationErrorFactory = &TApplicationErrorFactory{}

func (*TApplicationErrorFactory) Create

Returns *ApplicationError

type TErrorDescriptionFactory

type TErrorDescriptionFactory struct{}

Factory to create serializeable ErrorDescription from ApplicationException or from arbitrary errors. The ErrorDescriptions are used to pass errors through the wire between microservices implemented in different languages. They allow to restore exceptions on the receiving side close to the original type and preserve additional information. see ErrorDescription see ApplicationError

func (*TErrorDescriptionFactory) Create

func (c *TErrorDescriptionFactory) Create(err interface{}) *ErrorDescription

Creates a serializable ErrorDescription from error object. Parameters:

  • err error an error object

Returns *ErrorDescription a serializeable ErrorDescription object that describes the error.

Jump to

Keyboard shortcuts

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