diff --git a/utils/request.js b/utils/request.js index 3b94c53..6f535db 100644 --- a/utils/request.js +++ b/utils/request.js @@ -2,12 +2,32 @@ // @ts-ignore const fetch = require("node-fetch") -function request(url, options = {}) { +async function request(url, options = {}) { if (!options.headers) options.headers = {} options.headers = { "user-agent": "CloudTubeBackend/1.0" } - return fetch(url, options) + const res = await fetch(url, options) + + if (res.status >= 400) { + if (res.status == 500) { + let body = await res.text(); + + const traceback_start = '
'; + const traceback_end = ''; + + 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