Skip to content

Commit 1e15c85

Browse files
feat: Better documentation
1 parent 51209cb commit 1e15c85

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,20 @@ Wrap Go's stdlib `http.HandlerFunc` using `errhandler.Wrap()`:
1313

1414
```go
1515
mux := http.NewServeMux()
16-
mux.Handle("GET /products/{id}", errhandler.Wrap(getProduct))
16+
mux.Handle("GET /products", errhandler.Wrap(addProduct))
1717
```
1818

19-
Update your existing handler signature, by adding an `error` return type:
19+
Update your existing handler signature, by adding an `error` return type and utilizing the optional `errhandler.ParseJSON` and `errhandler.SendJSON` helper functions to reduce the amount of boilerplate code handlers require:
2020

2121
```go
2222
func addProduct(w http.ResponseWriter, r *http.Request) error {
23-
...
24-
}
25-
```
23+
var p product
24+
if err := errhandler.ParseJSON(r, &p); err != nil {
25+
return fmt.Errorf("parsing request json: %w", err)
26+
}
2627

27-
Utilize the `ParseJSON` and `SendJSON` functions to reduce the amount of boilerplate code handlers require:
28+
products[p.ID] = p
2829

29-
``` go
30-
var p product
31-
if err := errhandler.ParseJSON(r, &p); err != nil {
32-
return fmt.Errorf("parsing request json: %w", err)
30+
return errhandler.SendJSON(w, p)
3331
}
34-
35-
products[p.ID] = p
36-
37-
return errhandler.SendJSON(w, p)
3832
```

0 commit comments

Comments
 (0)