errors

package module
v0.0.1 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) (error, bool) {
	return errors.Is(err, codeTestError)
}

func main() {
	// We provide two graceful ways to handle error in Go: Wrap() and Is().
	// Wrap wraps error with a code and Is returns one error is with this code.
	// 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 e, ok := IsTestError(err); ok {
		fmt.Printf("I got a test error which says: \"%s\"\n", e.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) (error, bool)

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

func IsDBError

func IsDBError(err error) (error, bool)

IsDBError if err is db error.

func IsNetworkError

func IsNetworkError(err error) (error, bool)

IsNetworkError if err is network error.

func IsNotFound

func IsNotFound(err error) (error, bool)

IsNotFound if err is not found.

func IsTimeout

func IsTimeout(err error) (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 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

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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