Skip to content

Commit 6f72add

Browse files
committed
Updated docs
Signed-off-by: Vishal Rana <[email protected]>
1 parent 4226a3a commit 6f72add

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

website/docs/guide.md

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ for many use cases. Restricting path parameters allows us to use memory efficien
4040
`echo.NotFoundHandler(h Handler)`
4141

4242
Registers a custom NotFound handler. This handler is called in case router doesn't
43-
find matching route for the request.
43+
find a matching route for the HTTP request.
4444

4545
Default handler sends 404 "Not Found" response.
4646

4747
### HTTP error handler
4848

4949
`echo.HTTPErrorHandler(h HTTPErrorHandler)`
5050

51-
Registers a custom centralized HTTP error handler.
51+
Registers a custom centralized HTTP error handler `func(*HTTPError, *Context)`.
52+
53+
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
54+
code.
55+
56+
- If HTTPError.Code is not specified it uses 500 "Internal Server Error".
57+
- If HTTPError.Message is not specified it uses HTTPError.Error.Error() or the status
58+
code text.
5259

5360
## Routing
5461

@@ -60,7 +67,7 @@ zero dynamic memory allocation with no GC overhead.
6067

6168
Routes can be registered for any HTTP method, path and handler. For example, code
6269
below registers a route for method `GET`, path `/hello` and a handler which sends
63-
`Hello!` response.
70+
`Hello!` HTTP response.
6471

6572
```go
6673
echo.Get("/hello", func(*echo.Context) *HTTPError {
@@ -69,14 +76,16 @@ echo.Get("/hello", func(*echo.Context) *HTTPError {
6976
```
7077

7178
Echo's default handler is `func(*echo.Context) *echo.HTTPError` where `echo.Context`
72-
primarily holds request and response objects. Echo also has a support for other
79+
primarily holds HTTP request and response objects. Echo also has a support for other
7380
types of handlers.
7481

7582
<!-- TODO mention about not able to take advantage -->
7683

77-
<!-- ### Groups -->
84+
### Group
85+
86+
**WIP**
7887

79-
### Path parameters
88+
### Path parameter
8089

8190
URL path parameters can be extracted either by name `echo.Context.Param(name string) string` or by
8291
index `echo.Context.P(i uint8) string`. Getting parameter by index gives a slightly
@@ -164,23 +173,23 @@ e.Get("/users/:id", h)
164173
context.JSON(code int, v interface{}) *HTTPError
165174
```
166175

167-
Sends a JSON response with status code.
176+
Sends a JSON HTTP response with status code.
168177

169178
### String
170179

171180
```go
172181
context.String(code int, s string) *HTTPError
173182
```
174183

175-
Sends a text/plain response with status code.
184+
Sends a text/plain HTTP response with status code.
176185

177186
### HTML
178187

179188
```go
180189
func (c *Context) HTML(code int, html string) *HTTPError
181190
```
182191

183-
Sends an HTML response with status code.
192+
Sends an HTML HTTP response with status code.
184193

185194
### Static files
186195

@@ -210,8 +219,48 @@ index.html for path `/`
210219
e.Index("index.html")
211220
```
212221

213-
<!-- ## Error Handling -->
222+
## Error Handling
223+
224+
Echo advocates centralized HTTP error handling by returning `*echo.HTTPError` from
225+
middleware and handlers.
226+
227+
- In debug mode, write stack trace to the HTTP response.
228+
- Customize HTTP responses, may be using a pretty HTML template.
229+
- Recover from panics inside middleware or handlers.
230+
231+
For example, when a basic auth middleware finds invalid credentials it returns 401
232+
"Unauthorized" error, aborting the current HTTP request.
233+
234+
```go
235+
package main
236+
237+
import (
238+
"net/http"
239+
240+
"github.com/labstack/echo"
241+
)
242+
243+
func main() {
244+
e := echo.New()
245+
e.Use(func(c *echo.Context) *echo.HTTPError {
246+
// Extract the credentials from HTTP request header and perform a security
247+
// check
248+
249+
// For invalid credentials
250+
return &echo.HTTPError{Code: http.StatusUnauthorized}
251+
})
252+
e.Get("/welcome", welcome)
253+
e.Run(":4444")
254+
}
255+
256+
func welcome(c *echo.Context) *echo.HTTPError {
257+
return c.String(http.StatusOK, "Welcome!")
258+
}
259+
```
260+
261+
`echo.HTTPError` has status code, message and error fields. Internally it uses
262+
`http.Error` to send HTTP response with status code and message.
214263

215-
<!-- message not set err.Error() or status text -->
264+
## Deployment
216265

217-
<!-- Deployment -->
266+
**WIP**

website/docs/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
3333
- Handy encoding/decoding functions.
3434
- Serve static files, including index.
3535
- Centralized HTTP error handling.
36-
- Use a customized function to bind request body to a Go type.
36+
- Use a customized function to bind HTTP request body to a Go type.
3737
- Register a view render so you can use any HTML template engine.
3838

3939
## Getting Started
@@ -80,7 +80,7 @@ func main() {
8080

8181
`echo.New()` returns a new instance of Echo.
8282

83-
`e.Use(mw.Logger)` adds logging middleware to the chain. It logs every request
83+
`e.Use(mw.Logger)` adds logging middleware to the chain. It logs every HTTP request
8484
made to the server, producing output
8585

8686
```sh
@@ -90,10 +90,10 @@ made to the server, producing output
9090
```
9191

9292
`e.Get("/", hello)` Registers a GET route for path `/` with hello handler, so
93-
whenever server receives a request at `/`, hello handler is called.
93+
whenever server receives an HTTP request at `/`, hello handler is called.
9494

9595
In hello handler `c.String(http.StatusOK, "Hello, World!\n")` sends a text/plain
96-
response to the client with 200 status code.
96+
HTTP response to the client with 200 status code.
9797

9898
`e.Run(":4444")` Starts HTTP server at network address `:4444`.
9999

0 commit comments

Comments
 (0)