Skip to content

Commit 2f7caa2

Browse files
committed
(#415) Add some additional context to error messages to distinguish whether loading data or parsing failed
1 parent 1a71ead commit 2f7caa2

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Diff for: lib/imageResources.function.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('fetchFromUrl', () => {
3737
const SUT = () => fetchFromUrl(malformedUrl);
3838

3939
// THEN
40-
await expect(SUT).rejects.toThrowError("Invalid URL");
40+
await expect(SUT).rejects.toThrowError("Failed to fetch image data. Reason: Invalid URL");
4141
});
4242

4343
it('should throw on non-image URLs', async () => {
@@ -48,7 +48,7 @@ describe('fetchFromUrl', () => {
4848
const SUT = () => fetchFromUrl(nonImageUrl);
4949

5050
// THEN
51-
await expect(SUT).rejects.toThrowError('Could not find MIME for Buffer');
51+
await expect(SUT).rejects.toThrowError("Failed to parse image data. Reason: Could not find MIME for Buffer");
5252
});
5353

5454
it('should return an RGB image from a valid URL', async () => {

Diff for: lib/imageResources.function.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export async function fetchFromUrl(url: string | URL): Promise<Image> {
2222
} else {
2323
try {
2424
imageUrl = new URL(url);
25-
} catch (e) {
26-
throw e;
25+
} catch (e: any) {
26+
throw new Error(`Failed to fetch image data. Reason: ${e.message}`);
2727
}
2828
}
2929
return Jimp.read(imageUrl.href)
@@ -38,6 +38,6 @@ export async function fetchFromUrl(url: string | URL): Promise<Image> {
3838
);
3939
})
4040
.catch(err => {
41-
throw err;
41+
throw new Error(`Failed to parse image data. Reason: ${err.message}`);
4242
});
4343
}

0 commit comments

Comments
 (0)