errors

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 6 Imported by: 1

README

🧯 Errors

Go Doc License Coverage Test

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

Read me in English

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

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

package main

import (
	"fmt"
	"io"

	"github.com/FishGoddess/errors"
)

func main() {
	// Use wrap function to create an *Error error which has code and message.
	// You can get code and message of err anytime.
	err := errors.Wrap(1000, "need login")
	fmt.Println(err)
	fmt.Println(err.Code(), err.Message())

	// Try these ways to get code and message!
	// You will get default code or message if err doesn't have a code or message.
	code := errors.Code(err, 6699)
	message := errors.Message(err, "default message")
	fmt.Println(code, message)

	code = errors.Code(io.EOF, 6699)
	message = errors.Message(io.EOF, "default message")
	fmt.Println(code, message)

	// Also, we provide some useful information carrier for you.
	// For examples, you can carry an error, caller information or some args.
	// Remember the count of args should be even and their keys should be a string.
	err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("user_id", 123, "timeout", "200ms")
	fmt.Println(err)
	fmt.Println(errors.CodeMessage(err, 6666, "default message"))

	// What's more, we provide some shortcuts for you.
	// All these ways are returning *Error and you are free to use all methods on *Error.
	berr := errors.BadRequest("id is wrong")
	ferr := errors.Forbidden("user isn't allowed")
	nerr := errors.NotFound("book not found")
	rerr := errors.RequireLogin("user requires login")
	fmt.Printf("%+v\n%+v\n%+v\n%+v\n", berr, ferr, nerr, rerr)

	isBadRequest := errors.MatchBadRequest(berr)
	isForbidden := errors.MatchForbidden(ferr)
	isNotFound := errors.MatchNotFound(nerr)
	isRequireLogin := errors.MatchRequireLogin(rerr)
	fmt.Printf("isBadRequest: %+v\nisForbidden: %+v\nisNotFound: %+v\nisRequireLogin: %+v\n", isBadRequest, isForbidden, isNotFound, isRequireLogin)
}

👥 贡献者

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v0.7.0

func As(err error, target any) bool

As is a shortcut of errors.As.

func Caller added in v0.7.0

func Caller() string

func Callers added in v0.7.0

func Callers() []string

func Code added in v0.3.0

func Code(err error, defaultCode int32) int32

Code unwraps error and gets code of it. It returns 0 if err is nil. It returns defaultCode if err doesn't have a code.

func CodeMessage added in v0.7.0

func CodeMessage(err error, defaultCode int32, defaultMessage string, args ...any) (int32, string)

CodeMessage unwraps error and gets code & message of it. It returns 0 & "" if err is nil. It returns defaultCode if err doesn't have a code. It returns defaultMessage if err doesn't have a message.

func Is

func Is(err, target error) bool

Is is a shortcut of errors.Is.

func Join added in v0.7.0

func Join(errs ...error) error

Join is a shortcut of errors.Join.

func Match added in v0.7.0

func Match(err error, code int32) bool

Match unwraps error and check if its code equals to code.

func MatchBadRequest added in v0.7.0

func MatchBadRequest(err error) bool

MatchBadRequest matches err with bad request code.

func MatchForbidden added in v0.7.0

func MatchForbidden(err error) bool

MatchForbidden matches err with forbidden code.

func MatchNotFound added in v0.7.0

func MatchNotFound(err error) bool

MatchNotFound matches err with not found code.

func MatchRequireLogin added in v0.7.0

func MatchRequireLogin(err error) bool

MatchRequireLogin matches err with require login code.

func Message added in v0.7.0

func Message(err error, defaultMessage string, args ...any) string

Message unwraps error and gets message of it. It returns "" if err is nil. It returns defaultMessage if err doesn't have a message.

func New

func New(text string) error

func Unwrap added in v0.0.2

func Unwrap(err error) error

Unwrap is a shortcut of errors.Unwrap.

Types

type Error

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

func BadRequest added in v0.0.3

func BadRequest(message string, args ...any) *Error

BadRequest returns *Error with bad request code.

func Forbidden added in v0.0.4

func Forbidden(message string, args ...any) *Error

Forbidden returns *Error with forbidden code.

func NotFound

func NotFound(message string, args ...any) *Error

NotFound returns *Error with not found code.

func RequireLogin added in v0.7.0

func RequireLogin(message string, args ...any) *Error

RequireLogin returns *Error with require login code.

func Wrap

func Wrap(code int32, message string, args ...any) *Error

Wrap returns *Error with code and message formatted with args.

func (*Error) Args added in v0.7.2

func (e *Error) Args() map[string]any

Args returns the args of *Error.

func (*Error) Code added in v0.7.0

func (e *Error) Code() int32

Code returns the code of *Error.

func (*Error) Error

func (e *Error) Error() string

Error returns *Error as string.

func (*Error) Message added in v0.7.0

func (e *Error) Message() string

Message returns the message of *Error.

func (*Error) String

func (e *Error) String() string

String returns *Error as string.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the cause of *Error.

func (*Error) With added in v0.7.0

func (e *Error) With(err error) *Error

With carries the cause err for *Error.

func (*Error) WithArgs added in v0.7.1

func (e *Error) WithArgs(args ...any) *Error

WithArgs carries some args for *Error.

func (*Error) WithCaller added in v0.7.0

func (e *Error) WithCaller() *Error

WithCaller carries the top caller for *Error.

func (*Error) WithCallers added in v0.7.0

func (e *Error) WithCallers() *Error

WithCallers carries all callers for *Error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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