File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ export type { Config } from "./src/config.ts";
22export { config } from "./src/config.ts" ;
33
44export {
5+ ImageApiError ,
56 InvalidArgumentError ,
67 InvalidTimeoutError ,
78 MissingApiKeyError ,
Original file line number Diff line number Diff 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+
2432export class RequestTimeoutError extends Error {
2533 constructor ( ) {
2634 super ( "The request was timed out" ) ;
Original file line number Diff line number Diff line change 1- import { InvalidArgumentError } from "./errors.ts" ;
1+ import { ImageApiError , InvalidArgumentError } from "./errors.ts" ;
22import { readFile } from "node:fs" ;
33import 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 ;
Original file line number Diff line number Diff line change @@ -29,7 +29,6 @@ export type ImageApiParameters = {
2929} ;
3030
3131export type ImageApiResponse = {
32- message ?: string ;
33- image_id ?: string ;
34- error ?: string ;
32+ message : string ;
33+ image_id : string ;
3534} ;
Original file line number Diff line number Diff 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
243262describe (
You can’t perform that action at this time.
0 commit comments