Translate errors before rejecting in SendMessage

This commit is contained in:
Fedor Indutny
2021-07-19 18:10:09 -07:00
committed by GitHub
parent c4a09b7507
commit 31989a7706
10 changed files with 99 additions and 40 deletions
+57
View File
@@ -6,6 +6,8 @@
import { parseRetryAfter } from '../util/parseRetryAfter';
import { CallbackResultType } from './Types.d';
function appendStack(newError: Error, originalError: Error) {
// eslint-disable-next-line no-param-reassign
newError.stack += `\nOriginal stack:\n${originalError.stack}`;
@@ -137,6 +139,61 @@ export class SendMessageChallengeError extends ReplayableError {
}
}
export class SendMessageProtoError extends Error implements CallbackResultType {
public readonly successfulIdentifiers?: Array<string>;
public readonly failoverIdentifiers?: Array<string>;
public readonly errors?: CallbackResultType['errors'];
public readonly unidentifiedDeliveries?: Array<string>;
public readonly dataMessage?: ArrayBuffer;
// Fields necesary for send log save
public readonly contentHint?: number;
public readonly contentProto?: Uint8Array;
public readonly timestamp?: number;
public readonly recipients?: Record<string, Array<number>>;
constructor({
successfulIdentifiers,
failoverIdentifiers,
errors,
unidentifiedDeliveries,
dataMessage,
contentHint,
contentProto,
timestamp,
recipients,
}: CallbackResultType) {
super(`SendMessageProtoError: ${SendMessageProtoError.getMessage(errors)}`);
this.successfulIdentifiers = successfulIdentifiers;
this.failoverIdentifiers = failoverIdentifiers;
this.errors = errors;
this.unidentifiedDeliveries = unidentifiedDeliveries;
this.dataMessage = dataMessage;
this.contentHint = contentHint;
this.contentProto = contentProto;
this.timestamp = timestamp;
this.recipients = recipients;
}
protected static getMessage(errors: CallbackResultType['errors']): string {
if (!errors) {
return 'No errors';
}
return errors
.map(error => (error.stackForLog ? error.stackForLog : error.toString()))
.join(', ');
}
}
export class SignedPreKeyRotationError extends ReplayableError {
constructor() {
super({
+2 -6
View File
@@ -23,12 +23,7 @@ import {
} from '@signalapp/signal-client';
import { WebAPIType } from './WebAPI';
import {
CallbackResultType,
SendMetadataType,
SendOptionsType,
CustomError,
} from './SendMessage';
import { SendMetadataType, SendOptionsType } from './SendMessage';
import {
OutgoingIdentityKeyError,
OutgoingMessageError,
@@ -36,6 +31,7 @@ import {
SendMessageChallengeError,
UnregisteredUserError,
} from './Errors';
import { CallbackResultType, CustomError } from './Types.d';
import { isValidNumber } from '../types/PhoneNumber';
import { Sessions, IdentityKeys } from '../LibSignalStores';
import { typedArrayToArrayBuffer as toArrayBuffer } from '../Crypto';
+10 -24
View File
@@ -30,6 +30,7 @@ import {
WebAPIType,
} from './WebAPI';
import createTaskWithTimeout from './TaskWithTimeout';
import { CallbackResultType } from './Types.d';
import OutgoingMessage, {
SerializedCertificateType,
SendLogCallbackType,
@@ -46,7 +47,11 @@ import {
StorageServiceCallOptionsType,
StorageServiceCredentials,
} from '../textsecure.d';
import { MessageError, SignedPreKeyRotationError } from './Errors';
import {
MessageError,
SignedPreKeyRotationError,
SendMessageProtoError,
} from './Errors';
import { BodyRangesType } from '../types/Util';
import {
LinkPreviewImage,
@@ -72,25 +77,6 @@ export type SendOptionsType = {
online?: boolean;
};
export type CustomError = Error & {
identifier?: string;
number?: string;
};
export type CallbackResultType = {
successfulIdentifiers?: Array<string>;
failoverIdentifiers?: Array<string>;
errors?: Array<CustomError>;
unidentifiedDeliveries?: Array<string>;
dataMessage?: ArrayBuffer;
// Fields necesary for send log save
contentHint?: number;
contentProto?: Uint8Array;
timestamp?: number;
recipients?: Record<string, Array<number>>;
};
type PreviewType = {
url: string;
title: string;
@@ -826,7 +812,7 @@ export default class MessageSender {
callback: (res: CallbackResultType) => {
res.dataMessage = message.toArrayBuffer();
if (res.errors && res.errors.length > 0) {
reject(res);
reject(new SendMessageProtoError(res));
} else {
resolve(res);
}
@@ -906,7 +892,7 @@ export default class MessageSender {
return new Promise((resolve, reject) => {
const callback = (result: CallbackResultType) => {
if (result && result.errors && result.errors.length > 0) {
reject(result);
reject(new SendMessageProtoError(result));
return;
}
@@ -942,7 +928,7 @@ export default class MessageSender {
return new Promise((resolve, reject) => {
const callback = (res: CallbackResultType) => {
if (res && res.errors && res.errors.length > 0) {
reject(res);
reject(new SendMessageProtoError(res));
} else {
resolve(res);
}
@@ -1839,7 +1825,7 @@ export default class MessageSender {
const callback = (res: CallbackResultType) => {
res.dataMessage = dataMessage;
if (res.errors && res.errors.length > 0) {
reject(res);
reject(new SendMessageProtoError(res));
} else {
resolve(res);
}
+19
View File
@@ -208,3 +208,22 @@ export type ProcessedSent = Omit<
export type ProcessedSyncMessage = Omit<Proto.ISyncMessage, 'sent'> & {
sent?: ProcessedSent;
};
export type CustomError = Error & {
identifier?: string;
number?: string;
};
export interface CallbackResultType {
successfulIdentifiers?: Array<string>;
failoverIdentifiers?: Array<string>;
errors?: Array<CustomError>;
unidentifiedDeliveries?: Array<string>;
dataMessage?: ArrayBuffer;
// Fields necesary for send log save
contentHint?: number;
contentProto?: Uint8Array;
timestamp?: number;
recipients?: Record<string, Array<number>>;
}