An open API service indexing awesome lists of open source software.

https://github.com/sebastianwebber/berr

Better errors replaces the standard error message with a more detailed one.
https://github.com/sebastianwebber/berr

error-handling golang golang-error-handling golang-errors golang-library golang-package

Last synced: 9 days ago
JSON representation

Better errors replaces the standard error message with a more detailed one.

Awesome Lists containing this project

README

        

# `berr`: Better Errors

Better errors replaces the standard error message with a more detailed one.
This makes it easier to debug your code and understand what is going on.

Yes, message output is inspired by the rust's [`anyhow` crate](https://docs.rs/anyhow/latest/anyhow/). :D

## Installation

```shell
go get github.com/sebastianwebber/berr@latest
```

## Example

![example](./examples.gif)
> check [`./examples/main.go`](./examples/main.go) for details.

# berr

```go
import "github.com/sebastianwebber/berr"
```

## Index

- [Constants](<#constants>)
- [Variables](<#variables>)
- [func Errorf\(format string, a ...any\) error](<#Errorf>)
- [func Format\(err error\) string](<#Format>)
- [func Logger\(err error, args ...any\) \*log.Logger](<#Logger>)
- [func New\(text string\) error](<#New>)
- [func Reset\(\)](<#Reset>)
- [type Config](<#Config>)

## Constants

```go
const (
// MaxStackDepth is the maximum number of stack frames to retrieve
// when collecting a stack trace.
MaxStackDepth = 100
)
```

## Variables

```go
var (
// Options var is the default configuration used by this package
Options = Config{
PrintStack: false,
ShowCompleteStack: false,
}
)
```


## func [Errorf]()

```go
func Errorf(format string, a ...any) error
```

Errorf returns a Better Error with a formatted string


## func [Format]()

```go
func Format(err error) string
```

Format returns a pretty formatted error message. Heavily inspired by anyhow output: https://docs.rs/anyhow/latest/anyhow/


## func [Logger]()

```go
func Logger(err error, args ...any) *log.Logger
```

Logger returns a \*log.Logger with the error field set to the pretty error message


## func [New]()

```go
func New(text string) error
```

New returns a Better Error with a interface compatible with the errors.New\(\)


## func [Reset]()

```go
func Reset()
```

Reset sets the default options for the package


## type [Config]()

```go
type Config struct {
// PrintStack will print the stack trace of the error
// but ignores the functions related to the berr package
PrintStack bool

// ShowCompleteStack will print the complete stack trace
// including the functions related to the berr package
ShowCompleteStack bool
}
```

Generated by [gomarkdoc]()