Skip to content

Commit

Permalink
added MinimalErrorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Mar 18, 2022
1 parent 8ed7fe7 commit 4c5057d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,30 @@ package nvelope
import (
"encoding"
"errors"
"net/http"

"github.com/muir/nject"
)

// MinimalErrorHandler provides a way to catch returned error values from
// the many functions that return them if MakeResponseEncoder is not used.
// http.ResponseWriter is used instead of a DeferredWriter. That means that
// MinimalErrorHandler cannot know if a response has already been made. The
// assumption is that if the returned error is nil, a respons has been made
// and if the returned error is not nil, then a response has not yet been
// made and the MinimalErrorHandler should make one. GetReturnCode is used
// to determine the return code.
var MinimalErrorHandler = nject.Provide("minimal-error-handler", minimalErrorHandler)

func minimalErrorHandler(inner func() error, w http.ResponseWriter) {
err := inner()
if err == nil {
return
}
w.WriteHeader(GetReturnCode(err))
_, _ = w.Write([]byte(err.Error()))
}

// ReturnCode associates an HTTP return code with a error.
// if err is nil, then nil is returned.
func ReturnCode(err error, code int) error {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/golang/gddo v0.0.0-20210115222349-20d68f94ee1f
github.com/gorilla/mux v1.8.0
github.com/muir/nape v0.2.1
github.com/muir/nchi v0.0.1
github.com/muir/nject v0.4.0
github.com/muir/reflectutils v0.4.0
github.com/pkg/errors v0.9.1
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gregjones/httpcache v0.0.0-20170920190843-316c5e0ff04e/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/hcl v0.0.0-20170914154624-68e816d1c783/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/inconshreveable/log15 v0.0.0-20170622235902-74a0988b5f80/go.mod h1:cOaXtrgN4ScfRrD9Bre7U1thNq5RtJ8ZoP4iXVGRj6o=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -32,10 +34,13 @@ github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992/go.mod h1:F
github.com/muir/nape v0.2.0/go.mod h1:VjvP6gR5PzQ3yyOPjyGlFOxDFucMVhBkaJGT6c0CvsU=
github.com/muir/nape v0.2.1 h1:Yce3vLOW4umOMwFzvqYHfIKa3Ltx0Y6qiGcE8yFPkJA=
github.com/muir/nape v0.2.1/go.mod h1:FE26YLNvUZ7DQJVW34+ypho9cGTn0NccvXjMaDo1nTw=
github.com/muir/nchi v0.0.1 h1:2rg72QxXywSih++Ft+M83fWXOAqR0Vl/RdjcguiJHLY=
github.com/muir/nchi v0.0.1/go.mod h1:v6gUg0F1wD7qpZcp6I2/5Y4IUA9qzlKgCqehdEuOPJg=
github.com/muir/nject v0.2.0/go.mod h1:vFJkDQciI0UURYI3cSZLyBhYxL3gn+4FvJBY9cZSsaA=
github.com/muir/nject v0.4.0 h1:g9IBW+6PHtz65GsKinJRweHXBQLaRUZgtbjIGnEACA0=
github.com/muir/nject v0.4.0/go.mod h1:le4jeI0HP94VGIAGbdB4gLMzWENM/uHh8Iq3/Qv8wak=
github.com/muir/nvelope v0.2.0/go.mod h1:raB9voDUnhMELiU2fZG7yHYdiRGKSh/FGtsjYWtdbpc=
github.com/muir/nvelope v0.3.0/go.mod h1:XWPSm68reA4rxkuC/keqHbeFT9QJBorF2ttjF7flzyM=
github.com/muir/reflectutils v0.4.0 h1:1VXbXaav7tR5BTYTHcGTdOAyxu3idCzM5FCtCpgm2u4=
github.com/muir/reflectutils v0.4.0/go.mod h1:hyMWDtoeNsc1FTq9qlXsbwDzbwp3A3M8zbRNKDzvSmc=
github.com/pelletier/go-toml v1.0.1-0.20170904195809-1d6b12b7cb29/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down

0 comments on commit 4c5057d

Please sign in to comment.