GitXplorerGitXplorer
c

httperr

public
54 stars
0 forks
1 issues

Commits

List of commits on branch main.
Verified
f2547beaccc35951a2c4f8ad8d6e74a004373be8

build: goreleaser config

ccaarlos0 committed 5 months ago
Verified
ac3656b5386cc95b80a18d95c247620b9745fc3b

feat: prepare for v2

ccaarlos0 committed 5 months ago
Verified
2f2467265d9193efaa1b3bd45433d4554534a0b7

feat!: error handler takes a request, too

ccaarlos0 committed 5 months ago
Verified
2a8062b197001c78bf1f93e6fa3dfaa1f8eec2ed

chore(deps): Bump golangci/golangci-lint-action from 4 to 6 (#70)

ddependabot[bot] committed 6 months ago
Verified
bc0a385377e64b8939f960dceeec33a5a10fc19d

chore(deps): Bump goreleaser/goreleaser-action from 5 to 6 (#71)

ddependabot[bot] committed 6 months ago
Verified
8f9e5e3003f9300db1d8feb86d61ba83a3942bf8

chore(deps): Bump golangci/golangci-lint-action from 3 to 4 (#68)

ddependabot[bot] committed 10 months 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.