errors

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 3 Imported by: 1

README

🧯 Errors

Go Doc License License Test

Errors 是一个用于优雅地处理 Go 中错误的库。

Read me in English

🙋‍ 功能特性
  • 优雅地处理 error,嗯,没了。。。

历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md

package main

import (
	"fmt"
	"io"

	"github.com/FishGoddess/errors"
)

const (
	codeTestError = 10000 // Your error's code
)

// TestError returns a test error.
func TestError(err error) error {
	return errors.Wrap(err, codeTestError)
}

// IsTestError if err is test error.
func IsTestError(err error) bool {
	return errors.Is(err, codeTestError)
}

func main() {
	// We provide three graceful ways to handle error in Go: Wrap() and Unwrap() and Is().
	// Wrap wraps error with a code and Unwrap returns one error with this code.
	// Is returns one error is with the same code or not.
	// As you can see, we define two functions above, and this is the basic way to use this lib.
	err := TestError(errors.New("something wrong"))
	if IsTestError(err) {
		fmt.Println("I got a test error")
	}

	// Also, we provide some basic errors for you:
	err = errors.BadRequest(nil)          // Classic enough! Ah :)
	err = errors.Forbidden(nil)           // Classic enough! Ah :)
	err = errors.NotFound(nil)            // Classic enough! Ah :)
	err = errors.RequestTimeout(nil)      // Classic enough! Ah :)
	err = errors.InternalServerError(nil) // Classic enough! Ah :)
	err = errors.DBError(nil)
	err = errors.PageTokenInvalid(nil)

	// Use WithMsg to carry a message.
	err = errors.Wrap(io.EOF, codeTestError, errors.WithMsg("test"))
	fmt.Println(err.Error())
	fmt.Println(errors.Msg(err))
	fmt.Println(errors.MsgOrDefault(io.EOF, "default error message"))
}

👥 贡献者

如果您觉得 Errors 缺少您需要的功能,那就 fork 到自己仓库随便玩。当然,也可以提 issue :)。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// FormatError formats e and returns a string in Error().
	FormatError = func(e *Error) string {
		return e.err.Error()
	}

	// FormatString formats e and returns a string in String().
	FormatString = func(e *Error) string {
		return fmt.Sprintf("%d (%s)", e.code, e.msg)
	}
)

Functions

func BadRequest added in v0.0.3

func BadRequest(err error, opts ...Option) error

BadRequest returns a bad request error.

func DBError

func DBError(err error, opts ...Option) error

DBError returns a db error.

func Forbidden added in v0.0.4

func Forbidden(err error, opts ...Option) error

Forbidden returns a forbidden error.

func InternalServerError added in v0.0.3

func InternalServerError(err error, opts ...Option) error

InternalServerError returns an internal server error.

func Is

func Is(err error, code int32) bool

Is returns if err is Error and its code == code.

func IsBadRequest added in v0.0.3

func IsBadRequest(err error) bool

IsBadRequest if err is bad request.

func IsDBError

func IsDBError(err error) bool

IsDBError if err is db error.

func IsForbidden added in v0.0.4

func IsForbidden(err error) bool

IsForbidden if err is forbidden.

func IsInternalServerError added in v0.0.3

func IsInternalServerError(err error) bool

IsInternalServerError if err is an internal server.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound if err is not found.

func IsPageTokenInvalid added in v0.5.0

func IsPageTokenInvalid(err error) bool

IsPageTokenInvalid if err is page token invalid.

func IsRequestTimeout added in v0.5.0

func IsRequestTimeout(err error) bool

IsRequestTimeout if err is request timeout.

func Msg added in v0.2.0

func Msg(err error) (string, bool)

Msg returns the msg of err and false if err doesn't have a msg.

func MsgOrDefault added in v0.4.0

func MsgOrDefault(err error, defaultMsg string) string

MsgOrDefault returns the msg of err or defaultMsg if err doesn't have a msg.

func New

func New(text string) error

New returns a string error.

func Newf added in v0.5.0

func Newf(text string, params ...interface{}) error

Newf returns a string error formatted with params.

func NotFound

func NotFound(err error, opts ...Option) error

NotFound returns a not found error.

func PageTokenInvalid added in v0.5.0

func PageTokenInvalid(err error, opts ...Option) error

PageTokenInvalid returns a page token invalid error.

func RequestTimeout added in v0.5.0

func RequestTimeout(err error, opts ...Option) error

RequestTimeout returns a request timeout error.

func Unwrap added in v0.0.2

func Unwrap(err error, code int32) (error, bool)

Unwrap returns if err is Error and its code == code, and the original error will be returned, too.

func UnwrapBadRequest added in v0.0.3

func UnwrapBadRequest(err error) (error, bool)

UnwrapBadRequest if err is bad request.

func UnwrapDBError added in v0.0.2

func UnwrapDBError(err error) (error, bool)

UnwrapDBError if err is db error.

func UnwrapForbidden added in v0.0.4

func UnwrapForbidden(err error) (error, bool)

UnwrapForbidden if err is forbidden.

func UnwrapInternalServerError added in v0.0.3

func UnwrapInternalServerError(err error) (error, bool)

UnwrapInternalServerError if err is an internal server.

func UnwrapNotFound added in v0.0.2

func UnwrapNotFound(err error) (error, bool)

UnwrapNotFound if err is not found.

func UnwrapPageTokenInvalid added in v0.5.0

func UnwrapPageTokenInvalid(err error) (error, bool)

UnwrapPageTokenInvalid if err is page token invalid.

func UnwrapRequestTimeout added in v0.5.0

func UnwrapRequestTimeout(err error) (error, bool)

UnwrapRequestTimeout if err is request timeout.

func Wrap

func Wrap(err error, code int32, opts ...Option) error

Wrap wraps err with code

Types

type Error

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

Error wraps err with some information.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

Is returns if e has the same type of target.

func (*Error) String

func (e *Error) String() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns err inside.

type Option added in v0.2.0

type Option func(e *Error)

func WithMsg added in v0.2.0

func WithMsg(msg string, params ...interface{}) Option

WithMsg sets msg to e.

func (Option) Apply added in v0.2.0

func (o Option) Apply(e *Error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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