-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
factor out fetch error handling
- Loading branch information
Showing
13 changed files
with
294 additions
and
217 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export { FetchWithErr, NotOk, OkResponse, mkFetchWithErr }; | ||
|
||
type FetchWithErr = ( | ||
input: RequestInfo | URL, | ||
init?: RequestInit, | ||
) => Promise<OkResponse>; | ||
|
||
type OkResponse = Response & { ok: true }; | ||
|
||
/** | ||
* Returns a fetch function that errors on a non-ok response | ||
* @param fetchFn - The underlying fetch function to use | ||
* @returns A fetch function that throws an error for non-ok responses | ||
*/ | ||
const mkFetchWithErr = | ||
(fetchFn: typeof fetch): FetchWithErr => | ||
async (input, init) => { | ||
const res = await fetchFn(input, init); | ||
if (!res.ok) { | ||
// maybe we should use the url from the response? | ||
const url = new URL(input.toString()); | ||
throw new NotOk(url, res.status, res.statusText); | ||
} | ||
// this assertion really shouldn't be necessary... | ||
return res as OkResponse; | ||
}; | ||
|
||
/** | ||
* Error class for non-ok responses | ||
*/ | ||
class NotOk extends Error { | ||
readonly url: URL; | ||
readonly status: number; | ||
readonly statusText: string; | ||
|
||
/** | ||
* @param url - The URL that resulted in a non-ok response | ||
* @param status - The status code of the non-ok response | ||
* @param statusText - The status text of the non-ok response | ||
*/ | ||
constructor(url: URL, status: number, statusText: string) { | ||
super(`Not ok: ${status} ${statusText} (${url})`); | ||
this.name = "NotOkError"; | ||
this.url = url; | ||
this.status = status; | ||
this.statusText = statusText; | ||
} | ||
} |
Oops, something went wrong.