Skip to content

Commit

Permalink
Add more details to the returned API error
Browse files Browse the repository at this point in the history
  • Loading branch information
krasun committed Aug 26, 2024
1 parent 1e243ee commit 68ee8dd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "screenshotone-api-sdk",
"homepage": "https://screenshotone.com",
"version": "1.1.13",
"version": "1.1.14",
"description": "Use ScreenshotOne.com API to generate screenshots of any website.",
"repository": {
"type": "git",
Expand Down
18 changes: 18 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

class APIError extends Error {
public readonly httpStatusCode?: number;
public readonly errorCode?: string;
public readonly errorMessage?: string;
public readonly documentationUrl?: string;

constructor(message: string, httpStatusCode?: number, errorCode?: string, errorMessage?: string, documentationUrl?: string) {
super(message);

this.httpStatusCode = httpStatusCode;
this.errorCode = errorCode;
this.errorMessage = errorMessage;
this.documentationUrl = documentationUrl;
}
}

export default APIError;
17 changes: 13 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Big from "big.js";
import fetch from "cross-fetch";
import { signQueryString } from "./signature";
import APIError from "./errors";

const API_BASE_URL = "https://api.screenshotone.com";
const API_TAKE_PATH = "/take";
Expand Down Expand Up @@ -120,12 +121,20 @@ export class Client {
try {
const data = await response.json();

throw new Error(
`failed to generate animation, response returned ${response.status} ${response.statusText}: ${data?.error_message}`
);
throw new APIError(
`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
response.status,
data?.error_code,
data?.error_message,
data?.documentation_url
);
} catch (e) {
if (e instanceof APIError) {
throw e;
}

throw new Error(
`failed to generate animation, response returned ${response.status} ${response.statusText}`
`Failed to generate animation, response returned ${response.status} (${response.statusText})`
);
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import APIError from "../src/errors";
import { TakeOptions, Client, AnimateOptions } from "../src/main";

describe("testing client", () => {
Expand Down Expand Up @@ -52,4 +53,25 @@ describe("testing client", () => {

expect(options.toQuery().getAll("block_ads").length).toBe(1);
});

test("returning an API error when the response is not successful", async () => {
jest.mock('cross-fetch', () => ({
__esModule: true,
default: jest.fn().mockImplementationOnce(() =>
Promise.resolve({
ok: false,
status: 400,
statusText: "Bad Request",
json: () => Promise.resolve({
error_code: "name_not_resolved",
error_message: "Usually, the error happens when the domain name of the requested URL is not resolved. If you are trying to take a screenshot of the new site, please, wait a bit until the DNS records are refreshed.",
documentation_url: "https://screenshotone.com/docs/errors/"
})
})
)
}));

const options = AnimateOptions.url("https://example.com");
await expect(client.animate(options)).rejects.toThrow(APIError);
});
});

0 comments on commit 68ee8dd

Please sign in to comment.