Fix error thrown on message retry

This commit is contained in:
Evan Hahn 2020-10-22 12:32:23 -05:00 committed by GitHub
parent 96f5430779
commit c4de9436f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 115 additions and 5 deletions

View File

@ -33,6 +33,109 @@ describe('Message', () => {
return messages.add(attrs);
}
// NOTE: These tests are incomplete.
describe('send', () => {
it("saves the result's dataMessage", async () => {
const message = createMessage({ type: 'outgoing', source });
const fakeDataMessage = new ArrayBuffer(0);
const result = {
dataMessage: fakeDataMessage,
discoveredIdentifierPairs: [],
};
const promise = Promise.resolve(result);
await message.send(promise);
assert.strictEqual(message.get('dataMessage'), fakeDataMessage);
});
it('updates the `sent` attribute', async () => {
const message = createMessage({ type: 'outgoing', source, sent: false });
await message.send(
Promise.resolve({
discoveredIdentifierPairs: [],
})
);
assert.isTrue(message.get('sent'));
});
it("triggers the 'done' event on success", async () => {
const message = createMessage({ type: 'outgoing', source });
let callCount = 0;
message.on('done', () => {
callCount += 1;
});
await message.send(
Promise.resolve({
discoveredIdentifierPairs: [],
})
);
assert.strictEqual(callCount, 1);
});
it("triggers the 'sent' event on success", async () => {
const message = createMessage({ type: 'outgoing', source });
const calls = [];
message.on('sent', (...args) => {
calls.push(args);
});
await message.send(
Promise.resolve({
discoveredIdentifierPairs: [],
})
);
assert.lengthOf(calls, 1);
assert.strictEqual(calls[0][0], message);
});
it("triggers the 'done' event on failure", async () => {
const message = createMessage({ type: 'outgoing', source });
let callCount = 0;
message.on('done', () => {
callCount += 1;
});
await message.send(Promise.reject(new Error('something went wrong!')));
assert.strictEqual(callCount, 1);
});
it('saves errors from promise rejections with errors', async () => {
const message = createMessage({ type: 'outgoing', source });
const promise = Promise.reject(new Error('foo bar'));
await message.send(promise);
const errors = message.get('errors') || [];
assert.lengthOf(errors, 1);
assert.strictEqual(errors[0].message, 'foo bar');
});
it('saves errors from promise rejections with objects', async () => {
const message = createMessage({ type: 'outgoing', source });
const result = {
errors: [new Error('baz qux')],
discoveredIdentifierPairs: [],
};
const promise = Promise.reject(result);
await message.send(promise);
const errors = message.get('errors') || [];
assert.lengthOf(errors, 1);
assert.strictEqual(errors[0].message, 'baz qux');
});
});
describe('getContact', () => {
it('gets outgoing contact', () => {
const messages = new Whisper.MessageCollection();

View File

@ -1941,6 +1941,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
previewWithData,
stickerWithData,
null,
this.get('deletedForEveryoneTimestamp'),
this.get('sent_at'),
this.get('expireTimer'),
profileKey,
@ -2155,17 +2156,23 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
this.trigger('sent', this);
this.sendSyncMessage();
})
.catch(result => {
.catch((result: CustomError | CallbackResultType) => {
this.trigger('done');
if (result.dataMessage) {
if ('dataMessage' in result && result.dataMessage) {
this.set({ dataMessage: result.dataMessage });
}
let promises = [];
// If we successfully sent to a user, we can remove our unregistered flag.
result.successfulIdentifiers.forEach((identifier: string) => {
let successfulIdentifiers: Array<string>;
if ('successfulIdentifiers' in result) {
({ successfulIdentifiers = [] } = result);
} else {
successfulIdentifiers = [];
}
successfulIdentifiers.forEach((identifier: string) => {
const c = window.ConversationController.get(identifier);
if (c && c.isEverUnregistered()) {
c.setRegistered();
@ -2185,7 +2192,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
promises.push(c.getProfiles());
}
} else {
if (result.successfulIdentifiers.length > 0) {
if (successfulIdentifiers.length > 0) {
const sentTo = this.get('sent_to') || [];
// If we just found out that we couldn't send to a user because they are no
@ -2229,7 +2236,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
unidentifiedDeliveries: result.unidentifiedDeliveries,
});
promises.push(this.sendSyncMessage());
} else {
} else if (result.errors) {
this.saveErrors(result.errors);
}
promises = promises.concat(