From 64ba7a9253259e1b68cb4e48fec6963bdc2a2ecb Mon Sep 17 00:00:00 2001 From: 0xrohitgarg Date: Mon, 12 Feb 2024 14:08:08 +0530 Subject: [PATCH] fix: error handling and minor fixes --- CHANGELOG.md | 15 +++++++++++++++ src/constants.ts | 2 +- src/core/collection.ts | 13 +++++++++++++ src/index.ts | 1 + src/utils/httpClient.ts | 16 ++++++++++++++-- src/utils/job.ts | 17 ++++++++++++----- 6 files changed, 56 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5bb96b..1983dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,26 @@ # Changelog ## [Unreleased] + +### Added - Concept of Audio Files 🔈 - Concept of MediaAsset : VideoAsset, AudioAsset 💼 - Concept of Timeline for editing workflows ✂️ +- Export `VideodbError` - Minor updates in readme & package.json +### Changed +- Http client timeout 30s -> 60s + +### Fixed +- Better Error handling +- Param validation in + - `Coll.getVideo()` + - `Coll.getAudio()` + - `Coll.getVideo()` + - `Coll.deleteAudio()` + + ## [0.0.2]() (2024-01-24) diff --git a/src/constants.ts b/src/constants.ts index c85992d..fe34b14 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -35,7 +35,7 @@ export const ResponseStatus = { export const HttpClientDefaultValues = { max_retries: 3, - timeout: 30 * 1000, + timeout: 60 * 1000, backoff_factor: 0.1, } as const; diff --git a/src/core/collection.ts b/src/core/collection.ts index e387dce..cb67cd4 100644 --- a/src/core/collection.ts +++ b/src/core/collection.ts @@ -19,6 +19,7 @@ import { uploadToServer } from '@/utils/upload'; import { SearchFactory } from './search'; import { Video } from './video'; import { Audio } from './audio'; +import { VideodbError } from '@/utils/error'; const { video, audio } = ApiPath; @@ -56,6 +57,9 @@ export class Collection implements ICollection { * @throws an error if the request fails */ public getVideo = async (videoId: string) => { + if (!videoId.trim()) { + throw new VideodbError('Video ID cannot be empty'); + } const res = await this.#vhttp.get([video, videoId]); const data = fromSnakeToCamel(res.data) as VideoBase; return new Video(this.#vhttp, data); @@ -68,6 +72,9 @@ export class Collection implements ICollection { * @throws an error if the request fails */ public deleteVideo = async (videoId: string) => { + if (!videoId.trim()) { + throw new VideodbError('Video ID cannot be empty'); + } return await this.#vhttp.delete>([video, videoId]); }; @@ -91,6 +98,9 @@ export class Collection implements ICollection { * @throws an error if the request fails */ public getAudio = async (audioId: string) => { + if (!audioId.trim()) { + throw new VideodbError('Audio ID cannot be empty'); + } const res = await this.#vhttp.get([audio, audioId]); const data = fromSnakeToCamel(res.data) as AudioBase; return new Audio(this.#vhttp, data); @@ -103,6 +113,9 @@ export class Collection implements ICollection { * @throws an error if the request fails */ public deleteAudio = async (audioId: string) => { + if (!audioId.trim()) { + throw new VideodbError('Audio ID cannot be empty'); + } return await this.#vhttp.delete>([audio, audioId]); }; diff --git a/src/index.ts b/src/index.ts index c760e94..60ee91f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ export { Video } from './core/video'; export { Audio } from './core/audio'; export { Shot } from './core/shot'; export { IndexJob, UploadJob, TranscriptJob } from './utils/job'; +export { VideodbError } from './utils/error'; export { playStream, waitForJob } from './utils/index'; export { connect }; diff --git a/src/utils/httpClient.ts b/src/utils/httpClient.ts index 74f34f6..23c21b7 100644 --- a/src/utils/httpClient.ts +++ b/src/utils/httpClient.ts @@ -42,17 +42,29 @@ export class HttpClient { return this.#db .request, AxiosResponse, D>, D>(options) .then(successResponse => { - return successResponse.data; + return this.parseResponse(successResponse.data); }) .catch( ( - error: AxiosError> + error: + | AxiosError> + | VideodbError ) => { + if (error instanceof VideodbError) { + throw error; + } throw this.#getPlausibleError(error); } ); }; + parseResponse = (data: ResponseOf) => { + if (data.success === false) { + throw new VideodbError(data.message); + } + return data; + }; + /** * Used for getting a human readble and usable error type * @param error - The error recieved from the axios request diff --git a/src/utils/job.ts b/src/utils/job.ts index 087755d..c87b686 100644 --- a/src/utils/job.ts +++ b/src/utils/job.ts @@ -135,6 +135,9 @@ export abstract class Job< // TODO: remove the ignore comment after server update // @ts-ignore if ('response' in res && res.response) { + if (res.response.success === false) { + throw new VideodbError(res.response.message); + } this._handleSuccess(res.response.data); } else { this._handleSuccess(res.data); @@ -258,11 +261,15 @@ export class IndexJob extends Job { public start = async () => { const transcriptJob = new TranscriptJob(this.vhttp, this.videoId); transcriptJob.on('success', async () => { - const res = await this.vhttp.post( - [video, this.videoId, index], - this.indexConfig - ); - this._handleSuccess(res); + try { + const res = await this.vhttp.post( + [video, this.videoId, index], + this.indexConfig + ); + this._handleSuccess(res); + } catch (err) { + this._handleError(err); + } }); transcriptJob.on('error', err => { this._handleError(err);