Improve error handling of invalid bodies

This commit is contained in:
Jamie Kyle 2024-09-18 15:17:25 -07:00 committed by GitHub
parent 86ec8492e8
commit 25e864a863
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -351,23 +351,31 @@ async function _promiseAjax(
} }
let response: Response; let response: Response;
let result: string | Uint8Array | Readable | unknown;
try { try {
response = socketManager response = socketManager
? await socketManager.fetch(url, fetchOptions) ? await socketManager.fetch(url, fetchOptions)
: await fetch(url, fetchOptions); : await fetch(url, fetchOptions);
if ( } catch (e) {
options.serverUrl && log.error(logId, 0, 'Error');
getHostname(options.serverUrl) === getHostname(url) const stack = `${e.stack}\nInitial stack:\n${options.stack}`;
) { throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack);
await handleStatusCode(response.status); }
if (!unauthenticated && response.status === 401) { if (
log.error('Got 401 from Signal Server. We might be unlinked.'); options.serverUrl &&
window.Whisper.events.trigger('mightBeUnlinked'); getHostname(options.serverUrl) === getHostname(url)
} ) {
await handleStatusCode(response.status);
if (!unauthenticated && response.status === 401) {
log.error('Got 401 from Signal Server. We might be unlinked.');
window.Whisper.events.trigger('mightBeUnlinked');
} }
}
let result: string | Uint8Array | Readable | unknown;
try {
if (DEBUG && !isSuccess(response.status)) { if (DEBUG && !isSuccess(response.status)) {
result = await response.text(); result = await response.text();
} else if ( } else if (
@ -391,10 +399,15 @@ async function _promiseAjax(
} else { } else {
result = await response.textConverted(); result = await response.textConverted();
} }
} catch (e) { } catch (error) {
log.error(logId, 0, 'Error'); log.error(logId, response.status, 'Error');
const stack = `${e.stack}\nInitial stack:\n${options.stack}`; const stack = `${error.stack}\nInitial stack:\n${options.stack}`;
throw makeHTTPError('promiseAjax catch', 0, {}, e.toString(), stack); throw makeHTTPError(
`promiseAjax: error parsing body (Content-Type: ${response.headers.get('content-type')})`,
response.status,
response.headers.raw(),
stack
);
} }
if (!isSuccess(response.status)) { if (!isSuccess(response.status)) {