Skip to content

Commit

Permalink
🎨 Can now catch on error name
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Oct 9, 2017
1 parent 85852a6 commit 2a0f7e9
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 39 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ Shortcut to set the "Content-Type" header.
wretch("...").content("application/json")
```

#### catcher(code: number, catcher: (error: WretcherError) => void)
#### catcher(errorId: number | string, catcher: (error: WretcherError) => void)

Adds a [catcher](https://github.com/elbywan/wretch#catchers) which will be called on every subsequent request error.

Expand All @@ -359,8 +359,9 @@ Very useful when you need to perform a repetitive action on a specific error cod
const w = wretcher()
.catcher(404, err => redirect("/routes/notfound", err.message))
.catcher(500, err => flashMessage("internal.server.error"))
.error("SyntaxError", err => log("bad.json"))

// No need to catch 404 or 500 code, they are already taken care of.
// No need to catch 404 or 500 code or the json parsing error, they are already taken care of.
w.url("http://myapi.com/get/something").get().json(json => /* ... */)

// Default catchers can be overridden if needed.
Expand Down Expand Up @@ -576,9 +577,9 @@ Syntactic sugar for `error(418, cb)`.
Syntactic sugar for `error(500, cb)`.
#### error(code: number, cb: (error: WretcherError) => any)
#### error(errorId: number | string, cb: (error: WretcherError) => any)
Catch a specific error and perform the callback.
Catch a specific error given its code or name and perform the callback.
## Response Types
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle/wretch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/bundle/wretch.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/resolver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export declare type WretcherError = Error & {
text?: string;
json?: any;
};
export declare const resolver: (url: any) => (catchers?: Map<number, (error: WretcherError) => void>) => (opts?: {}) => {
export declare const resolver: (url: any) => (catchers?: Map<string | number, (error: WretcherError) => void>) => (opts?: {}) => {
res: <Result = Response>(cb?: (type: Response) => Result) => Promise<Result>;
json: <Result = {
[key: string]: any;
Expand Down
13 changes: 5 additions & 8 deletions dist/resolver.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2a0f7e9

Please sign in to comment.