1
0
Fork 0
mirror of https://git.sr.ht/~cadence/cloudtube synced 2026-06-01 06:56:48 +00:00
cloudtube/utils/request.js
Cadence Ember 095dc3f918 Update dependencies
Removes node-fetch in favour of node.js 20+ native fetch.
2026-05-27 22:51:13 +12:00

29 lines
776 B
JavaScript

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