GitXplorerGitXplorer
c

httperr

public
54 stars
0 forks
1 issues

Commits

List of commits on branch main.
Verified
d275d89d204e80ccb67b65bec00edd6b247c7a78

chore(deps): Bump codecov/codecov-action from 3 to 4 (#67)

ddependabot[bot] committed a year ago
Verified
c42c44cf0e8d5f61ed489389f383a9edc8c45708

chore(deps): Bump actions/setup-go from 4 to 5 (#66)

ddependabot[bot] committed a year ago
Verified
32d9a1c5416ffd821321e706cf615d2a072acebb

chore(deps): Bump goreleaser/goreleaser-action from 4 to 5 (#65)

ddependabot[bot] committed a year ago
Verified
823a9adf4055bed0b46f173fcf8f755bbcf54196

fix: set Content-Type to application/json when responding with json (#64)

uutilyre committed a year ago
Verified
4f22c57105d882c44a9fd8775702e5476e025b4e

chore(deps): Bump actions/checkout from 3 to 4 (#63)

ddependabot[bot] committed a year ago
Verified
490eb9c6ec953a25bba4edbfb191fe13dbdabdcb

chore(deps): Bump actions/setup-go from 3 to 4 (#62)

ddependabot[bot] committed 2 years ago

README

The README file for this repository.

httperr

Build Status Coverage Status

I've been doing this in several different projects, I finally decided to convert it to a proper lib.

The idea is to add an error return to HTTP handler functions, so you can avoid writing if err != nil { http.Error(w, err); return } everywhere.

The basic usage looks like:

mux.Handle("/", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  err := doSomething()
  return err
}))

This will yield a 500 and return a JSON like {"error":"doSomething error"}.

The lib also provide a Wrap function, so you can decide which status code you want:

mux.Handle("/e", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  err := doSomething()
  return httperr.Wrap(err, http.StatusBadRequest)
}))

Or, you can throw errors with a status, e.g.:

mux.Handle("/e", httperr.NewF(func(w http.ResponseWriter, r *http.Request) error {
  if something {
  	return httperr.Errorf(http.StatusBadRequest, "something: %v", something)
  }
  return nil
}))

So, this is it! You can also check the examples folder for a "real" usage.