gerror

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MulanPSL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gerror provides rich functionalities to manipulate errors.

For maintainers, please very note that, this package is quite a basic package, which SHOULD NOT import extra packages except standard packages and internal packages, to avoid cycle imports.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the root cause error of `err`.

func Code

func Code(err error) gcode.Code

Code returns the error code of `current error`. It returns `CodeNil` if it has no error code neither it does not implement 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 Equal

func Equal(err, target error) bool

Equal reports whether current error `err` equals to error `target`. Please note that, in default comparison logic for `Error`, the errors are considered the same if both the `code` and `text` of them are the same.

Example
package main

import (
	"errors"
	"fmt"

	"gitee.com/iceinto/gfutil/v2/errors/gcode"
	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

func main() {
	err1 := errors.New("permission denied")
	err2 := gerror.New("permission denied")
	err3 := gerror.NewCode(gcode.CodeNotAuthorized, "permission denied")
	fmt.Println(gerror.Equal(err1, err2))
	fmt.Println(gerror.Equal(err2, err3))

}
Output:

true
false

func HasCode

func HasCode(err error, code gcode.Code) bool

HasCode checks and reports whether `err` has `code` in its chaining errors.

func HasError

func HasError(err, target error) bool

HasError performs as Is. This function is designed and implemented early before errors.Is of go stdlib. Deprecated: use Is instead.

func HasStack

func HasStack(err error) bool

HasStack checks and reports whether `err` implemented interface `gerror.IStack`.

func Is

func Is(err, target error) bool

Is reports whether current error `err` has error `target` in its chaining errors. There's similar function HasError which is designed and implemented early before errors.Is of go stdlib. It is now alias of errors.Is of go stdlib, to guarantee the same performance as go stdlib.

Example
package main

import (
	"errors"
	"fmt"

	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

func main() {
	err1 := errors.New("permission denied")
	err2 := gerror.Wrap(err1, "operation failed")
	fmt.Println(gerror.Is(err1, err1))
	fmt.Println(gerror.Is(err2, err2))
	fmt.Println(gerror.Is(err2, err1))
	fmt.Println(gerror.Is(err1, err2))

}
Output:

true
true
true
false

func New

func New(text string) error

New creates and returns an error which is formatted from given text.

func NewCode

func NewCode(code gcode.Code, text ...string) error

NewCode creates and returns an error that has error code and given text.

Example
package main

import (
	"fmt"

	"gitee.com/iceinto/gfutil/v2/errors/gcode"
	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

func main() {
	err := gerror.NewCode(gcode.New(10000, "", nil), "My Error")
	fmt.Println(err.Error())
	fmt.Println(gerror.Code(err))

}
Output:

My Error
10000

func NewCodeSkip

func NewCodeSkip(code gcode.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 gcode.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 gcode.Code, 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"

	"gitee.com/iceinto/gfutil/v2/errors/gcode"
	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

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

}
Output:

It's My Error
10000

func NewOption

func NewOption(option Option) error

NewOption creates and returns a custom error with Option. Deprecated: use NewWithOption instead.

func NewSkip

func NewSkip(skip int, text string) error

NewSkip creates and returns an error which is formatted from given text. The parameter `skip` specifies the stack callers skipped amount.

func NewSkipf

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

NewSkipf returns an error that formats as the given format and args. The parameter `skip` specifies the stack callers skipped amount.

func NewWithOption

func NewWithOption(option Option) error

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

func Newf

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

Newf returns an error that formats as the given format and args.

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 Unwrap

func Unwrap(err error) error

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

func Wrap

func Wrap(err error, text string) error

Wrap wraps error with text. It returns nil if given err is nil. Note that it does not lose the error code of wrapped error, as it inherits the error code from it.

func WrapCode

func WrapCode(code gcode.Code, 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"

	"gitee.com/iceinto/gfutil/v2/errors/gcode"
	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

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

}
Output:

Custom Error: permission denied
10000

func WrapCodeSkip

func WrapCodeSkip(code gcode.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 gcode.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 gcode.Code, 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"

	"gitee.com/iceinto/gfutil/v2/errors/gcode"
	"gitee.com/iceinto/gfutil/v2/errors/gerror"
)

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

}
Output:

It's Custom Error: permission denied
10000

func WrapSkip

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

WrapSkip wraps error with text. It returns nil if given err is nil. The parameter `skip` specifies the stack callers skipped amount. Note that it does not lose the error code of wrapped error, as it inherits the error code from it.

func WrapSkipf

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

WrapSkipf wraps error with 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. Note that it does not lose the error code of wrapped error, as it inherits the error code from it.

func Wrapf

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

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. It returns nil if given `err` is nil. Note that it does not lose the error code of wrapped error, as it inherits the error code from it.

Types

type Error

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

Error is custom error for additional features.

func (*Error) Cause

func (err *Error) Cause() error

Cause returns the root cause error.

func (*Error) Code

func (err *Error) Code() gcode.Code

Code returns the error code. It returns CodeNil if it has no error code.

func (*Error) Current

func (err *Error) Current() error

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

func (*Error) Equal

func (err *Error) Equal(target error) bool

Equal reports whether current error `err` equals to error `target`. Please note that, in default comparison for `Error`, the errors are considered the same if both the `code` and `text` of them are the same.

func (*Error) Error

func (err *Error) Error() string

Error implements the interface of Error, it returns all the error as string.

func (*Error) Format

func (err *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 (err Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the interface MarshalJSON for json.Marshal. Note that do not use pointer as its receiver here.

func (*Error) SetCode

func (err *Error) SetCode(code gcode.Code)

SetCode updates the internal code with given code.

func (*Error) Stack

func (err *Error) Stack() string

Stack returns the error stack information as string.

func (*Error) Unwrap

func (err *Error) Unwrap() error

Unwrap is alias of function `Next`. It is just for implements for stdlib errors.Unwrap from Go version 1.17.

type ICause

type ICause interface {
	Error() string
	Cause() error
}

ICause is the interface for Cause feature.

type ICode

type ICode interface {
	Error() string
	Code() gcode.Code
}

ICode is the interface for Code feature.

type ICurrent

type ICurrent interface {
	Error() string
	Current() error
}

ICurrent is the interface for Current feature.

type IEqual

type IEqual interface {
	Error() string
	Equal(target error) bool
}

IEqual is the interface for Equal feature.

type IStack

type IStack interface {
	Error() string
	Stack() string
}

IStack is the interface for Stack feature.

type IUnwrap

type IUnwrap interface {
	Error() string
	Unwrap() error
}

IUnwrap is the interface for Unwrap feature.

type Option

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  gcode.Code // 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