Skip to content

Commit

Permalink
fix: propagate error in HTTP compat handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Nov 9, 2021
1 parent 4dcdf2e commit 5ed4d41
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
23 changes: 23 additions & 0 deletions example/cors/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"html/template"
"log"
Expand All @@ -19,17 +20,21 @@ func main() {
router.GET("/", indexHandler)

router.NewGroup("/api/v1",
bunrouter.WithMiddleware(errorMiddleware),
// Install CORS only for this group.
bunrouter.WithMiddleware(corsMiddleware),
bunrouter.WithGroup(func(g *bunrouter.Group) {
g.GET("/users/:id", userHandler)
g.GET("/error", failingHandler)
}))

router.NewGroup("/api/v2",
bunrouter.WithMiddleware(errorMiddleware),
// Install CORS only for this group.
bunrouter.WithMiddleware(newCorsMiddleware([]string{"http://localhost:9999"})),
bunrouter.WithGroup(func(g *bunrouter.Group) {
g.GET("/users/:id", userHandler)
g.GET("/error", failingHandler)
}))

log.Println("listening on http://localhost:9999")
Expand Down Expand Up @@ -73,6 +78,16 @@ func newCorsMiddleware(allowedOrigins []string) bunrouter.MiddlewareFunc {
}
}

func errorMiddleware(next bunrouter.HandlerFunc) bunrouter.HandlerFunc {
return func(w http.ResponseWriter, req bunrouter.Request) error {
err := next(w, req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return err
}
}

//------------------------------------------------------------------------------

func indexHandler(w http.ResponseWriter, req bunrouter.Request) error {
Expand All @@ -91,11 +106,19 @@ func userHandler(w http.ResponseWriter, req bunrouter.Request) error {
})
}

func failingHandler(w http.ResponseWriter, req bunrouter.Request) error {
return errors.New("just an error")
}

var indexTmpl = `
<html>
<h1>Welcome</h1>
<ul>
<li><a href="/api/v1/users/123">/api/v1/users/123</a></li>
<li><a href="/api/v1/error">/api/v1/error</a></li>
<li><a href="/api/v2/users/123">/api/v2/users/123</a></li>
<li><a href="/api/v2/error">/api/v2/error</a></li>
</ul>
</html>
`
Expand Down
18 changes: 15 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@ func HTTPHandler(handler http.Handler) HandlerFunc {

// HTTPHandlerFunc converts http.HandlerFunc to bunrouter.HandlerFunc.
func HTTPHandlerFunc(handler http.HandlerFunc) HandlerFunc {
return func(w http.ResponseWriter, req Request) error {
return func(w http.ResponseWriter, req Request) (err error) {
ctx := contextWithParams(req.Context(), req.params)

defer func() {
if v := recover(); v != nil {
var ok bool
err, ok = v.(error)
if !ok {
panic(v)
}
}
}()

handler.ServeHTTP(w, req.Request.WithContext(ctx))
return nil

return err
}
}

Expand All @@ -41,7 +53,7 @@ var _ http.Handler = (*HandlerFunc)(nil)

func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if err := h(w, NewRequest(req)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
panic(err)
}
}

Expand Down

0 comments on commit 5ed4d41

Please sign in to comment.