2022-01-14 21:34:52 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-09-27 14:44:09 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isRecord } from '../../util/isRecord';
|
|
|
|
import { parseIntWithFallback } from '../../util/parseIntWithFallback';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks for an HTTP code. First tries the top level error, then looks at its `httpError`
|
|
|
|
* property.
|
|
|
|
*/
|
|
|
|
export function getHttpErrorCode(maybeError: unknown): number {
|
|
|
|
if (!isRecord(maybeError)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
// This might be a textsecure/Errors/HTTPError
|
2021-09-27 14:44:09 +00:00
|
|
|
const maybeTopLevelCode = parseIntWithFallback(maybeError.code, -1);
|
|
|
|
if (maybeTopLevelCode !== -1) {
|
|
|
|
return maybeTopLevelCode;
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
// Various errors in textsecure/Errors have a nested httpError property
|
2021-09-27 14:44:09 +00:00
|
|
|
const { httpError } = maybeError;
|
|
|
|
if (!isRecord(httpError)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseIntWithFallback(httpError.code, -1);
|
|
|
|
}
|