errors

package
v1.1.0-alpha-8....-b28ede8 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2018 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package errors defines a package used for error handling in 0-Disk.

Error type

The package provides an error type that has the original error as the cause, more context to the error can be provided by wrapping the error with an additional message. The underlying error (cause) will then be preserved and can be fetched (and then checked) with the `Cause` function.

ErrorSlice

ErrorSlice provides a type to enable capturing multiple error and handle them as a single error. It implements the error interface which will return a string of the added errors in a `;` seperated list and can be returned as an error with the `AsError` method, returning a nil if no error was added.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error if possible. If the error does not implement `Cause() error` it returns the full error

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.New("this is the cause of the error")
	err = errors.Wrap(err, "this is an annotation wrapped around the error")

	cause := errors.Cause(err)

	fmt.Print(cause.Error())
}
Output:
this is the cause of the error
Example (MultipleWraps)
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.New("cause of the error")
	err = errors.Wrap(err, "annotation 1")
	err = errors.Wrap(err, "annotation 2")
	err = errors.Wrap(err, "annotation 3")

	cause := errors.Cause(err)

	fmt.Println(err.Error())
	fmt.Println(cause.Error())
}
Output:
annotation 3: annotation 2: annotation 1: cause of the error
cause of the error

func New

func New(msg string) error

New returns an error with provided message

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.New("an error message")

	fmt.Print(err.Error())
}
Output:
an error message

func Newf

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

Newf formats an error according to a format specifier

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.Newf("this is %d error %s with %d arguments", 1, "message", 3)

	fmt.Print(err.Error())
}
Output:
this is 1 error message with 3 arguments

func Wrap

func Wrap(err error, msg string) error

Wrap returns an error that is annotated with provided message If error is nil, Wrap returns nil

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.New("this is the original error message")

	err = errors.Wrap(err, "this is an annotation wrapped around the error")

	fmt.Print(err.Error())
}
Output:
this is an annotation wrapped around the error: this is the original error message

func WrapError

func WrapError(cause error, annotation error) error

WrapError takes 2 errors and wraps them into 1. It sets the cause error as the cause and takes the the annotation error to annotate the cause error

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	errCause := errors.New("this is the cause error")
	errAnnot := errors.New("this is the annotation error")

	err := errors.WrapError(errCause, errAnnot)

	fmt.Print(err.Error())
}
Output:
this is the annotation error: this is the cause error

func Wrapf

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

Wrapf returns an error that is annotated with provided message If err is nil, Wrapf returns nil.

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	err := errors.New("this is the original error message")

	err = errors.Wrapf(err, "this is a %s annotation with %d args ", "formatted", 2)

	fmt.Print(err.Error())
}
Output:
this is a formatted annotation with 2 args : this is the original error message

Types

type ErrorSlice

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

ErrorSlice represents a slice of errors

Example
package main

import (
	"fmt"

	"github.com/zero-os/0-Disk/errors"
)

func main() {
	errs := errors.NewErrorSlice()

	fmt.Println(errs.Len())

	if errs.AsError() == nil {
		fmt.Println("no errors")
	} else {
		fmt.Println("there shouldn't be any errors in here yet")
	}

	errs.Add(errors.New("an error"))

	fmt.Println(errs.Len())
	fmt.Println(errs.Error())

	errs.Add(errors.New("another error"))

	fmt.Println(errs.Len())
	fmt.Println(errs.Error())
	fmt.Println(errs.AsError())

}
Output:
0
no errors
1
an error;
2
an error;another error;
an error;another error;

func NewErrorSlice

func NewErrorSlice() *ErrorSlice

NewErrorSlice returns an empty error slice

func (*ErrorSlice) Add

func (errs *ErrorSlice) Add(err error)

Add adds an error to error slice

func (*ErrorSlice) AsError

func (errs *ErrorSlice) AsError() error

AsError returns nil if the error slice is empty

func (ErrorSlice) Error

func (errs ErrorSlice) Error() string

Error implements the error interface

func (*ErrorSlice) Len

func (errs *ErrorSlice) Len() int

Len returns the current amount of errors in the error slice

Jump to

Keyboard shortcuts

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