Fix error thrown on message retry
This commit is contained in:
parent
96f5430779
commit
c4de9436f3
2 changed files with 115 additions and 5 deletions
|
@ -33,6 +33,109 @@ describe('Message', () => {
|
||||||
return messages.add(attrs);
|
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', () => {
|
describe('getContact', () => {
|
||||||
it('gets outgoing contact', () => {
|
it('gets outgoing contact', () => {
|
||||||
const messages = new Whisper.MessageCollection();
|
const messages = new Whisper.MessageCollection();
|
||||||
|
|
|
@ -1941,6 +1941,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
previewWithData,
|
previewWithData,
|
||||||
stickerWithData,
|
stickerWithData,
|
||||||
null,
|
null,
|
||||||
|
this.get('deletedForEveryoneTimestamp'),
|
||||||
this.get('sent_at'),
|
this.get('sent_at'),
|
||||||
this.get('expireTimer'),
|
this.get('expireTimer'),
|
||||||
profileKey,
|
profileKey,
|
||||||
|
@ -2155,17 +2156,23 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
this.trigger('sent', this);
|
this.trigger('sent', this);
|
||||||
this.sendSyncMessage();
|
this.sendSyncMessage();
|
||||||
})
|
})
|
||||||
.catch(result => {
|
.catch((result: CustomError | CallbackResultType) => {
|
||||||
this.trigger('done');
|
this.trigger('done');
|
||||||
|
|
||||||
if (result.dataMessage) {
|
if ('dataMessage' in result && result.dataMessage) {
|
||||||
this.set({ dataMessage: result.dataMessage });
|
this.set({ dataMessage: result.dataMessage });
|
||||||
}
|
}
|
||||||
|
|
||||||
let promises = [];
|
let promises = [];
|
||||||
|
|
||||||
// If we successfully sent to a user, we can remove our unregistered flag.
|
// 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);
|
const c = window.ConversationController.get(identifier);
|
||||||
if (c && c.isEverUnregistered()) {
|
if (c && c.isEverUnregistered()) {
|
||||||
c.setRegistered();
|
c.setRegistered();
|
||||||
|
@ -2185,7 +2192,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
|
||||||
promises.push(c.getProfiles());
|
promises.push(c.getProfiles());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (result.successfulIdentifiers.length > 0) {
|
if (successfulIdentifiers.length > 0) {
|
||||||
const sentTo = this.get('sent_to') || [];
|
const sentTo = this.get('sent_to') || [];
|
||||||
|
|
||||||
// If we just found out that we couldn't send to a user because they are no
|
// 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,
|
unidentifiedDeliveries: result.unidentifiedDeliveries,
|
||||||
});
|
});
|
||||||
promises.push(this.sendSyncMessage());
|
promises.push(this.sendSyncMessage());
|
||||||
} else {
|
} else if (result.errors) {
|
||||||
this.saveErrors(result.errors);
|
this.saveErrors(result.errors);
|
||||||
}
|
}
|
||||||
promises = promises.concat(
|
promises = promises.concat(
|
||||||
|
|
Loading…
Reference in a new issue