-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: handle fetch errors consistently (Fetcher.fetch)
- Loading branch information
Showing
14 changed files
with
375 additions
and
181 deletions.
There are no files selected for viewing
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,56 @@ | ||
'use strict'; | ||
|
||
let Fetcher = module.exports; | ||
|
||
/** | ||
* @typedef ResponseSummary | ||
* @prop {Boolean} ok | ||
* @prop {Headers} headers | ||
* @prop {Number} status | ||
* @prop {String} body | ||
*/ | ||
|
||
/** | ||
* @param {String} url | ||
* @param {RequestInit} opts | ||
* @returns {Promise<ResponseSummary>} | ||
*/ | ||
Fetcher.fetch = async function (url, opts) { | ||
let resp = await fetch(url, opts); | ||
let summary = Fetcher.throwIfNotOk(resp); | ||
|
||
return summary; | ||
}; | ||
|
||
/** | ||
* @param {Response} resp | ||
* @returns {Promise<ResponseSummary>} | ||
*/ | ||
Fetcher.throwIfNotOk = async function (resp) { | ||
let text = await resp.text(); | ||
|
||
if (!resp.ok) { | ||
let headers = Array.from(resp.headers); | ||
console.error('[Fetcher] error: Response Headers:', headers); | ||
console.error('[Fetcher] error: Response Text:', text); | ||
let err = new Error(`fetch was not ok`); | ||
Object.assign({ | ||
status: 503, | ||
code: 'E_FETCH_RELEASES', | ||
response: { | ||
status: resp.status, | ||
headers: headers, | ||
body: text, | ||
}, | ||
}); | ||
throw err; | ||
} | ||
|
||
let summary = { | ||
ok: resp.ok, | ||
headers: resp.headers, | ||
status: resp.status, | ||
body: text, | ||
}; | ||
return summary; | ||
}; |
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
Oops, something went wrong.