derror

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	CodeNil                  = -1        // No error code specified.
	CodeOk                   = 0         // It is OK.
	CodeInternalError        = 50 + iota // An error occurred internally.
	CodeValidationFailed                 // Data validation failed.
	CodeDbOperationError                 // Database operation error.
	CodeInvalidParameter                 // The given parameter for current operation is invalid.
	CodeMissingParameter                 // Parameter for current operation is missing.
	CodeInvalidOperation                 // The function cannot be used like this.
	CodeInvalidConfiguration             // The configuration is invalid for current operation.
	CodeMissingConfiguration             // The configuration is missing for current operation.
	CodeNotImplemented                   // The operation is not implemented yet.
	CodeNotSupported                     // The operation is not supported yet.
	CodeOperationFailed                  // I tried, but I cannot give you what you want.
	CodeNotAuthorized                    // Not Authorized.
	CodeSecurityReason                   // Security Reason.
	CodeServerBusy                       // Server is busy, please try again later.
	CodeUnknown                          // Unknown error.
	CodeResourceNotExist                 // Resource does not exist.

	CodeBusinessValidationFailed = 300 + iota // Business validation failed.
)

Variables

This section is empty.

Functions

func Append

func Append(err error, errs ...error) error

func Cause

func Cause(err error) error

Cause returns the root cause error of <err>.

func Code

func Code(err error) int

Cause returns the error code of current error. It returns -1 if it has no error code or it does not implements interface Code.

func Current

func Current(err error) error

Current creates and returns the current level error. It returns nil if current level error is nil.

func Merge

func Merge(errs ...error) error

func New

func New(text string) error

func NewCode

func NewCode(code int, text string) error
Example
package main

import (
	"fmt"
	"github.com/osgochina/donkeygo/errors/derror"
)

func main() {
	err := derror.NewCode(10000, "My Error")
	fmt.Println(err.Error())
	fmt.Println(derror.Code(err))

}
Output:

My Error
10000

func NewCodeSkip

func NewCodeSkip(code, skip int, text string) error

NewCodeSkip creates and returns an error which has error code and is formatted from given text. The parameter <skip> specifies the stack callers skipped amount.

func NewCodeSkipf

func NewCodeSkipf(code, skip int, format string, args ...interface{}) error

NewCodeSkipf returns an error that has error code and formats as the given format and args. The parameter <skip> specifies the stack callers skipped amount.

func NewCodef

func NewCodef(code int, format string, args ...interface{}) error

NewCodef returns an error that has error code and formats as the given format and args.

Example
package main

import (
	"fmt"
	"github.com/osgochina/donkeygo/errors/derror"
)

func main() {
	err := derror.NewCodef(10000, "It's %s", "My Error")
	fmt.Println(err.Error())
	fmt.Println(derror.Code(err))

}
Output:

It's My Error
10000

func NewOption added in v0.1.9

func NewOption(option Option) error

NewOption creates and returns an error with Option. It is the senior usage for creating error, which is often used internally in framework.

func NewSkip

func NewSkip(skip int, text string) error

func NewSkipf

func NewSkipf(skip int, format string, args ...interface{}) error

func Newf

func Newf(format string, args ...interface{}) error

func Next

func Next(err error) error

Next returns the next level error. It returns nil if current level error or the next level error is nil.

func Stack

func Stack(err error) string

Stack returns the stack callers as string. It returns the error string directly if the <err> does not support stacks.

func Wrap

func Wrap(err error, text string) error

func WrapCode

func WrapCode(code int, err error, text string) error

WrapCode wraps error with code and text. It returns nil if given err is nil.

Example
package main

import (
	"errors"
	"fmt"
	"github.com/osgochina/donkeygo/errors/derror"
)

func main() {
	err1 := errors.New("permission denied")
	err2 := derror.WrapCode(10000, err1, "Custom Error")
	fmt.Println(err2.Error())
	fmt.Println(derror.Code(err2))

}
Output:

Custom Error: permission denied
10000

func WrapCodeSkip

func WrapCodeSkip(code, skip int, err error, text string) error

WrapCodeSkip wraps error with code and text. It returns nil if given err is nil. The parameter <skip> specifies the stack callers skipped amount.

func WrapCodeSkipf

func WrapCodeSkipf(code, skip int, err error, format string, args ...interface{}) error

WrapCodeSkipf wraps error with code and text that is formatted with given format and args. It returns nil if given err is nil. The parameter <skip> specifies the stack callers skipped amount.

func WrapCodef

func WrapCodef(code int, err error, format string, args ...interface{}) error

WrapCodef wraps error with code and format specifier. It returns nil if given <err> is nil.

Example
package main

import (
	"errors"
	"fmt"
	"github.com/osgochina/donkeygo/errors/derror"
)

func main() {
	err1 := errors.New("permission denied")
	err2 := derror.WrapCodef(10000, err1, "It's %s", "Custom Error")
	fmt.Println(err2.Error())
	fmt.Println(derror.Code(err2))

}
Output:

It's Custom Error: permission denied
10000

func WrapSkip

func WrapSkip(skip int, err error, text string) error

func WrapSkipf

func WrapSkipf(skip int, err error, format string, args ...interface{}) error

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Types

type Error

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

func (*Error) Cause

func (that *Error) Cause() error

func (*Error) Code

func (that *Error) Code() int

func (*Error) Current

func (that *Error) Current() error

func (*Error) Error

func (that *Error) Error() string

func (*Error) Format

func (that *Error) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%v, %s : Print all the error string; %-v, %-s : Print current level error string; %+s : Print full stack error list; %+v : Print the error string and full stack error list;

func (*Error) MarshalJSON

func (that *Error) MarshalJSON() ([]byte, error)

func (*Error) Next

func (that *Error) Next() error

func (*Error) Stack

func (that *Error) Stack() string

Stack returns the stack callers as string. It returns an empty string if the <err> does not support stacks.

type Option added in v0.1.9

type Option struct {
	Error error  // Wrapped error if any.
	Stack bool   // Whether recording stack information into error.
	Text  string // Error text, which is created by New* functions.
	Code  int    // Error code if necessary.
}

Option is option for creating error.

Jump to

Keyboard shortcuts

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