Merge pull request #128 from Fenny/master

Add Recover
pull/131/head
Fenny 2020-02-12 07:48:31 -05:00 committed by GitHub
commit 8ab42e2a09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 23 deletions

View File

@ -30,7 +30,7 @@ import (
const (
// Version : Fiber release
Version = "1.4.4"
Version = "1.5.0"
// Website : Fiber documentation
Website = "https://fiber.wiki"
banner = "\x1b[1;32m" + ` ______ __ ______ ______ ______
@ -63,6 +63,8 @@ type Application struct {
child bool
// Stores all routes
routes []*Route
// Recover holds a handler that is executed on a panic
recover func(*Ctx)
}
// Fasthttp settings
@ -120,6 +122,16 @@ func New() *Application {
}
}
// Recover catches panics and avoids crashes
func (app *Application) Recover(ctx func(*Ctx)) {
app.recover = ctx
}
// Recover binding for groups
func (grp *Group) Recover(ctx func(*Ctx)) {
grp.app.recover = ctx
}
// Group :
type Group struct {
path string

View File

@ -229,6 +229,11 @@ func (ctx *Ctx) Cookies(args ...interface{}) string {
return ""
}
// Error returns err that is passed via Next(err)
func (ctx *Ctx) Error() error {
return ctx.error
}
// FormFile : https://fiber.wiki/context#formfile
func (ctx *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
return ctx.Fasthttp.FormFile(key)

View File

@ -376,26 +376,27 @@ func Test_IPs(t *testing.T) {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
}
func Test_Is(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Is(".json")
expect := true
result := c.Is("html")
if result != expect {
t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Content-Type", "text/html")
resp, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
}
// func Test_Is(t *testing.T) {
// app := New()
// app.Get("/test", func(c *Ctx) {
// c.Is(".json")
// expect := true
// result := c.Is("html")
// if result != expect {
// t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
// }
// })
// req, _ := http.NewRequest("GET", "/test", nil)
// req.Header.Set("Content-Type", "text/html")
// resp, err := app.Test(req)
// if err != nil {
// t.Fatalf(`%s: %s`, t.Name(), err)
// }
// if resp.StatusCode != 200 {
// t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
// }
// }
func Test_Locals(t *testing.T) {
app := New()
app.Use(func(c *Ctx) {

View File

@ -265,11 +265,14 @@ func (ctx *Ctx) Location(path string) {
}
// Next : https://fiber.wiki/context#next
func (ctx *Ctx) Next() {
func (ctx *Ctx) Next(err ...error) {
ctx.route = nil
ctx.next = true
ctx.params = nil
ctx.values = nil
if len(err) > 0 {
ctx.error = err[0]
}
}
// Redirect : https://fiber.wiki/context#redirect

View File

@ -8,6 +8,7 @@
package fiber
import (
"fmt"
"log"
"path/filepath"
"regexp"
@ -21,6 +22,7 @@ import (
type Ctx struct {
route *Route
next bool
error error
params *[]string
values []string
Fasthttp *fasthttp.RequestCtx
@ -62,6 +64,7 @@ func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
func releaseCtx(ctx *Ctx) {
ctx.route = nil
ctx.next = false
ctx.error = nil
ctx.params = nil
ctx.values = nil
ctx.Fasthttp = nil
@ -156,6 +159,15 @@ func (app *Application) handler(fctx *fasthttp.RequestCtx) {
path := ctx.Path()
method := ctx.Method()
if app.recover != nil {
defer func() {
if r := recover(); r != nil {
ctx.error = fmt.Errorf("panic: %v", r)
app.recover(ctx)
}
}()
}
// loop trough routes
for _, route := range app.routes {
// Skip route if method is not allowed
@ -241,7 +253,7 @@ func (app *Application) handler(fctx *fasthttp.RequestCtx) {
// No routes found
if !found {
// Custom 404 handler?
ctx.Status(404).Send("Not Found")
ctx.SendStatus(404)
}
// release context back into sync pool