errors

package
v10.6.7+incompatible Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2018 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Prefix the error prefix, applies to each error's message.
	Prefix = ""
)

Functions

func PrintAndReturnErrors

func PrintAndReturnErrors(err error, printer func(string, ...interface{})) error

PrintAndReturnErrors prints the "err" to the given "printer", printer will be called multiple times if the "err" is a StackError, where it contains more than one error.

Types

type Error

type Error struct {
	// ID returns the unique id of the error, it's needed
	// when we want to check if a specific error returned
	// but the `Error() string` value is not the same because the error may be dynamic
	// by a `Format` call.
	ID string `json:"id"`
	// The message of the error.
	Message string `json:"message"`
	// Apennded is true whenever it's a child error.
	Appended bool `json:"appended"`
	// Stack returns the list of the errors that are shown at `Error() string`.
	Stack []Error `json:"stack"` // filled on AppendX.
}

Error holds the error message, this message never really changes

Example
package main

import (
	"fmt"

	"github.com/kataras/iris/core/errors"
)

var errMessage = "User with mail: %s already exists"
var errUserAlreadyExists = errors.New(errMessage)
var userMail = "user1@mail.go"

func main() {

	fmt.Print(errUserAlreadyExists.Format(userMail).Append("Please change your mail addr"))

}
Output:

User with mail: user1@mail.go already exists
Please change your mail addr

func New

func New(errMsg string) Error

New creates and returns an Error with a pre-defined user output message all methods below that doesn't accept a pointer receiver because actually they are not changing the original message

func NewFromErr

func NewFromErr(err error) *Error

NewFromErr same as `New` but pointer for nil checks without the need of the `Return()` function.

func (Error) Append

func (e Error) Append(format string, a ...interface{}) Error

Append adds a message to the predefined error message and returns a new error it does NOT change the original error's message

func (Error) AppendErr

func (e Error) AppendErr(err error) Error

AppendErr adds an error's message to the predefined error message and returns a new error. it does NOT change the original error's message

func (Error) AppendInline

func (e Error) AppendInline(format string, a ...interface{}) Error

AppendInline appends an error to the stack. It doesn't try to append a new line if needed.

func (Error) Empty

func (e Error) Empty() bool

Empty returns true if the "e" Error has no message on its stack.

func (Error) Equal

func (e Error) Equal(e2 Error) bool

Equal returns true if "e" and "e2" are matched, by their IDs. It will always returns true if the "e2" is a children of "e" or the error messages are exactly the same, otherwise false.

func (Error) Error

func (e Error) Error() string

Error returns the message of the actual error implements the error

func (Error) Format

func (e Error) Format(a ...interface{}) Error

Format returns a formatted new error based on the arguments it does NOT change the original error's message

func (Error) HasStack

func (e Error) HasStack() bool

HasStack returns true if the Error instance is created using Append/AppendInline/AppendErr funcs.

func (Error) Ignore

func (e Error) Ignore(err error) error

Ignore will ignore the "err" and return nil.

func (Error) NotEmpty

func (e Error) NotEmpty() bool

NotEmpty returns true if the "e" Error has got a non-empty message on its stack.

func (Error) Panic

func (e Error) Panic()

Panic output the message and after panics.

func (Error) Panicf

func (e Error) Panicf(args ...interface{})

Panicf output the formatted message and after panics.

func (Error) String

func (e Error) String() string

String returns the error message

func (Error) With

func (e Error) With(err error) error

With does the same thing as Format but it receives an error type which if it's nil it returns a nil error.

type Reporter

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

Reporter is a helper structure which can stack errors and prints them to a printer of func(string).

func NewReporter

func NewReporter() *Reporter

NewReporter returns a new empty error reporter.

func (*Reporter) Add

func (r *Reporter) Add(format string, a ...interface{}) bool

Add adds a formatted message as an error to the error stack.

Returns true if this "err" is not nil and it's added to the reporter's stack.

func (*Reporter) AddErr

func (r *Reporter) AddErr(err error) bool

AddErr adds an error to the error stack. if "err" is a StackError then each of these errors will be printed as individual.

Returns true if this "err" is not nil and it's added to the reporter's stack.

func (*Reporter) Describe

func (r *Reporter) Describe(format string, err error)

Describe same as `Add` but if "err" is nil then it does nothing.

func (*Reporter) Error

func (r *Reporter) Error() string

Error implements the error, returns the full error string.

func (*Reporter) PrintStack

func (r *Reporter) PrintStack(printer func(string, ...interface{})) error

PrintStack prints all the errors to the given "printer". Returns itself in order to be used as printer and return the full error in the same time.

func (*Reporter) Return

func (r *Reporter) Return() error

Return returns nil if the error is empty, otherwise returns the full error.

func (*Reporter) Stack

func (r *Reporter) Stack() []Error

Stack returns the list of the errors in the stack.

type StackError

type StackError interface {
	Stack() []Error
	Error() string
}

StackError contains the Stack method.

Jump to

Keyboard shortcuts

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