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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 (*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