https://github.com/aodin/errors
A robust errors type for Go
https://github.com/aodin/errors
Last synced: 2 months ago
JSON representation
A robust errors type for Go
- Host: GitHub
- URL: https://github.com/aodin/errors
- Owner: aodin
- License: mit
- Created: 2016-03-16T05:36:29.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-06T19:58:29.000Z (almost 9 years ago)
- Last Synced: 2025-01-21T08:29:56.633Z (4 months ago)
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Errors
======[](https://godoc.org/github.com/aodin/errors)
[](https://travis-ci.org/aodin/errors?branch=master)
[](https://goreportcard.com/report/aodin/errors)A robust errors type which implements the built-in `error` interface. It includes the following:
* Code, an `int` field for integer error codes such as HTTP status
* Meta, a `[]string` field for high-level errors
* Fields, a `map[string]string` field for named errorsIt supports both JSON and XML marshaling.
### Quickstart
```go
package mainimport (
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http""github.com/aodin/errors"
)func main() {
err := errors.New()
err.Code = http.StatusNotFound
err.AddMeta("Not Found")
err.SetField("ID", "Missing ID")// String
fmt.Printf("%s\n", err.Error())
// 404: Not Found; Missing ID (ID)// JSON
jsonBytes, jsonErr := json.Marshal(err)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Printf("%s\n", jsonBytes)
// {"code":404,"meta":["Not Found"],"fields":{"ID":"Missing ID"}}// XML
xmlBytes, xmlErr := xml.Marshal(err)
if xmlErr != nil {
log.Fatal(xmlErr)
}
fmt.Printf("%s\n", xmlBytes)
//404
Not FoundMissing ID
}
```Happy hacking!
aodin, 2016