screenshotone-api-sdk 1.1.13 → 1.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/@types/errors.d.ts +8 -0
- package/dist/errors.js +12 -0
- package/dist/main.js +6 -2
- package/package.json +1 -1
- package/src/errors.ts +18 -0
- package/src/main.ts +13 -4
- package/tests/main.test.ts +22 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare class APIError extends Error {
|
|
2
|
+
readonly httpStatusCode?: number;
|
|
3
|
+
readonly errorCode?: string;
|
|
4
|
+
readonly errorMessage?: string;
|
|
5
|
+
readonly documentationUrl?: string;
|
|
6
|
+
constructor(message: string, httpStatusCode?: number, errorCode?: string, errorMessage?: string, documentationUrl?: string);
|
|
7
|
+
}
|
|
8
|
+
export default APIError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class APIError extends Error {
|
|
4
|
+
constructor(message, httpStatusCode, errorCode, errorMessage, documentationUrl) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.httpStatusCode = httpStatusCode;
|
|
7
|
+
this.errorCode = errorCode;
|
|
8
|
+
this.errorMessage = errorMessage;
|
|
9
|
+
this.documentationUrl = documentationUrl;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.default = APIError;
|
package/dist/main.js
CHANGED
|
@@ -7,6 +7,7 @@ exports.AnimateOptions = exports.TakeOptions = exports.Client = void 0;
|
|
|
7
7
|
const big_js_1 = __importDefault(require("big.js"));
|
|
8
8
|
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
|
9
9
|
const signature_1 = require("./signature");
|
|
10
|
+
const errors_1 = __importDefault(require("./errors"));
|
|
10
11
|
const API_BASE_URL = "https://api.screenshotone.com";
|
|
11
12
|
const API_TAKE_PATH = "/take";
|
|
12
13
|
const API_ANIMATE_PATH = "/animate";
|
|
@@ -100,10 +101,13 @@ class Client {
|
|
|
100
101
|
}
|
|
101
102
|
try {
|
|
102
103
|
const data = await response.json();
|
|
103
|
-
throw new
|
|
104
|
+
throw new errors_1.default(`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`, response.status, data?.error_code, data?.error_message, data?.documentation_url);
|
|
104
105
|
}
|
|
105
106
|
catch (e) {
|
|
106
|
-
|
|
107
|
+
if (e instanceof errors_1.default) {
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
throw new Error(`Failed to generate animation, response returned ${response.status} (${response.statusText})`);
|
|
107
111
|
}
|
|
108
112
|
}
|
|
109
113
|
/**
|
package/package.json
CHANGED
package/src/errors.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
class APIError extends Error {
|
|
3
|
+
public readonly httpStatusCode?: number;
|
|
4
|
+
public readonly errorCode?: string;
|
|
5
|
+
public readonly errorMessage?: string;
|
|
6
|
+
public readonly documentationUrl?: string;
|
|
7
|
+
|
|
8
|
+
constructor(message: string, httpStatusCode?: number, errorCode?: string, errorMessage?: string, documentationUrl?: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
|
|
11
|
+
this.httpStatusCode = httpStatusCode;
|
|
12
|
+
this.errorCode = errorCode;
|
|
13
|
+
this.errorMessage = errorMessage;
|
|
14
|
+
this.documentationUrl = documentationUrl;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default APIError;
|
package/src/main.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import Big from "big.js";
|
|
2
2
|
import fetch from "cross-fetch";
|
|
3
3
|
import { signQueryString } from "./signature";
|
|
4
|
+
import APIError from "./errors";
|
|
4
5
|
|
|
5
6
|
const API_BASE_URL = "https://api.screenshotone.com";
|
|
6
7
|
const API_TAKE_PATH = "/take";
|
|
@@ -120,12 +121,20 @@ export class Client {
|
|
|
120
121
|
try {
|
|
121
122
|
const data = await response.json();
|
|
122
123
|
|
|
123
|
-
throw new
|
|
124
|
-
`
|
|
125
|
-
|
|
124
|
+
throw new APIError(
|
|
125
|
+
`Failed to generate animation, response returned ${response.status} (${response.statusText}): ${data?.error_message}`,
|
|
126
|
+
response.status,
|
|
127
|
+
data?.error_code,
|
|
128
|
+
data?.error_message,
|
|
129
|
+
data?.documentation_url
|
|
130
|
+
);
|
|
126
131
|
} catch (e) {
|
|
132
|
+
if (e instanceof APIError) {
|
|
133
|
+
throw e;
|
|
134
|
+
}
|
|
135
|
+
|
|
127
136
|
throw new Error(
|
|
128
|
-
`
|
|
137
|
+
`Failed to generate animation, response returned ${response.status} (${response.statusText})`
|
|
129
138
|
);
|
|
130
139
|
}
|
|
131
140
|
}
|
package/tests/main.test.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import APIError from "../src/errors";
|
|
1
2
|
import { TakeOptions, Client, AnimateOptions } from "../src/main";
|
|
2
3
|
|
|
3
4
|
describe("testing client", () => {
|
|
@@ -52,4 +53,25 @@ describe("testing client", () => {
|
|
|
52
53
|
|
|
53
54
|
expect(options.toQuery().getAll("block_ads").length).toBe(1);
|
|
54
55
|
});
|
|
56
|
+
|
|
57
|
+
test("returning an API error when the response is not successful", async () => {
|
|
58
|
+
jest.mock('cross-fetch', () => ({
|
|
59
|
+
__esModule: true,
|
|
60
|
+
default: jest.fn().mockImplementationOnce(() =>
|
|
61
|
+
Promise.resolve({
|
|
62
|
+
ok: false,
|
|
63
|
+
status: 400,
|
|
64
|
+
statusText: "Bad Request",
|
|
65
|
+
json: () => Promise.resolve({
|
|
66
|
+
error_code: "name_not_resolved",
|
|
67
|
+
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.",
|
|
68
|
+
documentation_url: "https://screenshotone.com/docs/errors/"
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
)
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
const options = AnimateOptions.url("https://example.com");
|
|
75
|
+
await expect(client.animate(options)).rejects.toThrow(APIError);
|
|
76
|
+
});
|
|
55
77
|
});
|