Skip to content

Commit 04b5cb9

Browse files
committed
🏭 Add global catchers with the catcher method
1 parent ad0d612 commit 04b5cb9

17 files changed

+257
-307
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<a href="https://coveralls.io/github/elbywan/wretch?branch=master"><img src="https://coveralls.io/repos/github/elbywan/wretch/badge.svg?branch=master" alt="Coverage Status" /></a>
99
</h1>
1010
<h4 align="center">
11-
A tiny (&lt; 1.4Kb g-zipped) wrapper built around fetch with an intuitive syntax.
11+
A tiny (&lt; 1.5Kb g-zipped) wrapper built around fetch with an intuitive syntax.
1212
</h4>
1313
<h5 align="center">
1414
<i>f[ETCH] [WR]apper</i>
@@ -254,6 +254,21 @@ wretch("http://server/which/returns/an/error/with/a/json/body")
254254
}
255255
```
256256
257+
#### catcher(code: number, catcher: (error: WretcherError) => void)
258+
259+
Adds a [catcher](https://github.com/elbywan/wretch#catchers) which will be called on every subsequent request error.
260+
261+
Very useful when you need to perform a repetitive action on a specific error code.
262+
263+
```js
264+
const w = wretcher()
265+
.catcher(404, err => redirect("/routes/notfound", err.message))
266+
.catcher(500, err => flashMessage("internal.server.error"))
267+
268+
// No need to catch 404 or 500 code, they are already taken care of.
269+
w.url("http://myapi.com/get/something").get().json(json => /* ... */)
270+
```
271+
257272
#### options(options: Object)
258273
259274
Set the fetch options.

dist/bundle/wretch.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)