Skip to content

Commit d603f17

Browse files
authored
Merge pull request #29 from Visual-Regression-Tracker/128-custom-exception
Unknown exception printed
2 parents 3020bf5 + 85ac9cd commit d603f17

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

lib/visualRegressionTracker.spec.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ const axiosErrorUnknown: AxiosError = {
5959
message: "Unknown error",
6060
response: {
6161
status: 500,
62-
data: {},
62+
data: {
63+
some: "data",
64+
},
6365
statusText: "Internal exception",
6466
headers: {},
6567
config: {},
@@ -363,12 +365,14 @@ describe("VisualRegressionTracker", () => {
363365
[
364366
axiosErrorUnknown.response?.status,
365367
axiosErrorUnknown,
366-
axiosErrorUnknown.message,
368+
JSON.stringify(axiosErrorUnknown),
367369
],
368-
[undefined, axiosErrorEmptyResponse, axiosErrorUnknown.message],
370+
[undefined, axiosErrorEmptyResponse, "No response from server"],
369371
])("handleException", (code, error, expectedMessage) => {
370372
it(`Error ${code}`, async () => {
371-
await expect(vrt["handleException"](error)).rejects.toThrowError(expectedMessage);
373+
await expect(vrt["handleException"](error)).rejects.toThrowError(
374+
expectedMessage
375+
);
372376
});
373377
});
374378
});

lib/visualRegressionTracker.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,20 @@ export class VisualRegressionTracker {
7373
}
7474

7575
private async handleException(error: AxiosError) {
76-
const status = error.response?.status;
77-
if (status === 401) {
78-
throw new Error("Unauthorized");
76+
if (!error.response) {
77+
throw new Error("No response from server");
7978
}
80-
if (status === 403) {
81-
throw new Error("Api key not authenticated");
79+
const status = error.response.status;
80+
switch (status) {
81+
case 401:
82+
throw new Error("Unauthorized");
83+
case 403:
84+
throw new Error("Api key not authenticated");
85+
case 404:
86+
throw new Error("Project not found");
87+
default:
88+
throw new Error(JSON.stringify(error.response.data, null, "\t"));
8289
}
83-
if (status === 404) {
84-
throw new Error("Project not found");
85-
}
86-
87-
throw new Error(error.message);
8890
}
8991

9092
async track(test: TestRun) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@visual-regression-tracker/sdk-js",
3-
"version": "4.1.3",
3+
"version": "4.2.0",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)