@@ -41,16 +41,16 @@ for many use cases. Restricting path parameters allows us to use memory efficien
4141
4242Registers a custom ` Echo.HTTPErrorHandler ` .
4343
44- Default handler sends ` HTTPError.Message ` HTTP response with ` HTTPError.Code ` status
45- code.
44+ Default handler rules
4645
47- - If HTTPError.Code is not set it uses ` 500 ` ".
48- - If HTTPError.Message is not set it uses status code text.
49- - If debug mode is enabled, HTTPError.Message is set to ` HTTPError.Error.Error() ` .
46+ - If error is of type ` Echo.HTTPError ` it sends HTTP response with status code ` HTTPError.Code `
47+ and message ` HTTPError.Message ` .
48+ - Else it sends ` 500 - Internal Server Error ` .
49+ - If debug mode is enabled, it uses ` error.Error() ` as status message.
5050
5151### Debug
5252
53- ` echo .SetDebug(on bool)`
53+ ` Echo .SetDebug(on bool)`
5454
5555Enables debug mode.
5656
@@ -67,12 +67,12 @@ code below registers a route for method `GET`, path `/hello` and a handler which
6767` Hello! ` HTTP response.
6868
6969``` go
70- echo.Get (" /hello" , func (*echo.Context ) *HTTPError {
70+ echo.Get (" /hello" , func (*echo.Context ) error {
7171 return c.String (http.StatusOK , " Hello!" )
7272})
7373```
7474
75- Echo's default handler is ` func(*echo.Context) *echo.HTTPError ` where ` echo.Context `
75+ Echo's default handler is ` func(*echo.Context) error ` where ` echo.Context `
7676primarily holds HTTP request and response objects. Echo also has a support for other
7777types of handlers.
7878
@@ -89,7 +89,7 @@ or by index `echo.Context.P(i uint8) string`. Getting parameter by index gives a
8989slightly better performance.
9090
9191``` go
92- echo.Get (" /users/:id" , func (c *echo.Context ) *HTTPError {
92+ echo.Get (" /users/:id" , func (c *echo.Context ) error {
9393 // By name
9494 id := c.Param (" id" )
9595
@@ -119,15 +119,15 @@ match
119119#### Example
120120
121121``` go
122- e.Get (" /users/:id" , func (c *echo.Context ) *HTTPError {
122+ e.Get (" /users/:id" , func (c *echo.Context ) error {
123123 return c.String (http.StatusOK , " /users/:id" )
124124})
125125
126- e.Get (" /users/new" , func (c *echo.Context ) *HTTPError {
126+ e.Get (" /users/new" , func (c *echo.Context ) error {
127127 return c.String (http.StatusOK , " /users/new" )
128128})
129129
130- e.Get (" /users/1/files/*" , func (c *echo.Context ) *HTTPError {
130+ e.Get (" /users/1/files/*" , func (c *echo.Context ) error {
131131 return c.String (http.StatusOK , " /users/1/files/*" )
132132})
133133```
@@ -152,7 +152,7 @@ application.
152152
153153``` go
154154// Handler
155- h := func (*echo.Context ) *HTTPError {
155+ h := func (*echo.Context ) error {
156156 return c.String (http.StatusOK , " OK" )
157157}
158158
@@ -169,23 +169,23 @@ e.Get("/users/:id", h)
169169### JSON
170170
171171``` go
172- context.JSON (code int , v interface {}) *HTTPError
172+ context.JSON (code int , v interface {}) error
173173```
174174
175175Sends a JSON HTTP response with status code.
176176
177177### String
178178
179179``` go
180- context.String (code int , s string ) *HTTPError
180+ context.String (code int , s string ) error
181181```
182182
183183Sends a text/plain HTTP response with status code.
184184
185185### HTML
186186
187187``` go
188- func (c *Context ) HTML (code int , html string ) * HTTPError
188+ func (c *Context ) HTML (code int , html string ) error
189189```
190190
191191Sends an HTML HTTP response with status code.
@@ -228,17 +228,17 @@ e.Favicon("public/favicon.ico")
228228
229229## Error Handling
230230
231- Echo advocates centralized HTTP error handling by returning `*echo.HTTPError ` from
232- middleware and handlers.
231+ Echo advocates centralized HTTP error handling by returning `error ` from middleware
232+ and handlers.
233233
234234It allows you to
235235
236236- Debug by writing stack trace to the HTTP response.
237237- Customize HTTP responses.
238238- Recover from panics inside middleware or handlers.
239239
240- For example, when a basic auth middleware finds invalid credentials it returns 401
241- " Unauthorized" error, aborting the current HTTP request.
240+ For example, when a basic auth middleware finds invalid credentials it returns
241+ `401 - Unauthorized` error, aborting the current HTTP request.
242242
243243```go
244244package main
@@ -251,18 +251,18 @@ import (
251251
252252func main() {
253253 e := echo.New ()
254- e.Use (func (c *echo.Context ) *echo. HTTPError {
254+ e.Use (func (c *echo.Context ) error {
255255 // Extract the credentials from HTTP request header and perform a security
256256 // check
257257
258258 // For invalid credentials
259- return & echo.HTTPError {Code: http.StatusUnauthorized }
259+ return echo.NewHTTPError ( http.StatusUnauthorized )
260260 })
261261 e.Get (" /welcome" , welcome)
262262 e.Run (" :1323" )
263263}
264264
265- func welcome (c *echo .Context ) * echo . HTTPError {
265+ func welcome (c *echo .Context ) error {
266266 return c.String (http.StatusOK , " Welcome!" )
267267}
268268```
0 commit comments