Skip to content

Commit ea07ae6

Browse files
Merge pull request #17 from apivideo/improve-errors
Improve error management
2 parents 7a8363b + 5fca607 commit ea07ae6

10 files changed

+547
-401
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Changelog
22
All changes to this project will be documented in this file.
33

4-
## [1.0.5] - 2022-03-24
4+
## [1.0.6] - 2022-04-22
5+
- Improve errors management
6+
7+
## [1.0.5] - 2022-04-21
58
- Fix date attributes types
69
- Add authentication using an API key
710

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/common.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ export declare type VideoUploadResponse = {
2929
readonly thumbnail?: string;
3030
};
3131
};
32+
export declare type VideoUploadError = {
33+
status: number;
34+
type?: string;
35+
title?: string;
36+
reason?: string;
37+
raw: string;
38+
};
3239
export declare const apiResponseToVideoUploadResponse: (response: any) => VideoUploadResponse;
40+
export declare const parseErrorResponse: (xhr: XMLHttpRequest) => VideoUploadError;

package-lock.json

Lines changed: 499 additions & 384 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@api.video/video-uploader",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "api.video video uploader",
55
"repository": {
66
"type": "git",

src/common.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export declare type VideoUploadResponse = {
3131
};
3232
};
3333

34+
export type VideoUploadError = {
35+
status: number;
36+
type?: string;
37+
title?: string;
38+
reason?: string;
39+
raw: string;
40+
}
41+
3442
export const apiResponseToVideoUploadResponse = (response: any): VideoUploadResponse => {
3543
const res = {
3644
...response,
@@ -41,4 +49,24 @@ export const apiResponseToVideoUploadResponse = (response: any): VideoUploadResp
4149
};
4250
delete res.public;
4351
return res;
52+
}
53+
54+
export const parseErrorResponse = (xhr: XMLHttpRequest): VideoUploadError => {
55+
try {
56+
const parsedResponse = JSON.parse(xhr.response);
57+
58+
return {
59+
status: xhr.status,
60+
raw: xhr.response,
61+
...parsedResponse
62+
}
63+
} catch(e) {
64+
// empty
65+
}
66+
67+
return {
68+
status: xhr.status,
69+
raw: xhr.response,
70+
reason: "UNKWOWN",
71+
}
4472
}

src/progressive-video-uploader.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_RETRIES, MIN_CHUNK_SIZE, VideoUploadResponse } from "./common";
1+
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_RETRIES, MIN_CHUNK_SIZE, parseErrorResponse, VideoUploadResponse } from "./common";
22
import { PromiseQueue } from "./promise-queue";
33

44
export interface ProgressiveUploaderOptionsWithUploadToken extends Options {
@@ -129,10 +129,7 @@ export class ProgressiveUploader {
129129
xhr.onreadystatechange = (_) => {
130130
if (xhr.readyState === 4) { // DONE
131131
if (xhr.status >= 400) {
132-
reject({
133-
status: xhr.status,
134-
message: xhr.response
135-
});
132+
reject(parseErrorResponse(xhr));
136133
}
137134
}
138135
};

src/video-uploader.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_CHUNK_SIZE, DEFAULT_RETRIES, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, VideoUploadResponse } from "./common";
1+
import { apiResponseToVideoUploadResponse, DEFAULT_API_HOST, DEFAULT_CHUNK_SIZE, DEFAULT_RETRIES, MAX_CHUNK_SIZE, MIN_CHUNK_SIZE, parseErrorResponse, VideoUploadResponse } from "./common";
22
import { PromiseQueue } from "./promise-queue";
33

44
export interface VideoUploaderOptionsWithUploadToken extends Options {
@@ -150,10 +150,7 @@ export class VideoUploader {
150150
xhr.onreadystatechange = (_) => {
151151
if (xhr.readyState === 4) { // DONE
152152
if (xhr.status >= 400) {
153-
reject({
154-
status: xhr.status,
155-
message: xhr.response
156-
});
153+
reject(parseErrorResponse(xhr));
157154
}
158155
}
159156
};

test/progressive-video-uploader.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ describe('Errors & retries', () => {
166166
uploader.uploadPart(new File([new ArrayBuffer(5*1024*1024)], "filename")).then(() => {
167167
throw new Error('should not succeed');
168168
}).catch((e) => {
169-
expect(e.status).to.be.eq(500);
170-
expect(e.message).to.be.eq('{"error": "oups"}');
169+
expect(e).to.be.eqls({ status: 500, raw: '{"error": "oups"}', error: 'oups' });
171170
done();
172171
});
173172
}).timeout(20000);

test/video-uploader.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ describe('Errors & retries', () => {
192192
uploader.upload().then(() => {
193193
throw new Error('should not succeed');
194194
}).catch((e) => {
195-
expect(e.status).to.be.eq(500);
196-
expect(e.message).to.be.eq('{"error": "oups"}');
195+
expect(e).to.be.eqls({ status: 500, raw: '{"error": "oups"}', error: 'oups' });
197196
done();
198197
});
199198
}).timeout(10000);

0 commit comments

Comments
 (0)