ohno

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

package ohno is a error context addition utility package. It is built to work in tandem with enums that are generated by ohnogen. You can however use this package even without that as long as you know how this package's internals work although it is not recommended. This package is mainly used to enable context addition to errors along with some simple inspection capabilities. It also provides structured representation in json and yaml formats

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToJoinError

func ConvertToJoinError(err error) error

This method takes all the nested ohno errors and joins them to yield an OhNoJoinError. For example if there was a 3 level nested error all of type OhNoError then this function will convert it to an OhNoJoinError containing those 3 errors in a flat structure. For example

OhNoError{
	Message: "err1"
	Cause: OhNoError{
		Message: "err2"
		Cause: OhNoError{
			Message: "err3"
		}
	}
}

will be converted to

OhNoJoinError{
	Errors: []error{
		OhNoError{
			Message: "err1"
		},
		OhNoError{
			Message: "err2"
		},
		OhNoError{
			Message: "err3"
		},
	 }
}

If the err parameter is not of OhNoError type then the err is returned back as it is. If any of the nested errors are of type OhNoJoinError, implements the Unwrap() []error method or is not of type OhNoError then no further unwrapping is done the nested error is simply appended to the list and the unwrapping is halted.

func Join

func Join(errs ...error) error

Join combines multiple errors similar to errors.Join and returns a combined error as an OhNoJoinError

func New

func New(ohnoer ohnoer.OhNoer, message string, extra any, cause error, sourceInfoType sourceinfo.SourceInfoType, callDepth int, timeStamp time.Time, timestampLayout string) error

This method generates a new error of OhNoError type. sourceInfoType parameter can be one of the sourceinfo.SourceInfoType s. callDepth is required if you require the Function name in the source information to be captured, use sourceinfo.DefaultCallDepth if you want the source information for the line where you are calling this method from. If you do not require source information pass sourceInfoType as sourceinfo.NoSourceInfo extra, cause, timestamp are optional and will be omitted from printing and marshaling. timestampLayout can be one of the standard timestamp layouts in time package. Default is time.RFC3339Nano.

Typically you would not need to use this directly since this would be called internally from the OhNo() method in the code generated by ohnogen.

Types

type OhNoError

type OhNoError struct {
	// An ohnoer.OhNoer interface error field
	ErrorCode error
	// A custom message for this instance of the error
	Message string
	// Any additional data to add context to this error
	Extra any
	// The error which led to this error being generated
	Cause error
	// File, Line & possibly Function name where this error was generated
	SourceInfo *sourceinfo.SourceInformation
	// Time at which this error occurred
	Timestamp time.Time
	// Layout in which the timestamp needs to be printed refer https://pkg.go.dev/time#pkg-constants
	TimestampLayout string
}

OhNoError is a structure which holds an error interface which satisfies the ohnoer.OhNoer interface.

func (*OhNoError) Error

func (o *OhNoError) Error() string

This is the Error() method which satisfies the builtin error interface This prints the error in the format

timestamp file:line(function): [code]name: description, message, extra
	cause(same representation as above with one indent)...

func (*OhNoError) Is

func (o *OhNoError) Is(target error) bool

This is a method implementation for usage with errors.Is in order to check if any errors in the chain match the current one

func (*OhNoError) MarshalJSON

func (o *OhNoError) MarshalJSON() ([]byte, error)

A simple json marshaler implementation for satisfying encoding/json.Marshaler

func (*OhNoError) MarshalYAML

func (o *OhNoError) MarshalYAML() (interface{}, error)

A simple yaml marshaler implementation for satisfying yaml.Marshaler

func (*OhNoError) Unwrap

func (o *OhNoError) Unwrap() error

This is a method implementation for usage with errors.Unwrap in order to get the wrapped/nested error

type OhNoJoinError

type OhNoJoinError struct {
	Errors []error `json:"errors" yaml:"errors"`
}

This is structural representation of multiple errors in the same level. It is implemented as an array of errors

func (*OhNoJoinError) Error

func (oj *OhNoJoinError) Error() string

This is the Error() method which satisfies the builtin error interface This prints the errors in the format

timestamp file:line(function): [code]name: description, message, extra
timestamp file:line(function): [code]name: description, message, extra
...

func (*OhNoJoinError) Unwrap

func (oj *OhNoJoinError) Unwrap() []error

This method is an implementation to satisfy errors.Unwrap usage. It returns the underlying error array

Jump to

Keyboard shortcuts

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