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>
Click to show internal directories.
Click to hide internal directories.