2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
/* eslint-disable max-classes-per-file */
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2023-03-09 23:33:12 +00:00
|
|
|
import type { LibSignalErrorBase } from '@signalapp/libsignal-client';
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
import { parseRetryAfter } from '../util/parseRetryAfter';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../types/ServiceId';
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-09-02 22:31:21 +00:00
|
|
|
import type { CallbackResultType } from './Types.d';
|
|
|
|
import type { HeaderListType } from './WebAPI';
|
2021-07-20 01:10:09 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
function appendStack(newError: Error, originalError: Error) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
newError.stack += `\nOriginal stack:\n${originalError.stack}`;
|
|
|
|
}
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
export type HTTPErrorHeadersType = {
|
|
|
|
[name: string]: string | ReadonlyArray<string>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class HTTPError extends Error {
|
2021-11-12 23:44:20 +00:00
|
|
|
public override readonly name = 'HTTPError';
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
public readonly code: number;
|
|
|
|
|
|
|
|
public readonly responseHeaders: HTTPErrorHeadersType;
|
|
|
|
|
|
|
|
public readonly response: unknown;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
message: string,
|
|
|
|
options: {
|
|
|
|
code: number;
|
|
|
|
headers: HTTPErrorHeadersType;
|
|
|
|
response?: unknown;
|
|
|
|
stack?: string;
|
2023-02-28 20:34:54 +00:00
|
|
|
cause?: unknown;
|
2021-07-28 21:37:09 +00:00
|
|
|
}
|
|
|
|
) {
|
2023-02-28 20:34:54 +00:00
|
|
|
super(`${message}; code: ${options.code}`, { cause: options.cause });
|
2021-07-28 21:37:09 +00:00
|
|
|
|
|
|
|
const { code: providedCode, headers, response, stack } = options;
|
|
|
|
|
|
|
|
this.code = providedCode > 999 || providedCode < 100 ? -1 : providedCode;
|
|
|
|
this.responseHeaders = headers;
|
|
|
|
|
|
|
|
this.stack += `\nOriginal stack:\n${stack}`;
|
|
|
|
this.response = response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export class ReplayableError extends Error {
|
|
|
|
functionCode?: number;
|
|
|
|
|
|
|
|
constructor(options: {
|
|
|
|
name?: string;
|
|
|
|
message: string;
|
|
|
|
functionCode?: number;
|
2022-11-22 18:43:43 +00:00
|
|
|
cause?: unknown;
|
2020-04-13 17:37:29 +00:00
|
|
|
}) {
|
2022-11-22 18:43:43 +00:00
|
|
|
super(options.message, { cause: options.cause });
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
this.name = options.name || 'ReplayableError';
|
|
|
|
this.message = options.message;
|
|
|
|
|
|
|
|
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
|
|
|
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.functionCode = options.functionCode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OutgoingIdentityKeyError extends ReplayableError {
|
2022-11-22 18:43:43 +00:00
|
|
|
public readonly identifier: string;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
// Note: Data to resend message is no longer captured
|
2023-03-09 23:33:12 +00:00
|
|
|
constructor(incomingIdentifier: string, cause?: LibSignalErrorBase) {
|
2020-04-13 17:37:29 +00:00
|
|
|
const identifier = incomingIdentifier.split('.')[0];
|
|
|
|
|
|
|
|
super({
|
|
|
|
name: 'OutgoingIdentityKeyError',
|
|
|
|
message: `The identity of ${identifier} has changed.`,
|
2023-03-09 23:33:12 +00:00
|
|
|
cause,
|
2020-04-13 17:37:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.identifier = identifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class OutgoingMessageError extends ReplayableError {
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly identifier: string;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly httpError?: HTTPError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
// Note: Data to resend message is no longer captured
|
|
|
|
constructor(
|
|
|
|
incomingIdentifier: string,
|
2021-05-28 19:11:19 +00:00
|
|
|
_m: unknown,
|
|
|
|
_t: unknown,
|
2021-09-22 00:58:03 +00:00
|
|
|
httpError?: HTTPError
|
2020-04-13 17:37:29 +00:00
|
|
|
) {
|
|
|
|
const identifier = incomingIdentifier.split('.')[0];
|
|
|
|
|
|
|
|
super({
|
|
|
|
name: 'OutgoingMessageError',
|
|
|
|
message: httpError ? httpError.message : 'no http error',
|
|
|
|
});
|
|
|
|
|
|
|
|
this.identifier = identifier;
|
|
|
|
|
|
|
|
if (httpError) {
|
2021-09-27 14:44:09 +00:00
|
|
|
this.httpError = httpError;
|
2020-04-13 17:37:29 +00:00
|
|
|
appendStack(this, httpError);
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 14:44:09 +00:00
|
|
|
|
|
|
|
get code(): undefined | number {
|
|
|
|
return this.httpError?.code;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class SendMessageNetworkError extends ReplayableError {
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly identifier: string;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly httpError: HTTPError;
|
2021-09-02 22:31:21 +00:00
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
constructor(identifier: string, _m: unknown, httpError: HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
super({
|
|
|
|
name: 'SendMessageNetworkError',
|
|
|
|
message: httpError.message,
|
|
|
|
});
|
|
|
|
|
2020-09-24 21:53:21 +00:00
|
|
|
[this.identifier] = identifier.split('.');
|
2021-09-27 14:44:09 +00:00
|
|
|
this.httpError = httpError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
|
|
|
}
|
2021-09-27 14:44:09 +00:00
|
|
|
|
|
|
|
get code(): number {
|
|
|
|
return this.httpError.code;
|
|
|
|
}
|
|
|
|
|
|
|
|
get responseHeaders(): undefined | HeaderListType {
|
|
|
|
return this.httpError.responseHeaders;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 00:31:37 +00:00
|
|
|
export type SendMessageChallengeData = Readonly<{
|
|
|
|
token?: string;
|
|
|
|
options?: ReadonlyArray<string>;
|
|
|
|
}>;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
export class SendMessageChallengeError extends ReplayableError {
|
|
|
|
public identifier: string;
|
|
|
|
|
2021-09-27 14:44:09 +00:00
|
|
|
public readonly httpError: HTTPError;
|
|
|
|
|
2021-05-06 00:09:29 +00:00
|
|
|
public readonly data: SendMessageChallengeData | undefined;
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
public readonly retryAt?: number;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
constructor(identifier: string, httpError: HTTPError) {
|
2021-05-06 00:09:29 +00:00
|
|
|
super({
|
|
|
|
name: 'SendMessageChallengeError',
|
|
|
|
message: httpError.message,
|
2022-11-22 18:43:43 +00:00
|
|
|
cause: httpError,
|
2021-05-06 00:09:29 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
[this.identifier] = identifier.split('.');
|
2021-09-27 14:44:09 +00:00
|
|
|
this.httpError = httpError;
|
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
this.data = httpError.response as SendMessageChallengeData;
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
const headers = httpError.responseHeaders || {};
|
|
|
|
|
2022-04-25 23:05:23 +00:00
|
|
|
const retryAfter = parseRetryAfter(headers['retry-after']);
|
|
|
|
if (retryAfter) {
|
|
|
|
this.retryAt = Date.now() + retryAfter;
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
|
|
|
}
|
2021-09-27 14:44:09 +00:00
|
|
|
|
|
|
|
get code(): number {
|
|
|
|
return this.httpError.code;
|
|
|
|
}
|
2021-05-06 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 01:10:09 +00:00
|
|
|
export class SendMessageProtoError extends Error implements CallbackResultType {
|
2023-08-10 16:43:33 +00:00
|
|
|
public readonly successfulServiceIds?: Array<ServiceIdString>;
|
2021-07-20 01:10:09 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
public readonly failoverServiceIds?: Array<ServiceIdString>;
|
2021-07-20 01:10:09 +00:00
|
|
|
|
|
|
|
public readonly errors?: CallbackResultType['errors'];
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
public readonly unidentifiedDeliveries?: Array<ServiceIdString>;
|
2021-07-20 01:10:09 +00:00
|
|
|
|
2023-05-10 01:25:37 +00:00
|
|
|
public readonly dataMessage: Uint8Array | undefined;
|
|
|
|
|
|
|
|
public readonly editMessage: Uint8Array | undefined;
|
2021-07-20 01:10:09 +00:00
|
|
|
|
2022-02-09 20:33:19 +00:00
|
|
|
// Fields necessary for send log save
|
2021-07-20 01:10:09 +00:00
|
|
|
public readonly contentHint?: number;
|
|
|
|
|
|
|
|
public readonly contentProto?: Uint8Array;
|
|
|
|
|
|
|
|
public readonly timestamp?: number;
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
public readonly recipients?: Record<ServiceIdString, Array<number>>;
|
2021-07-20 01:10:09 +00:00
|
|
|
|
2022-06-02 21:25:55 +00:00
|
|
|
public readonly sendIsNotFinal?: boolean;
|
|
|
|
|
2021-07-20 01:10:09 +00:00
|
|
|
constructor({
|
2023-08-10 16:43:33 +00:00
|
|
|
successfulServiceIds,
|
|
|
|
failoverServiceIds,
|
2021-07-20 01:10:09 +00:00
|
|
|
errors,
|
|
|
|
unidentifiedDeliveries,
|
|
|
|
dataMessage,
|
2023-05-10 01:25:37 +00:00
|
|
|
editMessage,
|
2021-07-20 01:10:09 +00:00
|
|
|
contentHint,
|
|
|
|
contentProto,
|
|
|
|
timestamp,
|
|
|
|
recipients,
|
2022-06-02 21:25:55 +00:00
|
|
|
sendIsNotFinal,
|
2021-07-20 01:10:09 +00:00
|
|
|
}: CallbackResultType) {
|
|
|
|
super(`SendMessageProtoError: ${SendMessageProtoError.getMessage(errors)}`);
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
this.successfulServiceIds = successfulServiceIds;
|
|
|
|
this.failoverServiceIds = failoverServiceIds;
|
2021-07-20 01:10:09 +00:00
|
|
|
this.errors = errors;
|
|
|
|
this.unidentifiedDeliveries = unidentifiedDeliveries;
|
|
|
|
this.dataMessage = dataMessage;
|
2023-05-10 01:25:37 +00:00
|
|
|
this.editMessage = editMessage;
|
2021-07-20 01:10:09 +00:00
|
|
|
this.contentHint = contentHint;
|
|
|
|
this.contentProto = contentProto;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.recipients = recipients;
|
2022-06-02 21:25:55 +00:00
|
|
|
this.sendIsNotFinal = sendIsNotFinal;
|
2021-07-20 01:10:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static getMessage(errors: CallbackResultType['errors']): string {
|
|
|
|
if (!errors) {
|
|
|
|
return 'No errors';
|
|
|
|
}
|
|
|
|
|
2022-11-22 18:43:43 +00:00
|
|
|
return errors.map(error => error.toString()).join(', ');
|
2021-07-20 01:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:37:29 +00:00
|
|
|
export class MessageError extends ReplayableError {
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly httpError: HTTPError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
constructor(_m: unknown, httpError: HTTPError) {
|
2020-04-13 17:37:29 +00:00
|
|
|
super({
|
|
|
|
name: 'MessageError',
|
|
|
|
message: httpError.message,
|
|
|
|
});
|
|
|
|
|
2021-09-27 14:44:09 +00:00
|
|
|
this.httpError = httpError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
|
|
|
}
|
2021-09-27 14:44:09 +00:00
|
|
|
|
|
|
|
get code(): number {
|
|
|
|
return this.httpError.code;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class UnregisteredUserError extends Error {
|
2023-08-10 16:43:33 +00:00
|
|
|
readonly serviceId: string;
|
2020-09-24 21:53:21 +00:00
|
|
|
|
2021-09-27 14:44:09 +00:00
|
|
|
readonly httpError: HTTPError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
constructor(serviceId: ServiceIdString, httpError: HTTPError) {
|
2020-09-24 21:53:21 +00:00
|
|
|
const { message } = httpError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
super(message);
|
|
|
|
|
|
|
|
this.message = message;
|
|
|
|
this.name = 'UnregisteredUserError';
|
|
|
|
|
|
|
|
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
|
|
|
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this);
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
this.serviceId = serviceId;
|
2021-09-27 14:44:09 +00:00
|
|
|
this.httpError = httpError;
|
2020-04-13 17:37:29 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
|
|
|
}
|
2021-09-27 14:44:09 +00:00
|
|
|
|
|
|
|
get code(): number {
|
|
|
|
return this.httpError.code;
|
|
|
|
}
|
2020-04-13 17:37:29 +00:00
|
|
|
}
|
2021-06-23 14:47:42 +00:00
|
|
|
|
|
|
|
export class ConnectTimeoutError extends Error {}
|
2021-09-22 00:58:03 +00:00
|
|
|
|
2023-01-13 22:01:47 +00:00
|
|
|
export class UnknownRecipientError extends Error {}
|
|
|
|
|
2023-01-21 00:50:34 +00:00
|
|
|
export class IncorrectSenderKeyAuthError extends Error {}
|
|
|
|
|
2021-09-22 00:58:03 +00:00
|
|
|
export class WarnOnlyError extends Error {}
|
2023-03-14 20:25:05 +00:00
|
|
|
|
|
|
|
export class NoSenderKeyError extends Error {}
|