Skip to content

Golang package to generate errors with http code embedded

License

Notifications You must be signed in to change notification settings

sheki/statuserr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Package statuserr provides a errors with a HTTP status. this is useful for writing HTTP APIs where you want to return a correct http status from any part of the codebase

Inspired by nodejs boom

##Usage

_, err := ioutil.ReadAll(r)
if err != nil {
  return statuserr.InternalServerError.Wrap(err)
}

other possible uses

if user != "stark" {
  return statuserr.NotImplemented.Msgf("service does not work for %s", user)
}

if one is lazy

if user.NoName() {
  return statuserr.Conflict
}

package has types for all http status codes greater than 400.

The error values implement interface

type StatusCode interface {
  Status() int
}

to get the status code do

if e, ok := err.(StatusCode); ok {
  http.Error(w, e.Status(), e.Error())
}

About

Golang package to generate errors with http code embedded

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages