UNPKG

wikibase-edit

Version:

Edit Wikibase from NodeJS

44 lines 1.28 kB
import { debug } from '../debug.js'; export async function parseResponseBody(res) { const raw = await res.text(); let data; try { data = JSON.parse(raw); } catch (err) { const customErr = new Error('Could not parse response: ' + raw); customErr.cause = err; customErr.name = 'wrong response format'; throw customErr; } debug('response', res.url, res.status, data); if (data.error != null) throw requestError(res, data); else return data; } function requestError(res, body) { const { code, info } = body.error || {}; const errMessage = `${code}: ${info}`; const err = new Error(errMessage); err.name = code; if (res.status === 200) { // Override false positive status code err.statusCode = 500; } else { err.statusCode = res.status; } err.headers = res.headers; err.body = body; err.url = res.url; if (res.url) err.stack += `\nurl: ${res.url}`; if (res.status) err.stack += `\nresponse status: ${res.status}`; if (body) err.stack += `\nresponse body: ${JSON.stringify(body)}`; err.context = { url: res.url, body }; return err; } //# sourceMappingURL=parse_response_body.js.map