Skip to content

Commit 018e7b1

Browse files
committed
Improve error handling
1 parent d52140d commit 018e7b1

5 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎mod.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type { Config } from "./src/config.ts";
22
export { config } from "./src/config.ts";
33

44
export {
5+
ImageApiError,
56
InvalidArgumentError,
67
InvalidTimeoutError,
78
MissingApiKeyError,

‎src/errors.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export class InvalidTimeoutError extends Error {
2121
}
2222
}
2323

24+
export class ImageApiError extends Error {
25+
constructor(message: string) {
26+
super(message);
27+
this.name = "ImageApiError";
28+
Object.setPrototypeOf(this, ImageApiError.prototype);
29+
}
30+
}
31+
2432
export class RequestTimeoutError extends Error {
2533
constructor() {
2634
super("The request was timed out");

‎src/serpapi.ts‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InvalidArgumentError } from "./errors.ts";
1+
import { ImageApiError, InvalidArgumentError } from "./errors.ts";
22
import { readFile } from "node:fs";
33
import type {
44
AccountApiParameters,
@@ -362,14 +362,23 @@ export async function uploadImage(
362362
} else {
363363
image = parameters.image;
364364
}
365-
const response = await _internals.uploadImage(
366-
image,
367-
{
368-
api_key: key,
369-
requestOptions: parameters.requestOptions,
370-
},
371-
timeout,
372-
);
365+
let response: string;
366+
try {
367+
response = await _internals.uploadImage(
368+
image,
369+
{
370+
api_key: key,
371+
requestOptions: parameters.requestOptions,
372+
},
373+
timeout,
374+
);
375+
} catch (error) {
376+
let message = "Image upload failed";
377+
try {
378+
message = JSON.parse(String(error)).error || message;
379+
} catch { /* */ }
380+
throw new ImageApiError(message);
381+
}
373382
const result = JSON.parse(response) as ImageApiResponse;
374383
callback?.(result);
375384
return result;

‎src/types.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export type ImageApiParameters = {
2929
};
3030

3131
export type ImageApiResponse = {
32-
message?: string;
33-
image_id?: string;
34-
error?: string;
32+
message: string;
33+
image_id: string;
3534
};

‎tests/serpapi_test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
getJson,
3333
getJsonBySearchId,
3434
getLocations,
35+
ImageApiError,
3536
InvalidArgumentError,
3637
InvalidTimeoutError,
3738
MissingApiKeyError,
@@ -238,6 +239,24 @@ describe("uploadImage", () => {
238239
await Deno.remove(imagePath);
239240
}
240241
});
242+
243+
it("throws ImageApiError", async () => {
244+
const executeStub = stub(
245+
_internals,
246+
"uploadImage",
247+
() => Promise.reject('{"error":"Invalid image"}'),
248+
);
249+
config.api_key = "test_api_key";
250+
try {
251+
await assertRejects(
252+
async () => await uploadImage({ image }),
253+
ImageApiError,
254+
"Invalid image",
255+
);
256+
} finally {
257+
executeStub.restore();
258+
}
259+
});
241260
});
242261

243262
describe(

0 commit comments

Comments
 (0)