mirror of
https://git.sr.ht/~cadence/cloudtube
synced 2026-05-26 04:22:25 +00:00
33 lines
871 B
JavaScript
33 lines
871 B
JavaScript
/** @type {import("node-fetch").default} */
|
|
// @ts-ignore
|
|
const fetch = require("node-fetch")
|
|
|
|
async function request(url, options = {}) {
|
|
if (!options.headers) options.headers = {}
|
|
options.headers = {
|
|
"user-agent": "CloudTubeBackend/1.0"
|
|
}
|
|
const res = await fetch(url, options)
|
|
|
|
if (res.status >= 400) {
|
|
if (res.status == 500) {
|
|
let body = await res.text();
|
|
|
|
const traceback_start = '<pre id="traceback">';
|
|
const traceback_end = '</pre>';
|
|
|
|
const si = body.indexOf(traceback_start)
|
|
if (si >= 0) body = body.slice(si + traceback_start.length);
|
|
|
|
const ei = body.indexOf(traceback_end)
|
|
if (ei >= 0) body = body.slice(0, ei);
|
|
|
|
throw new Error(`Unexpected error in backend:\n\n${body}`);
|
|
} else {
|
|
throw new Error(`Unexpected response from backend: ${res.status} ${res.statusText}`);
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
module.exports.request = request
|