errors

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2022 License: MIT Imports: 3 Imported by: 1

README

🔧 Errors

Go Doc License License License

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

Read me in English

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

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

package main

import (
	"fmt"

	"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.Printf("I got a test error")
	}

	// Also, we provide some basic errors for you:
	err = errors.NotFound(nil) // Classic enough! Ah :)
	err = errors.Timeout(nil)
	err = errors.NetworkError(nil)
	err = errors.DBError(nil)
}
👥 贡献者

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorFormat formats e and returns a string in Error().
	ErrorFormat = func(e *Error) string {
		return fmt.Sprintf("%s", e.err.Error())
	}

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

Functions

func DBError

func DBError(err error) error

DBError returns a db error.

func Is

func Is(err error, code int32) bool

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

func IsDBError

func IsDBError(err error) bool

IsDBError if err is db error.

func IsNetworkError

func IsNetworkError(err error) bool

IsNetworkError if err is network error.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound if err is not found.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout if err is timeout.

func NetworkError

func NetworkError(err error) error

NetworkError returns a network error.

func New

func New(text string) error

New returns a string error.

func NotFound

func NotFound(err error) error

NotFound returns a not found error.

func Timeout

func Timeout(err error) error

Timeout returns a 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 UnwrapDBError added in v0.0.2

func UnwrapDBError(err error) (error, bool)

UnwrapDBError if err is db error.

func UnwrapNetworkError added in v0.0.2

func UnwrapNetworkError(err error) (error, bool)

UnwrapNetworkError if err is network error.

func UnwrapNotFound added in v0.0.2

func UnwrapNotFound(err error) (error, bool)

UnwrapNotFound if err is not found.

func UnwrapTimeout added in v0.0.2

func UnwrapTimeout(err error) (error, bool)

UnwrapTimeout if err is timeout.

func Wrap

func Wrap(err error, code int32) error

Wrap wraps err with code

Types

type Error

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

Error wraps err with code.

func (*Error) Error

func (e *Error) Error() string

Error returns the msg of e.

func (*Error) Is

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

Unwrap 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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