GitXplorerGitXplorer
c

httperr

public
54 stars
0 forks
1 issues

Commits

List of commits on branch main.
Verified
1f76dc5b8fc163f7cda1a2900ab46e5e92c17f96

chore(deps): Bump goreleaser/goreleaser-action from 3 to 4 (#52)

ddependabot[bot] committed 2 years ago
Verified
127f1f723a9faaac0cf0e86f854c029461d765e7

build: update workflow

ccaarlos0 committed 2 years ago
Verified
b37f2c0f37e9c39d93bcded34a700a80c3f17243

docs: fix badge style

ccaarlos0 committed 2 years ago
Verified
4e8263e2a86fe6b3350344baf9690ab84cefc51d

docs: license fix

ccaarlos0 committed 2 years ago
Verified
2721e02f8d4b84009bd708edb4da0385879b0ee5

docs: fix badge

ccaarlos0 committed 2 years ago
Verified
467e3c4f846a4dfc7a59200cba416ebce38b948e

feat: implements errors.Is

ccaarlos0 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.