Skip to content

ninja-software/e

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

E

This package is under review

Structured errors package, inspired mostly by Ben Johnson and Upspin with some modifications (extra meta data, JSON marshalling, type coercions, HTTP codes).

$ cd example
$ go run *.go
serving on :8080
visit:
http://localhost:8080?username=nii236
http://localhost:8080?username=nii237

Usage

Return your structured error:

return &e.Error{
        Op:      op,
        Code:    e.ECONFLICT,
        Message: "Username is already in use. Please choose a different username.",
        Meta:    map[string]string{"username": username, "filename": "storage.go"},
    }

Handle your structured root error:

if err != nil {
    fmt.Println(err)
    http.Error(w, e.ErrorMessage(err), e.HTTPCode(err))
}

Server result:

main.handlerExample: main.createUser: <conflict> [filename=storage.go username=nii236] Username is already in use. Please choose a different username.

API response:

Username is already in use. Please choose a different username.

Packages

No packages published

Languages