Skip to content

Commit a6a965f

Browse files
committed
verified that all errors are custom errors and renamed i18n to code
1 parent a4ec610 commit a6a965f

35 files changed

+162
-62
lines changed

src/clips/audio/audio.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { MediaClip } from '../media';
99
import { AudioSource } from '../../sources';
10+
import { IOError } from '../../errors';
1011

1112
import type { Track } from '../../tracks';
1213
import type { AudioClipProps } from './audio.interfaces';
@@ -57,8 +58,10 @@ export class AudioClip extends MediaClip<AudioClipProps> {
5758
this.element.onerror = () => {
5859
this.state = 'ERROR';
5960

60-
const error = new Error('An error occurred while processing the input medium.');
61-
this.trigger('error', error);
61+
const error = new IOError({
62+
code: 'sourceNotProcessable',
63+
message: 'An error occurred while processing the input medium.',
64+
});
6265

6366
reject(this.element.error ?? error);
6467
}

src/clips/clip/clip.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Serializer, serializable } from '../../services';
1010
import { EventEmitterMixin } from '../../mixins';
1111
import { Container } from 'pixi.js';
1212
import { replaceKeyframes } from './clip.utils';
13+
import { ValidationError } from '../../errors';
1314

1415
import type { ClipEvents, ClipState, ClipType } from './clip.types';
1516
import type { frame } from '../../types';
@@ -211,10 +212,16 @@ export class Clip<Props extends ClipProps = ClipProps> extends EventEmitterMixin
211212

212213
// invalid cases
213214
if (!time || time.millis <= this.start.millis || time.millis >= this.stop.millis) {
214-
throw new Error("Cannot split clip at the specified time")
215+
throw new ValidationError({
216+
code: 'splitOutOfRange',
217+
message: 'Cannot split clip at the specified time'
218+
})
215219
}
216220
if (!this.track) {
217-
throw new Error('Split must be connected to a track')
221+
throw new ValidationError({
222+
code: 'trackNotAttached',
223+
message: 'Track must be attached to a track',
224+
})
218225
}
219226

220227
const copy = this.copy() as this;

src/clips/html/html.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HtmlSource } from '../../sources';
99
import { Clip } from '../clip';
1010
import { VisualMixin, visualize } from '../mixins';
1111
import { Sprite, Texture } from 'pixi.js';
12+
import { IOError } from '../../errors';
1213

1314
import type { Track } from '../../tracks';
1415
import type { HtmlClipProps } from './html.interfaces';
@@ -57,7 +58,7 @@ export class HtmlClip extends VisualMixin(Clip<HtmlClipProps>) {
5758
this.context.imageSmoothingEnabled = false;
5859
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
5960
this.context.drawImage(this.element, 0, 0);
60-
61+
6162
this.sprite.texture = Texture.from(this.canvas, true);
6263

6364
this.trigger('load', undefined);
@@ -66,8 +67,11 @@ export class HtmlClip extends VisualMixin(Clip<HtmlClipProps>) {
6667
this.element.addEventListener('error', (e) => {
6768
console.error(e);
6869
this.state = 'ERROR';
70+
this.trigger('error', new IOError({
71+
code: 'sourceNotProcessable',
72+
message: 'An error occurred while processing the input medium.',
73+
}));
6974
if (this.track) this.detach();
70-
this.trigger('error', new Error('An error occurred while processing the input medium.'));
7175
});
7276

7377
this.on('update', async () => {
@@ -85,15 +89,21 @@ export class HtmlClip extends VisualMixin(Clip<HtmlClipProps>) {
8589
const height = this.source.document?.body?.scrollHeight;
8690

8791
if (!width || !height) {
88-
return reject(new Error('This html document cannot be displayed!'))
92+
return reject(new IOError({
93+
code: 'sourceNotProcessable',
94+
message: 'Cannot display source with height or width at 0',
95+
}))
8996
}
9097

9198
this.state = 'READY';
9299
resolve();
93100
}
94101
this.element.onerror = (e) => {
95102
console.error(e);
96-
reject(new Error('An error occurred while processing the input medium.'));
103+
reject(new IOError({
104+
code: 'sourceNotProcessable',
105+
message: 'An error occurred while processing the input medium.',
106+
}));
97107
}
98108
});
99109
}

src/clips/image/image.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Sprite, Texture } from 'pixi.js';
99
import { ImageSource } from '../../sources';
1010
import { Clip } from '../clip';
1111
import { VisualMixin, visualize } from '../mixins';
12+
import { IOError } from '../../errors';
1213

1314
import type { Track } from '../../tracks';
1415
import type { ImageClipProps } from './image.interfaces';
@@ -53,7 +54,10 @@ export class ImageClip extends VisualMixin(Clip<ImageClipProps>) {
5354
this.element.onerror = (e) => {
5455
console.error(e);
5556
this.state = 'ERROR';
56-
reject(new Error('An error occurred while processing the input medium.'));
57+
reject(new IOError({
58+
code: 'sourceNotProcessable',
59+
message: 'An error occurred while processing the input medium.',
60+
}));
5761
}
5862
});
5963
}

src/clips/media/media.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AudioSource } from '../../sources';
1010
import { RangeDeserializer } from './media.deserializer';
1111
import { serializable, } from '../../services';
1212
import { replaceKeyframes } from '../clip/clip.utils';
13-
import { ValidationError } from '../../errors';
13+
import { ReferenceError, ValidationError } from '../../errors';
1414
import { Clip } from '../clip';
1515

1616
import type { CaptionPresetStrategy, CaptionTrack } from '../../tracks';
@@ -164,7 +164,10 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
164164
public seek(time: Timestamp): Promise<void> {
165165
return new Promise((resolve, reject) => {
166166
if (!this.element) {
167-
return reject(new Error("Can't seek on element becaused it's not defined"));
167+
return reject(new ReferenceError({
168+
code: 'elementNotDefined',
169+
message: 'Cannot seek on undefined element',
170+
}));
168171
}
169172
if (time.millis < this.start.millis || time.millis > this.stop.millis) {
170173
time = this.start;
@@ -194,7 +197,10 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
194197

195198
// start is larger than the stop
196199
if (start.millis >= stop.millis) {
197-
throw new Error("Start can't lower than or equal the stop");
200+
throw new ValidationError({
201+
code: 'invalidKeyframe',
202+
message: "Start can't lower than or equal the stop"
203+
});
198204
}
199205
// start and/or stop are out of bounds
200206
if (start.millis < 0) {
@@ -240,10 +246,16 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
240246

241247
// invalid cases
242248
if (!time || time.millis <= this.start.millis || time.millis >= this.stop.millis) {
243-
throw new Error("Cannot split clip at the specified time")
249+
throw new ValidationError({
250+
code: 'invalidKeyframe',
251+
message: 'Cannot split clip at the specified time',
252+
});
244253
}
245254
if (!this.track) {
246-
throw new Error('Split must be connected to a track')
255+
throw new ReferenceError({
256+
code: 'trackNotDefined',
257+
message: 'Clip must be attached to a track',
258+
});
247259
}
248260

249261
// slice relative to the offset
@@ -267,7 +279,7 @@ export class MediaClip<Props extends MediaClipProps = MediaClipProps> extends Cl
267279
public async addCaptions(strategy?: CaptionPresetStrategy | (new () => CaptionPresetStrategy)): Promise<CaptionTrack> {
268280
if (!this.track?.composition) {
269281
throw new ValidationError({
270-
i18n: 'compositionNotDefined',
282+
code: 'compositionNotDefined',
271283
message: 'Captions can only be generated after the clip has been added to the composition',
272284
});
273285
}

src/clips/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function parseMimeType(mimeType: string): MimeType {
1919
if (!Object.keys(SUPPORTED_MIME_TYPES.MIXED).includes(mimeType)) {
2020
throw new errors.ValidationError({
2121
message: `${mimeType} is not an accepted mime type`,
22-
i18n: 'invalid_mimetype',
22+
code: 'invalid_mimetype',
2323
});
2424
}
2525
return mimeType as MimeType;

src/clips/video/video.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { MediaClip } from '../media';
1313
import { textureSwap } from './video.decorator';
1414
import { VisualMixin, visualize } from '../mixins';
1515
import { FPS_DEFAULT, Timestamp } from '../../models';
16+
import { IOError } from '../../errors';
1617

1718
import type { Track } from '../../tracks';
1819
import type { InitMessageData } from './worker.types';
@@ -90,8 +91,10 @@ export class VideoClip extends VisualMixin(MediaClip<VideoClipProps>) {
9091
this.element.onerror = () => {
9192
this.state = 'ERROR';
9293

93-
const error = new Error('An error occurred while processing the input medium.');
94-
this.trigger('error', error);
94+
const error = new IOError({
95+
code: 'sourceNotProcessable',
96+
message: 'An error occurred while processing the input medium.',
97+
});
9598

9699
reject(this.element.error ?? error);
97100
}

src/composition/composition.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ export class Composition extends EventEmitterMixin<CompositionEvents, typeof Ser
106106
})
107107
.catch(error => {
108108
console.error(error);
109-
this.trigger('error', new Error(`${error}`));
109+
this.trigger('error', new BaseError({
110+
code: 'backendDetectionError',
111+
message: `${error}`
112+
}));
110113
});
111114
}
112115

@@ -342,7 +345,7 @@ export class Composition extends EventEmitterMixin<CompositionEvents, typeof Ser
342345

343346
if (!this.renderer) {
344347
throw new BaseError({
345-
i18n: 'rendererNotDefined',
348+
code: 'rendererNotDefined',
346349
message: 'Please wait until the renderer is defined'
347350
})
348351
}

src/encoders/canvas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export class CanvasEncoder implements Required<EncoderInit> {
123123
public async encodeAudio(buffer: AudioBuffer): Promise<void> {
124124
if (!this.audio) {
125125
throw new EncoderError({
126-
i18n: 'initializationError',
126+
code: 'initializationError',
127127
message: 'Encoder must be initialized using {audio: true} to use this method'
128128
});
129129
}
@@ -170,7 +170,7 @@ export class CanvasEncoder implements Required<EncoderInit> {
170170

171171
if (!buffer) {
172172
throw new EncoderError({
173-
i18n: 'unexpectedRenderError',
173+
code: 'unexpectedRenderError',
174174
message: 'Muxer could not be finalized because the target buffer is not defined'
175175
})
176176
}

src/encoders/encoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Encoder extends WebcodecsVideoEncoder {
4242
public async render(target: FileSystemFileHandle | string = 'video.mp4', signal?: AbortSignal): Promise<void> {
4343
if (!this.composition.renderer) {
4444
throw new EncoderError({
45-
i18n: 'rendererNotInitialized',
45+
code: 'rendererNotInitialized',
4646
message: 'Cannot encode composition before the renderer has been initialized'
4747
});
4848
};

0 commit comments

Comments
 (0)