errs

package
v0.0.0-...-14c8dc9 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2017 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Here are some error handling snippets.

Example (PkgErrors)
package main

import (
	"fmt"
	"github.com/pkg/errors"
)

func raiseAnError() error {
	return errors.Wrap(errors.New("All is not well"), "error raiser")
}

func main() {
	fmt.Println(raiseAnError())
}
Output:

error raiser: All is not well
Example (PkgErrorsDeeper)
package main

import (
	"fmt"
	"github.com/pkg/errors"
)

func raiseAnError() error {
	return errors.Wrap(errors.New("All is not well"), "error raiser")
}

func anotherLayer() error {
	return errors.Wrap(raiseAnError(), "anotherLayer()")
}

func main() {
	fmt.Println(anotherLayer())
}
Output:

anotherLayer(): error raiser: All is not well
Example (PkgErrorsDeeperWithCause)
package main

import (
	"fmt"
	"github.com/pkg/errors"
)

func raiseAnError() error {
	return errors.Wrap(errors.New("All is not well"), "error raiser")
}

func anotherLayer() error {
	return errors.Wrap(raiseAnError(), "anotherLayer()")
}

func main() {
	fmt.Println(errors.Cause(anotherLayer()))
}
Output:

All is not well
Example (PkgErrorsDepthWithoutError)
package main

import (
	"fmt"
	"github.com/pkg/errors"
)

func withoutAnError() error {
	return nil
}

func anotherLayerWithoutAnError() error {
	return errors.Wrap(withoutAnError(), "anotherLayerWithoutAnError")
}

func main() {
	fmt.Println(anotherLayerWithoutAnError())
}
Output:

<nil>

Jump to

Keyboard shortcuts

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