Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling and minor fixes #6

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions src/core/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<VideoResponse>([video, videoId]);
const data = fromSnakeToCamel(res.data) as VideoBase;
return new Video(this.#vhttp, data);
Expand All @@ -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<Record<string, never>>([video, videoId]);
};

Expand All @@ -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<AudioResponse>([audio, audioId]);
const data = fromSnakeToCamel(res.data) as AudioBase;
return new Audio(this.#vhttp, data);
Expand All @@ -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<Record<string, never>>([audio, audioId]);
};

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
16 changes: 14 additions & 2 deletions src/utils/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,29 @@ export class HttpClient {
return this.#db
.request<ResponseOf<R>, AxiosResponse<ResponseOf<R>, D>, D>(options)
.then(successResponse => {
return successResponse.data;
return this.parseResponse(successResponse.data);
})
.catch(
(
error: AxiosError<ErrorResponse | undefined, AxiosRequestConfig<D>>
error:
| AxiosError<ErrorResponse | undefined, AxiosRequestConfig<D>>
| VideodbError
) => {
if (error instanceof VideodbError) {
throw error;
}
throw this.#getPlausibleError(error);
}
);
};

parseResponse = <D>(data: ResponseOf<D>) => {
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
Expand Down
17 changes: 12 additions & 5 deletions src/utils/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -258,11 +261,15 @@ export class IndexJob extends Job<NoDataResponse, NoDataResponse> {
public start = async () => {
const transcriptJob = new TranscriptJob(this.vhttp, this.videoId);
transcriptJob.on('success', async () => {
const res = await this.vhttp.post<NoDataResponse, IndexConfig>(
[video, this.videoId, index],
this.indexConfig
);
this._handleSuccess(res);
try {
const res = await this.vhttp.post<NoDataResponse, IndexConfig>(
[video, this.videoId, index],
this.indexConfig
);
this._handleSuccess(res);
} catch (err) {
this._handleError(err);
}
});
transcriptJob.on('error', err => {
this._handleError(err);
Expand Down
Loading