Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 383d96b

Browse files
committed
refactor: make error types and messages more consistent
1 parent 19629e1 commit 383d96b

File tree

98 files changed

+303
-330
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+303
-330
lines changed

src/Image.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,9 @@ export class Image {
264264
);
265265
} else {
266266
if (bitDepth === 8 && data instanceof Uint16Array) {
267-
throw new Error(`bitDepth is ${bitDepth} but data is Uint16Array`);
267+
throw new RangeError(`bitDepth is ${bitDepth} but data is Uint16Array`);
268268
} else if (bitDepth === 16 && data instanceof Uint8Array) {
269-
throw new Error(`bitDepth is ${bitDepth} but data is Uint8Array`);
269+
throw new RangeError(`bitDepth is ${bitDepth} but data is Uint8Array`);
270270
}
271271
const expectedLength = this.size * this.channels;
272272
if (data.length !== expectedLength) {
@@ -493,7 +493,7 @@ export class Image {
493493
} else {
494494
if (value.length !== this.channels) {
495495
throw new RangeError(
496-
`the size of value must match the number of channels (${this.channels}). Got ${value.length} instead`,
496+
`the size of value must match the number of channels (${this.channels}). Received ${value.length}`,
497497
);
498498
}
499499
for (const val of value) validateValue(val, this);
@@ -546,7 +546,7 @@ export class Image {
546546
public fillAlpha(value: number): this {
547547
validateValue(value, this);
548548
if (!this.alpha) {
549-
throw new Error(
549+
throw new TypeError(
550550
'fillAlpha can only be called if the image has an alpha channel',
551551
);
552552
}
@@ -600,7 +600,7 @@ export class Image {
600600
case 'bottom-right':
601601
return { column: this.width - 1, row: this.height - 1 };
602602
default:
603-
throw new Error(`Unknown image coordinates ${coordinates}`);
603+
throw new RangeError(`invalid image coordinates: ${coordinates}`);
604604
}
605605
}
606606

@@ -1075,7 +1075,7 @@ function createPixelArray(
10751075
arr = new Uint16Array(length);
10761076
break;
10771077
default:
1078-
throw new Error(`unexpected bitDepth: ${bitDepth}`);
1078+
throw new RangeError(`invalid bitDepth: ${bitDepth}`);
10791079
}
10801080

10811081
// Alpha channel is 100% by default.

src/Mask.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ function printData(mask: Mask): string {
762762
*/
763763
function checkChannel(channel: number) {
764764
if (channel !== 0) {
765-
throw new Error(`Channel value must be 0 on type Mask, got ${channel}.`);
765+
throw new RangeError(
766+
`channel value must be 0 on type Mask. Received ${channel}`,
767+
);
766768
}
767769
}

src/__tests__/Image.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { inspect } from 'node:util';
22

3-
import { Image } from '../Image';
3+
import { Image, ImageCoordinates } from '../Image';
4+
import { Point } from '../geometry';
45

56
describe('create new images', () => {
67
it('should create a 8-bit image', () => {
@@ -86,7 +87,7 @@ describe('create new images', () => {
8687
it('should throw on wrong bit depth', () => {
8788
// @ts-expect-error we want to test the error.
8889
expect(() => new Image(1, 1, { bitDepth: 20 })).toThrow(
89-
/unexpected bitDepth: 20/,
90+
/invalid bitDepth: 20/,
9091
);
9192
});
9293
it('should throw with bit depth 8 but data 16', () => {
@@ -181,32 +182,31 @@ test('changeEach', () => {
181182
]);
182183
});
183184

184-
test('getCoordinates', () => {
185+
test.each<[ImageCoordinates, Point]>([
186+
['bottom-left', { column: 0, row: 4 }],
187+
['bottom-right', { column: 3, row: 4 }],
188+
['center', { column: 1.5, row: 2 }],
189+
['top-left', { column: 0, row: 0 }],
190+
['top-right', { column: 3, row: 0 }],
191+
])('getCoordinates - %s', (coordinates, point) => {
192+
const img = new Image(4, 5);
193+
expect(img.getCoordinates(coordinates)).toStrictEqual(point);
194+
});
195+
196+
test('getCoordinates - with rounding', () => {
185197
const img = new Image(4, 5);
186-
expect(img.getCoordinates('bottom-left')).toStrictEqual({
187-
column: 0,
188-
row: 4,
189-
});
190-
expect(img.getCoordinates('bottom-right')).toStrictEqual({
191-
column: 3,
192-
row: 4,
193-
});
194-
expect(img.getCoordinates('center')).toStrictEqual({
195-
column: 1.5,
196-
row: 2,
197-
});
198198
expect(img.getCoordinates('center', true)).toStrictEqual({
199199
column: 2,
200200
row: 2,
201201
});
202-
expect(img.getCoordinates('top-left')).toStrictEqual({
203-
column: 0,
204-
row: 0,
205-
});
206-
expect(img.getCoordinates('top-right')).toStrictEqual({
207-
column: 3,
208-
row: 0,
209-
});
202+
});
203+
204+
test('getCoordinates - bad parameter', () => {
205+
const img = new Image(4, 5);
206+
// @ts-expect-error bad parameter
207+
expect(() => img.getCoordinates('bad')).toThrow(
208+
/invalid image coordinates: bad/,
209+
);
210210
});
211211

212212
test('fill with a constant color', () => {
@@ -253,7 +253,7 @@ test('fill with out of range value in array', () => {
253253
test('fill with channel mismatch', () => {
254254
const img = new Image(1, 1);
255255
expect(() => img.fill([0, 1, 2, 3])).toThrow(
256-
/the size of value must match the number of channels \(3\). Got 4 instead/,
256+
/the size of value must match the number of channels \(3\). Received 4/,
257257
);
258258
});
259259

src/__tests__/Mask.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('get and set value', () => {
8282
const mask = new Mask(10, 20);
8383
expect(() => {
8484
mask.getValue(2, 1, 2);
85-
}).toThrow(/Channel value must be 0 on type Mask, got 2./);
85+
}).toThrow(/channel value must be 0 on type Mask. Received 2/);
8686
});
8787
});
8888

src/compare/__tests__/subtract.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test('difference size images should throw', async () => {
4545
const other = testUtils.createRgbImage([[5, 5, 5, 10, 10, 10]]);
4646
expect(() => {
4747
image.subtract(other);
48-
}).toThrow(`subtract: both images must have the same size`);
48+
}).toThrow(`both images must have the same size`);
4949
});
5050

5151
test('different alpha should throw', async () => {
@@ -55,13 +55,13 @@ test('different alpha should throw', async () => {
5555
const other = testUtils.createRgbImage([[1, 1, 1, 5, 5, 5, 10, 10, 10]]);
5656
expect(() => {
5757
image.subtract(other);
58-
}).toThrow(`subtract: both images must have the same alpha and bitDepth`);
58+
}).toThrow(`both images must have the same alpha and bitDepth`);
5959
});
6060

6161
test('different number of channels should throw', async () => {
6262
const image = testUtils.createGreyImage([[5, 10, 15]]);
6363
const other = testUtils.createRgbImage([[1, 1, 1, 5, 5, 5, 10, 10, 10]]);
6464
expect(() => {
6565
image.subtract(other);
66-
}).toThrow(`subtract: both images must have the same number of channels`);
66+
}).toThrow(`both images must have the same number of channels`);
6767
});

src/compare/computeSsim.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ export function computeSsim(
5353

5454
if (windowSize) {
5555
if (windowSize > image.width || windowSize > image.height) {
56-
throw new Error('windowSize cannot exceed image dimensions');
56+
throw new RangeError('windowSize cannot exceed image dimensions');
5757
}
5858
} else {
5959
windowSize = Math.min(11, image.height, image.width);
6060
}
61-
checkProcessable(image, 'ssim', {
61+
checkProcessable(image, {
6262
bitDepth: [8],
6363
channels: [1, 3, 4],
6464
});
6565

66-
validateForComparison('ssim', image, otherImage);
66+
validateForComparison(image, otherImage);
6767

6868
if (image.colorModel !== 'RGBA') {
6969
image = image.convertColor('RGBA');

src/compare/subtract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ export function subtract(
4242
let { absolute = false } = options;
4343

4444
if (image instanceof Image) {
45-
checkProcessable(image, 'subtract', {
45+
checkProcessable(image, {
4646
bitDepth: [1, 8, 16],
4747
components: [1, 3],
4848
alpha: false,
4949
});
5050
}
5151

52-
validateForComparison('subtract', image, otherImage);
52+
validateForComparison(image, otherImage);
5353

5454
let newImage = image.clone();
5555
if (newImage instanceof Image) {

src/compute/histogram.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function histogram(
2626
let { channel } = options;
2727
if (typeof channel !== 'number') {
2828
if (image.channels !== 1) {
29-
throw new Error('channel option is mandatory for multi-channel images');
29+
throw new TypeError(
30+
'channel option is mandatory for multi-channel images',
31+
);
3032
}
3133
channel = 0;
3234
}

src/correctColor/__tests__/correctColor.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,5 @@ test('should throw on different array length', () => {
200200

201201
expect(() => {
202202
image.correctColor(measuredColors, referenceColors);
203-
}).toThrow(
204-
'correctColor: number of measured colors and reference colors differ',
205-
);
203+
}).toThrow('number of measured colors and reference colors must be the same');
206204
});

src/correctColor/correctColor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ export function correctColor(
2222
measuredColors: RgbColor[],
2323
referenceColors: RgbColor[],
2424
): Image {
25-
checkProcessable(image, 'correctColor', {
25+
checkProcessable(image, {
2626
colorModel: ['RGB', 'RGBA'],
2727
});
2828

2929
if (measuredColors.length !== referenceColors.length) {
30-
throw new Error(
31-
'correctColor: number of measured colors and reference colors differ',
30+
throw new RangeError(
31+
'number of measured colors and reference colors must be the same',
3232
);
3333
}
3434

0 commit comments

Comments
 (0)