Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maru44/perr
Perr gives error persona
https://github.com/maru44/perr
Last synced: 5 days ago
JSON representation
Perr gives error persona
- Host: GitHub
- URL: https://github.com/maru44/perr
- Owner: maru44
- License: mit
- Created: 2021-09-27T13:13:37.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-13T11:18:04.000Z (almost 3 years ago)
- Last Synced: 2023-07-27T22:20:52.731Z (over 1 year ago)
- Language: Go
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# perr
Perr gives error persona and enrich error.
You can handle error properly, safely and easily with perr.## Purpose
I make this library for the following reasons.### main purpose
- I think errors must possess their persona.
- For client(which means client side and user using your service)
- For developerFor example, `dial tcp 127.0.0.1:3306: connect: connection refused` must not be shown for client. At the same time, `Internal Server Error` is not enough for developer.
### sub purpose
- `Map()` and `Json()` method make it easy to store and analyze error for you.
- `Is()` and `Level()` method make it easy to handle error.
- You can trace error with `Traces()` method.## How to use
I'll show you how to use simply.
### wrap error
```go:wrap.go
package mainimport (
"fmt"
"strconv"
"github.com/maru44/perr"
)func main() {
_, err := strconv.Atoi("a")p := perr.Wrap(err, perr.BadRequest)
p2 := perr.Wrap(p, perr.BadRequest)
p3 := perr.Wrap(p2, perr.InternalServerError)fmt.Println("developer:", p3)
fmt.Println("client:", p3.Output())
fmt.Println("\n", p3.Traces())
}/* output */
// developer: strconv.Atoi: parsing "a": invalid syntax
// client: Bad Request// /tmp/sandbox2199832404/prog.go:13 ===> main
// /tmp/sandbox2199832404/prog.go:14 ===> main
// /tmp/sandbox2199832404/prog.go:15 ===> main```
### new error
```go:new.go
package mainimport (
"fmt"
"github.com/maru44/perr"
)func main() {
p:= perr.New("Someone pour coffee", perr.IAmATeaPot)
fmt.Println("developer: ", p)
fmt.Println("client: ", p.Output())
p2 := perr.New("", perr.IAmATeaPot)
fmt.Println("\ndeveloper: ", p2)
}/* output */
// developer: Someone pour coffee
// client: I'm a teapot// developer: I'm a teapot
```
Here's more sample.
https://github.com/maru44/perr/tree/master/samples