Skip to content

Commit 4dabce2

Browse files
authored
feat: dispatch error when endpoint promise fails to provide a string (#151)
1 parent 255a51d commit 4dabce2

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/upchunk.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,16 @@ export class UpChunk {
411411
defaultChunkSize: options.chunkSize,
412412
});
413413
this.chunkedIterator = this.chunkedIterable[Symbol.asyncIterator]();
414-
this.getEndpoint().then(() => {
415-
this.sendChunks();
416-
});
414+
this.getEndpoint()
415+
.then(() => {
416+
this.sendChunks();
417+
})
418+
.catch((e) => {
419+
const message = e?.message ? `: ${e.message}` : '';
420+
this.dispatch('error', {
421+
message: `Failed to get endpoint${message}`,
422+
});
423+
});
417424
this.off('error', readableStreamErrorCallback);
418425
}
419426
};
@@ -433,7 +440,14 @@ export class UpChunk {
433440
this.totalChunks = Math.ceil(this.file.size / this.chunkByteSize);
434441
this.validateOptions();
435442

436-
this.getEndpoint().then(() => this.sendChunks());
443+
this.getEndpoint()
444+
.then(() => this.sendChunks())
445+
.catch((e) => {
446+
const message = e?.message ? `: ${e.message}` : '';
447+
this.dispatch('error', {
448+
message: `Failed to get endpoint${message}`,
449+
});
450+
});
437451

438452
// restart sync when back online
439453
// trigger events when offline/back online
@@ -632,6 +646,9 @@ export class UpChunk {
632646

633647
return this.endpoint(this.file).then((value) => {
634648
this.endpointValue = value;
649+
if (typeof value !== 'string') {
650+
throw new TypeError('endpoint must return a string');
651+
}
635652
return this.endpointValue;
636653
});
637654
}

test/upchunk.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,37 @@ describe('integration', () => {
492492
});
493493
});
494494
});
495-
});
495+
496+
describe('endpoint promise error handling', () => {
497+
it('dispatches an error if the endpoint promise fails', (done) => {
498+
const upload = createUploadFixture({
499+
endpoint: () => Promise.reject(new Error('Endpoint fetch failed')),
500+
});
501+
502+
upload.on('error', (err) => {
503+
expect(err.detail.message).to.include('Failed to get endpoint: Endpoint fetch failed');
504+
done();
505+
});
506+
507+
upload.on('success', () => {
508+
done(new Error('Expected an error, but upload succeeded'));
509+
});
510+
});
511+
512+
it('dispatches an error if the endpoint promise does not return a string', (done) => {
513+
const upload = createUploadFixture({
514+
// @ts-expect-error we're testing this case
515+
endpoint: () => Promise.resolve(12345),
516+
});
517+
518+
upload.on('error', (err) => {
519+
expect(err.detail.message).to.include('Failed to get endpoint');
520+
done();
521+
});
522+
523+
upload.on('success', () => {
524+
done(new Error('Expected an error, but upload succeeded'));
525+
});
526+
});
527+
});
528+
});

0 commit comments

Comments
 (0)