2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2022-01-12 00:50:11 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { isRecord } from '../../util/isRecord';
|
|
|
|
import { HTTPError } from '../../textsecure/Errors';
|
2022-04-25 23:05:23 +00:00
|
|
|
import { parseRetryAfterWithDefault } from '../../util/parseRetryAfter';
|
2022-01-12 00:50:11 +00:00
|
|
|
|
2024-10-25 14:10:33 +00:00
|
|
|
export function findRetryAfterTimeFromError(
|
|
|
|
err: unknown,
|
|
|
|
defaultValue?: number
|
|
|
|
): number {
|
2022-01-12 00:50:11 +00:00
|
|
|
let rawValue: unknown;
|
|
|
|
|
|
|
|
if (isRecord(err)) {
|
|
|
|
if (isRecord(err.responseHeaders)) {
|
|
|
|
rawValue = err.responseHeaders['retry-after'];
|
|
|
|
} else if (err.httpError instanceof HTTPError) {
|
|
|
|
rawValue = err.httpError.responseHeaders?.['retry-after'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 23:23:41 +00:00
|
|
|
if (Array.isArray(rawValue)) {
|
2024-10-25 14:10:33 +00:00
|
|
|
return parseRetryAfterWithDefault(rawValue[0], defaultValue);
|
2024-01-17 23:23:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 14:10:33 +00:00
|
|
|
return parseRetryAfterWithDefault(rawValue, defaultValue);
|
2022-01-12 00:50:11 +00:00
|
|
|
}
|