errorbus

package
v1.4.5-alpha1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2019 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package errorbus is a default implementation of InterfaceErrorBus declared in "github.com/ottemo/commerce/env" package.

Error bus is a service which provides a convenient way to deliver error to system administrator. Error bus uses logger for an error message storing. Error message withing error bus provides with extra information which allows to fileter error messages. Following extra information used: "module name", "error level", "error code".

"module name" is a short-name for a package to have ability distinguish one package from another (no rules on it).

"error level" is one of "env" package constant. The more level of constant then it is less system related but more application code related. So, system administrators most likely needs to monitor error messages with error level below 5. Where as for customer applications should provide to user error messages above 5. Special meaning have error level 0 which means that error happened outside Ottemo system package.

env.ConstErrorLevelAPI        = 10
env.ConstErrorLevelModel      = 9
env.ConstErrorLevelActor      = 8
env.ConstErrorLevelHelper     = 7
env.ConstErrorLevelService    = 4
env.ConstErrorLevelServiceAct = 3
env.ConstErrorLevelCritical   = 2
env.ConstErrorLevelStartStop  = 1
env.ConstErrorLevelExternal   = 0

"error code" - unique string name identifying error message. It is highly recommended to use "uuidgen" or similar tool to get unique UUID identifier.

Once handled message will nto be handled second time it come to error bus as it flags as handled. In this way error message can safely travel between application routines, knowing that it will be logged only once (first occurrence).

As "module name" and "error level" in most cases not changing for a particular package, it is recommended to use package constants for them, like:

ConstErrorModule = "rts"
ConstErrorLevel  = env.ConstErrorLevelActor

There are two main usage approaches:

    Example 1: (Handling external errors)
    -------------------------------------
		if err := api.ValidateAdminRights(context); err != nil {
			return env.ErrorDispatch(err)
		}

    Example 2: (Generating error)
    -----------------------------
		return env.ErrorNew(ConstErrorModule, ConstErrorLevel, "d4962872-9ee7-4c86-94ef-7325cb6e1c9d", "ID is not valid")

Index

Constants

View Source
const (
	ConstCollectCallStack = true // flag to indicate that call stack information within error is required

	ConstConfigPathError            = "general.error"
	ConstConfigPathErrorHideLevel   = "general.error.hide_level"
	ConstConfigPathErrorHideMessage = "general.error.hide_message"

	ConstErrorModule = "env/errorbus"
	ConstErrorLevel  = env.ConstErrorLevelService
)

Package global constants

Variables

View Source
var (
	// ConstMsgRegexp is a regular expression used to parse error message
	ConstMsgRegexp = regexp.MustCompile(`^[\[{(]?\s*(?:(?:([a-zA-Z_\/-]+)?[:])?([0-9]+)?[-: ]([0-9a-fA-F-]+)?)?\s*[\]})]?\s*[:\->]*\s*(.+)`)
)

Package global variables

Functions

This section is empty.

Types

type DefaultErrorBus

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

DefaultErrorBus InterfaceErrorBus implementer class

func (*DefaultErrorBus) Dispatch

func (it *DefaultErrorBus) Dispatch(err error) error

Dispatch converts regular error to OttemoError and passes it through registered listeners

func (*DefaultErrorBus) GetErrorCode

func (it *DefaultErrorBus) GetErrorCode(err error) string

GetErrorCode returns errors code

func (*DefaultErrorBus) GetErrorLevel

func (it *DefaultErrorBus) GetErrorLevel(err error) int

GetErrorLevel returns error level

func (*DefaultErrorBus) GetErrorMessage

func (it *DefaultErrorBus) GetErrorMessage(err error) string

GetErrorMessage returns error message

func (*DefaultErrorBus) Modify

func (it *DefaultErrorBus) Modify(err error, module string, level int, code string) error

Modify works similar to Dispatch but allows to specify some additional information

func (*DefaultErrorBus) New

func (it *DefaultErrorBus) New(module string, level int, code string, message string) error

New creates and processes OttemoError

func (*DefaultErrorBus) Prepare

func (it *DefaultErrorBus) Prepare(module string, level int, code string, message string) error

Prepare creates OttemoError without processing

func (*DefaultErrorBus) Raw

func (it *DefaultErrorBus) Raw(message string) error

Raw creates and processes OttemoError encoded in given string

func (*DefaultErrorBus) RegisterListener

func (it *DefaultErrorBus) RegisterListener(listener env.FuncErrorListener)

RegisterListener registers error listener

type OttemoError

type OttemoError struct {
	Message string
	Module  string
	Code    string
	Level   int

	CallStack string
	// contains filtered or unexported fields
}

OttemoError @reconcile@ InterfaceOttemoError implementer class

func (*OttemoError) Error

func (it *OttemoError) Error() string

returns error message only

func (*OttemoError) ErrorCallStack

func (it *OttemoError) ErrorCallStack() string

ErrorCallStack returns error functions call stack for error

Note: ConstCollectStack constant should be set to true, otherwise, stack information will be blank

func (*OttemoError) ErrorCode

func (it *OttemoError) ErrorCode() string

ErrorCode returns error code (hexadecimal value) if specified, otherwise MD5 over error message

func (*OttemoError) ErrorFull

func (it *OttemoError) ErrorFull() string

ErrorFull returns error detail information about error

func (*OttemoError) ErrorLevel

func (it *OttemoError) ErrorLevel() int

ErrorLevel returns error level - if specified or 0

func (*OttemoError) ErrorMessage

func (it *OttemoError) ErrorMessage() string

ErrorMessage returns original error message

func (*OttemoError) IsHandled

func (it *OttemoError) IsHandled() bool

IsHandled returns handled flag

func (*OttemoError) IsLogged

func (it *OttemoError) IsLogged() bool

IsLogged returns logged flag

func (*OttemoError) MarkHandled

func (it *OttemoError) MarkHandled() bool

MarkHandled makes error as already processed (prevents from future processing)

func (*OttemoError) MarkLogged

func (it *OttemoError) MarkLogged() bool

MarkLogged makes error as already logged (prevents from future logging)

Jump to

Keyboard shortcuts

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