Skip to content

Commit

Permalink
Add layouts to error handlers
Browse files Browse the repository at this point in the history
- Change github.com/pkg/errors -> github.com/friendsofgo/errors
  • Loading branch information
aarondl committed May 16, 2020
1 parent 0cbb574 commit df8d094
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 31 deletions.
2 changes: 1 addition & 1 deletion PACKAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Thank you for your continued efforts.
* [github.com/mitchellh/mapstructure](https://github.com/mitchellh/mapstructure)
* [github.com/pelletier/go-buffruneio](https://github.com/pelletier/go-buffruneio)
* [github.com/pelletier/go-toml](https://github.com/pelletier/go-toml)
* [github.com/pkg/errors](https://github.com/pkg/errors)
* [github.com/friendsofgo/errors](https://github.com/friendsofgo/errors)
* [github.com/pmezard/go-difflib/difflib](https://github.com/pmezard/go-difflib/difflib)
* [github.com/go-chi/chi](https://github.com/go-chi/chi)
* [github.com/go-chi/chi/middleware](https://github.com/go-chi/chi/middleware)
Expand Down
2 changes: 1 addition & 1 deletion abcconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"time"

"github.com/friendsofgo/errors"
"github.com/kat-co/vala"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
Expand Down
2 changes: 1 addition & 1 deletion abcdatabase/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/volatiletech/abcweb/v5/abcconfig"
"github.com/volatiletech/mig"
)
Expand Down
43 changes: 30 additions & 13 deletions abcmiddleware/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var (
type ErrorContainer struct {
// The error that will be returned by the controller
Err error
// Optional override layout string
ErrLayout string
// The template file top render (e.g "errors/500")
Template string
// HTTP status code to respond with
Expand All @@ -32,24 +34,26 @@ type ErrorContainer struct {

// ErrorManager helps manage errors at the application level
type ErrorManager struct {
render abcrender.Renderer
errors []ErrorContainer
render abcrender.Renderer
errors []ErrorContainer
errLayout string
}

// NewErrorManager creates an error manager that can be used to
// create an error handler to wrap your controllers with
func NewErrorManager(render abcrender.Renderer) *ErrorManager {
func NewErrorManager(render abcrender.Renderer, errorLayout string) *ErrorManager {
return &ErrorManager{
render: render,
errors: []ErrorContainer{},
render: render,
errors: []ErrorContainer{},
errLayout: errorLayout,
}
}

// NewError creates a new ErrorContainer that can be added to an errorManager.
// If you provide a handler here (instead of nil) then the Errors middleware
// will use your handler opposed to taking the default route of logging
// and rendering. You must handle logging and rendering yourself.
func NewError(err error, code int, template string, handler ErrorHandler) ErrorContainer {
func NewError(err error, code int, errorLayout, template string, handler ErrorHandler) ErrorContainer {
if err == nil {
panic("cannot supply nil error")
}
Expand All @@ -60,10 +64,11 @@ func NewError(err error, code int, template string, handler ErrorHandler) ErrorC
}

return ErrorContainer{
Err: err,
Code: code,
Template: template,
Handler: handler,
Err: err,
Code: code,
ErrLayout: errorLayout,
Template: template,
Handler: handler,
}
}

Expand Down Expand Up @@ -100,12 +105,13 @@ func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc {
}

var container ErrorContainer
var layout string
var template string
var code int

found := false
for _, e := range m.errors {
if e.Err == err {
if errors.Is(err, e.Err) {
container = e
found = true
break
Expand All @@ -114,6 +120,7 @@ func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc {

if !found { // no error containers/handlers found, default path
code = http.StatusInternalServerError
layout = m.errLayout
template = "errors/500"
} else if container.Handler != nil { // container and handler are set
err := container.Handler(w, r, container, m.render)
Expand All @@ -125,6 +132,7 @@ func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc {
return
} else { // container is set and handler is nil
code = container.Code
layout = container.ErrLayout
template = container.Template
}

Expand All @@ -146,10 +154,19 @@ func (m *ErrorManager) Errors(ctrl AppHandler) http.HandlerFunc {
case http.StatusInternalServerError:
log.Error("request error", fields...)
requestID := chimiddleware.GetReqID(r.Context())
m.render.HTML(w, code, template, requestID)

if len(layout) != 0 {
m.render.HTMLWithLayout(w, code, template, requestID, layout)
} else {
m.render.HTML(w, code, template, requestID)
}
default: // warn does not log stacktrace in prod, but error and above does
log.Warn("request failed", fields...)
m.render.HTML(w, code, template, nil)
if len(layout) != 0 {
m.render.HTMLWithLayout(w, code, template, nil, layout)
} else {
m.render.HTML(w, code, template, nil)
}
}
}
}
2 changes: 1 addition & 1 deletion abcmiddleware/zap_response_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"
"net/http"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
)

// zapResponseWriter is a wrapper that includes that http status and size for logging
Expand Down
2 changes: 1 addition & 1 deletion abcrender/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/unrolled/render"
)

Expand Down
2 changes: 1 addition & 1 deletion abcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os/signal"
"strings"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/volatiletech/abcweb/v5/abcconfig"
"go.uber.org/zap"
)
Expand Down
2 changes: 1 addition & 1 deletion abcsessions/cookie_overseer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/base64"
"net/http"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
)

// CookieOverseer oversees cookie operations that are encrypted and verified
Expand Down
2 changes: 1 addition & 1 deletion abcsessions/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/djherbis/times"
"github.com/pkg/errors"
"github.com/friendsofgo/errors"
)

// DiskStorer is a session storer implementation for saving sessions
Expand Down
2 changes: 1 addition & 1 deletion abcsessions/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package abcsessions
import (
"time"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
redis "gopkg.in/redis.v5"
)

Expand Down
2 changes: 1 addition & 1 deletion abcsessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"time"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
)

// session holds the session value and the flash messages key/value mapping
Expand Down
2 changes: 1 addition & 1 deletion abcsessions/storage_overseer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package abcsessions
import (
"net/http"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
uuid "github.com/satori/go.uuid"
)

Expand Down
2 changes: 1 addition & 1 deletion cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"text/template"

"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/BurntSushi/toml"
"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/spf13/afero"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/BurntSushi/toml v0.3.1
github.com/djherbis/times v1.2.0
github.com/fatih/color v1.9.0 // indirect
github.com/friendsofgo/errors v0.9.2
github.com/go-chi/chi v4.1.1+incompatible
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
github.com/lib/pq v1.5.1 // indirect
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/pierrre/archivefile v0.0.0-20170218184037-e2d100bc74f5
github.com/pkg/errors v0.9.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaI
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/friendsofgo/errors v0.9.1/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI=
github.com/friendsofgo/errors v0.9.2/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -108,8 +110,6 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pierrre/archivefile v0.0.0-20170218184037-e2d100bc74f5 h1:NSJ2ncDyrZ58zh67WXRBVoythaPHPTcF64mRIvrQgQk=
github.com/pierrre/archivefile v0.0.0-20170218184037-e2d100bc74f5/go.mod h1:VKhfi3lB86pwfB8XPHcj7Ysi1TczeIwwx3cOGcWXVXU=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand Down
2 changes: 1 addition & 1 deletion templates/app/setup.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/go-chi/chi"
chimiddleware "github.com/go-chi/chi/middleware"
"{{.ImportPath}}/controllers"
"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/spf13/pflag"
"github.com/volatiletech/abcweb/v5/abcconfig"
"github.com/volatiletech/abcweb/v5/abcmiddleware"
Expand Down
2 changes: 1 addition & 1 deletion templates/commands.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"

"{{.ImportPath}}/app"
"github.com/pkg/errors"
"github.com/friendsofgo/errors"
"github.com/spf13/cobra"
"github.com/volatiletech/abcweb/v5/abcconfig"
"github.com/volatiletech/abcweb/v5/abcdatabase"
Expand Down

0 comments on commit df8d094

Please sign in to comment.