Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ type Context struct {
handlers []Handler // the handlers associated with the current route
}

var defaultContentType = []byte("text/plain; charset=utf-8")
// Error sets response status code to the given value and sets response body
// to the given message.
func (c *Context) ErrorWithHeaders(msg string, statusCode int) {
c.SetStatusCode(statusCode)
c.SetContentTypeBytes(defaultContentType)
c.SetBodyString(msg)
}

// Router returns the Router that is handling the incoming HTTP request.
func (c *Context) Router() *Router {
return c.router
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ func (r *Router) NotFound(handlers ...Handler) {
// handleError is the error handler for handling any unhandled errors.
func (r *Router) handleError(c *Context, err error) {
if httpError, ok := err.(HTTPError); ok {
c.Error(httpError.Error(), httpError.StatusCode())
c.ErrorWithHeaders(httpError.Error(), httpError.StatusCode())
} else {
c.Error(err.Error(), http.StatusInternalServerError)
c.ErrorWithHeaders(err.Error(), http.StatusInternalServerError)
}
}

Expand Down