Improve error handling of invalid bodies

This commit is contained in:
Jamie Kyle 2024-09-18 15:17:25 -07:00 committed by Jamie Kyle
parent 60b73a42c2
commit 5367b26320

View file

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