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

https://github.com/pchchv/json_errors

JSON errors for Go Rest APIs
https://github.com/pchchv/json_errors

api error error-handling error-messages errors go go-library go-module go-package golang golang-library golang-module golang-package json json-error rest rest-api rest-errors

Last synced: 5 months ago
JSON representation

JSON errors for Go Rest APIs

Awesome Lists containing this project

README

          

# `json_errors` provides a simple error handling for your REST applications. [![PkgGoDev](https://pkg.go.dev/badge/github.com/pchchv/json_errors)](https://pkg.go.dev/github.com/pchchv/json_errors) [![Go Report Card](https://goreportcard.com/badge/github.com/pchchv/json_errors)](https://goreportcard.com/report/github.com/pchchv/json_errors)

## Features:

* Compatible with built-in `error` interface
* Error wrapping
* JSON escaping

#

## Installation:

```bash
go get github.com/pchchv/json_errors
```

#

## Usage:

```go
package main

import (
"fmt"

"github.com/pchchv/json_errors"
)

func someFunc() error {
return json_errors.New("nope")
}

func main() {
if err := someFunc(); err != nil {
wrapped := json_errors.Wrap(err, "Message about error")
fmt.Println(wrapped.Error())
}
}
```

```bash
go run main.go
```

### Output:

```json
{"message":"Message about error","details":{"message":"nope"}}
```

### See [examples](examples) for more.

#