util

package module
v0.0.0-...-a8de713 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2018 License: Apache-2.0 Imports: 10 Imported by: 1

README

golang_util

Documentation

Overview

it is an example to show the best practices for errors

Index

Constants

This section is empty.

Variables

View Source
var (
	Err = &Error{Code: http.StatusInternalServerError, Err: "Error"}

	// https://golang.org/pkg/net/http/#pkg-constants
	ErrBadRequest                    = Err.WithCode(http.StatusBadRequest)
	ErrUnauthorized                  = Err.WithCode(http.StatusUnauthorized)
	ErrPaymentRequired               = Err.WithCode(http.StatusPaymentRequired)
	ErrForbidden                     = Err.WithCode(http.StatusForbidden)
	ErrNotFound                      = Err.WithCode(http.StatusNotFound)
	ErrMethodNotAllowed              = Err.WithCode(http.StatusMethodNotAllowed)
	ErrNotAcceptable                 = Err.WithCode(http.StatusNotAcceptable)
	ErrProxyAuthRequired             = Err.WithCode(http.StatusProxyAuthRequired)
	ErrRequestTimeout                = Err.WithCode(http.StatusRequestTimeout)
	ErrConflict                      = Err.WithCode(http.StatusConflict)
	ErrGone                          = Err.WithCode(http.StatusGone)
	ErrLengthRequired                = Err.WithCode(http.StatusLengthRequired)
	ErrPreconditionFailed            = Err.WithCode(http.StatusPreconditionFailed)
	ErrRequestEntityTooLarge         = Err.WithCode(http.StatusRequestEntityTooLarge)
	ErrRequestURITooLong             = Err.WithCode(http.StatusRequestURITooLong)
	ErrUnsupportedMediaType          = Err.WithCode(http.StatusUnsupportedMediaType)
	ErrRequestedRangeNotSatisfiable  = Err.WithCode(http.StatusRequestedRangeNotSatisfiable)
	ErrExpectationFailed             = Err.WithCode(http.StatusExpectationFailed)
	ErrTeapot                        = Err.WithCode(http.StatusTeapot)
	ErrUnprocessableEntity           = Err.WithCode(http.StatusUnprocessableEntity)
	ErrLocked                        = Err.WithCode(http.StatusLocked)
	ErrFailedDependency              = Err.WithCode(http.StatusFailedDependency)
	ErrUpgradeRequired               = Err.WithCode(http.StatusUpgradeRequired)
	ErrPreconditionRequired          = Err.WithCode(http.StatusPreconditionRequired)
	ErrTooManyRequests               = Err.WithCode(http.StatusTooManyRequests)
	ErrRequestHeaderFieldsTooLarge   = Err.WithCode(http.StatusRequestHeaderFieldsTooLarge)
	ErrUnavailableForLegalReasons    = Err.WithCode(http.StatusUnavailableForLegalReasons)
	ErrInternalServerError           = Err.WithCode(http.StatusInternalServerError)
	ErrNotImplemented                = Err.WithCode(http.StatusNotImplemented)
	ErrBadGateway                    = Err.WithCode(http.StatusBadGateway)
	ErrServiceUnavailable            = Err.WithCode(http.StatusServiceUnavailable)
	ErrGatewayTimeout                = Err.WithCode(http.StatusGatewayTimeout)
	ErrHTTPVersionNotSupported       = Err.WithCode(http.StatusHTTPVersionNotSupported)
	ErrVariantAlsoNegotiates         = Err.WithCode(http.StatusVariantAlsoNegotiates)
	ErrInsufficientStorage           = Err.WithCode(http.StatusInsufficientStorage)
	ErrLoopDetected                  = Err.WithCode(http.StatusLoopDetected)
	ErrNotExtended                   = Err.WithCode(http.StatusNotExtended)
	ErrNetworkAuthenticationRequired = Err.WithCode(http.StatusNetworkAuthenticationRequired)
)

Predefined errors

Functions

func AssertTrue

func AssertTrue(b bool)

AssertTrue asserts that b is true. Otherwise, it would log fatal.

func AssertTruef

func AssertTruef(b bool, format string, args ...interface{})

AssertTruef is AssertTrue with extra info.

func Check

func Check(err error)

Check logs fatal if err != nil.

func Check2

func Check2(_ interface{}, err error)

Check2 acts as convenience wrapper around Check, using the 2nd argument as error.

func IsNil

func IsNil(val interface{}) bool

IsNil checks if a specified object is nil or not, without Failing.

func Wrap

func Wrap(err error) error

Wrap wraps errors from external lib.

func Wrapf

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

Wrapf is Wrap with extra info.

Types

type Error

type Error struct {
	Code  int         `json:"-"`
	Err   string      `json:"error"`
	Msg   string      `json:"message"`
	Data  interface{} `json:"data,omitempty"`
	Stack string      `json:"-"`
}

Error represents a numeric error with optional meta. It can be used in middleware as a return result.

func ErrorWithStack

func ErrorWithStack(val interface{}, skip ...int) *Error

ErrorWithStack create a error with stacktrace

func (*Error) Error

func (err *Error) Error() string

Error implemented HTTPError interface.

func (Error) From

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

From returns a copy of err with given error. It will try to merge the given error. If the given error is a *Error instance, it will be returned without copy.

err := gear.ErrBadRequest.From(errors.New("invalid email"))
err := gear.Err.From(someErr)

func (Error) GoString

func (err Error) GoString() string

GoString implemented fmt.GoStringer interface, returns a Go-syntax string.

func (*Error) Status

func (err *Error) Status() int

Status implemented HTTPError interface.

func (Error) String

func (err Error) String() string

String implemented fmt.Stringer interface.

func (Error) WithCode

func (err Error) WithCode(code int) *Error

WithCode returns a copy of err with given code.

BadRequestErr := gear.Err.WithCode(400)

func (Error) WithMsg

func (err Error) WithMsg(msgs ...string) *Error

WithMsg returns a copy of err with given new messages.

err := gear.Err.WithMsg() // just clone
err := gear.ErrBadRequest.WithMsg("invalid email") // 400 Bad Request error with message invalid email"

func (Error) WithMsgf

func (err Error) WithMsgf(format string, args ...interface{}) *Error

WithMsgf returns a copy of err with given message in the manner of fmt.Printf.

err := gear.ErrBadRequest.WithMsgf(`invalid email: "%s"`, email)

func (Error) WithStack

func (err Error) WithStack(skip ...int) *Error

WithStack returns a copy of err with error stack.

err := gear.Err.WithMsg("some error").WithStack()

type HTTPError

type HTTPError interface {
	// Error returns error's message.
	Error() string
	// Status returns error's http status code.
	Status() int
}

func ParseError

func ParseError(e error, code ...int) HTTPError

ParseError parse a error, textproto.Error or HTTPError to HTTPError

type WaitGroupWrapper

type WaitGroupWrapper struct {
	sync.WaitGroup
}

func (*WaitGroupWrapper) Wrap

func (w *WaitGroupWrapper) Wrap(cb func())

Jump to

Keyboard shortcuts

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