mirror of https://github.com/gofiber/fiber.git
Add content type to default error handler
parent
164fbb3619
commit
713b51ae30
107
app.go
107
app.go
|
@ -35,19 +35,11 @@ type Map map[string]interface{}
|
||||||
// Handler defines a function to serve HTTP requests.
|
// Handler defines a function to serve HTTP requests.
|
||||||
type Handler = func(*Ctx)
|
type Handler = func(*Ctx)
|
||||||
|
|
||||||
// default settings
|
// Error represents an error that occurred while handling a request.
|
||||||
var (
|
type Error struct {
|
||||||
defaultBodyLimit = 4 * 1024 * 1024
|
Code int
|
||||||
defaultConcurrency = 256 * 1024
|
Message string
|
||||||
defaultErrorHandler = func(ctx *Ctx, err error) {
|
}
|
||||||
code := StatusInternalServerError
|
|
||||||
if e, ok := err.(*Error); ok {
|
|
||||||
code = e.Code
|
|
||||||
}
|
|
||||||
ctx.Status(code).SendString(err.Error())
|
|
||||||
}
|
|
||||||
defaultCompressedFileSuffix = ".fiber.gz"
|
|
||||||
)
|
|
||||||
|
|
||||||
// App denotes the Fiber application.
|
// App denotes the Fiber application.
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -178,48 +170,20 @@ type Static struct {
|
||||||
Index string
|
Index string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error represents an error that occurred while handling a request.
|
// default settings
|
||||||
type Error struct {
|
var (
|
||||||
Code int
|
defaultBodyLimit = 4 * 1024 * 1024
|
||||||
Message string
|
defaultConcurrency = 256 * 1024
|
||||||
}
|
defaultErrorHandler = func(ctx *Ctx, err error) {
|
||||||
|
code := StatusInternalServerError
|
||||||
// Error makes it compatible with `error` interface.
|
if e, ok := err.(*Error); ok {
|
||||||
func (e *Error) Error() string {
|
code = e.Code
|
||||||
return e.Message
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewError creates a new HTTPError instance.
|
|
||||||
func NewError(code int, message ...string) *Error {
|
|
||||||
e := &Error{code, utils.StatusMessage(code)}
|
|
||||||
if len(message) > 0 {
|
|
||||||
e.Message = message[0]
|
|
||||||
}
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Routes returns all registered routes
|
|
||||||
//
|
|
||||||
// for _, r := range app.Routes() {
|
|
||||||
// fmt.Printf("%s\t%s\n", r.Method, r.Path)
|
|
||||||
// }
|
|
||||||
func (app *App) Routes() []*Route {
|
|
||||||
routes := make([]*Route, 0)
|
|
||||||
for m := range app.stack {
|
|
||||||
for r := range app.stack[m] {
|
|
||||||
// Ignore HEAD routes handling GET routes
|
|
||||||
if m == 1 && app.stack[m][r].Method == MethodGet {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
routes = append(routes, app.stack[m][r])
|
|
||||||
}
|
}
|
||||||
|
ctx.Set(HeaderContentType, MIMETextPlainCharsetUTF8)
|
||||||
|
ctx.Status(code).SendString(err.Error())
|
||||||
}
|
}
|
||||||
// Sort routes by stack position
|
defaultCompressedFileSuffix = ".fiber.gz"
|
||||||
sort.Slice(routes, func(i, k int) bool {
|
)
|
||||||
return routes[i].pos < routes[k].pos
|
|
||||||
})
|
|
||||||
return routes
|
|
||||||
}
|
|
||||||
|
|
||||||
// New creates a new Fiber named instance.
|
// New creates a new Fiber named instance.
|
||||||
// You can pass optional settings when creating a new instance.
|
// You can pass optional settings when creating a new instance.
|
||||||
|
@ -498,6 +462,43 @@ func (app *App) Test(request *http.Request, msTimeout ...int) (*http.Response, e
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error makes it compatible with `error` interface.
|
||||||
|
func (e *Error) Error() string {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewError creates a new HTTPError instance.
|
||||||
|
func NewError(code int, message ...string) *Error {
|
||||||
|
e := &Error{code, utils.StatusMessage(code)}
|
||||||
|
if len(message) > 0 {
|
||||||
|
e.Message = message[0]
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
// Routes returns all registered routes
|
||||||
|
//
|
||||||
|
// for _, r := range app.Routes() {
|
||||||
|
// fmt.Printf("%s\t%s\n", r.Method, r.Path)
|
||||||
|
// }
|
||||||
|
func (app *App) Routes() []*Route {
|
||||||
|
routes := make([]*Route, 0)
|
||||||
|
for m := range app.stack {
|
||||||
|
for r := range app.stack[m] {
|
||||||
|
// Ignore HEAD routes handling GET routes
|
||||||
|
if m == 1 && app.stack[m][r].Method == MethodGet {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
routes = append(routes, app.stack[m][r])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Sort routes by stack position
|
||||||
|
sort.Slice(routes, func(i, k int) bool {
|
||||||
|
return routes[i].pos < routes[k].pos
|
||||||
|
})
|
||||||
|
return routes
|
||||||
|
}
|
||||||
|
|
||||||
// Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
|
// Sharding: https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/
|
||||||
func (app *App) prefork(address string) (ln net.Listener, err error) {
|
func (app *App) prefork(address string) (ln net.Listener, err error) {
|
||||||
// Master proc
|
// Master proc
|
||||||
|
|
Loading…
Reference in New Issue