mirror of
https://github.com/bcomnes/deploy-to-neocities.git
synced 2026-01-16 22:56:28 +00:00
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
async function handleResponse (response) {
|
|
const contentType = response.headers.get('Content-Type');
|
|
const isJSON = contentType && contentType.match(/json/);
|
|
const data = isJSON ? await response.json() : await response.text();
|
|
if (response.ok) return data;
|
|
else {
|
|
return isJSON
|
|
? Promise.reject(new JSONHTTPError(response, data))
|
|
: Promise.reject(new TextHTTPError(response, data));
|
|
}
|
|
}
|
|
|
|
class HTTPError extends Error {
|
|
constructor (response) {
|
|
super(response.statusText);
|
|
this.name = this.constructor.name;
|
|
if (typeof Error.captureStackTrace === 'function') {
|
|
Error.captureStackTrace(this, this.constructor);
|
|
} else {
|
|
this.stack = new Error(response.statusText).stack;
|
|
}
|
|
this.status = response.status;
|
|
}
|
|
}
|
|
|
|
class TextHTTPError extends HTTPError {
|
|
constructor (response, data) {
|
|
super(response);
|
|
this.data = data;
|
|
}
|
|
}
|
|
|
|
class JSONHTTPError extends HTTPError {
|
|
constructor (response, json) {
|
|
super(response);
|
|
this.json = json;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
handleResponse,
|
|
HTTPError,
|
|
TextHTTPError,
|
|
JSONHTTPError
|
|
};
|