Skip to content

Commit 3f2e121

Browse files
committed
fix internal server error handling in the SDK
1 parent a7ae5a1 commit 3f2e121

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

tests/response-metadata.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const dummyAPIErrorResponse = {
2020
message: "message"
2121
}
2222

23+
const dummyAPIErrorResponseString = "Internal server error"
24+
2325
const responseHeaders = {
2426
'x-request-id': "request-id"
2527
}
@@ -65,4 +67,28 @@ describe("Promise", function () {
6567

6668
return Promise.reject();
6769
});
70+
71+
it('Server unhandled error', async function () {
72+
var requestId = "sdfdsfksjfldsjfjsdf";
73+
74+
const scope = nock('https://api.imagekit.io')
75+
.get(`/v1/files/purge/${requestId}`)
76+
.basicAuth({ user: initializationParams.privateKey, pass: '' })
77+
.reply(500, dummyAPIErrorResponseString, responseHeaders)
78+
79+
try {
80+
await imagekit.getPurgeCacheStatus(requestId);
81+
} catch (ex) {
82+
expect(ex).to.be.deep.equal({
83+
help: dummyAPIErrorResponseString
84+
});
85+
expect(ex.$ResponseMetadata.statusCode).to.be.equal(500);
86+
expect(ex.$ResponseMetadata.headers).to.be.deep.equal({
87+
...responseHeaders
88+
});
89+
return Promise.resolve();
90+
}
91+
92+
return Promise.reject();
93+
});
6894
});

utils/request.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImageKitOptions } from "../libs/interfaces/";
44
import { IKCallback } from "../libs/interfaces/IKCallback";
55
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
66

7-
export default function request<T, E extends Error> (
7+
export default function request<T, E extends Error>(
88
requestOptions: RequestOptions,
99
defaultOptions: ImageKitOptions,
1010
callback?: IKCallback<T, E>,
@@ -50,8 +50,16 @@ export default function request<T, E extends Error> (
5050
statusCode: error.response.status,
5151
headers: error.response.headers
5252
}
53-
// define status code and headers as non-enumerable properties on data
54-
var result = error.response.data ? error.response.data : {} as any;
53+
54+
var result = {} as Object;
55+
if (error.response.data && typeof error.response.data === "object") {
56+
result = error.response.data
57+
} else if (error.response.data && typeof error.response.data === "string") {
58+
result = {
59+
help: error.response.data
60+
}
61+
}
62+
5563
if (error.response.status === 429) {
5664
result = {
5765
...result,
@@ -60,6 +68,7 @@ export default function request<T, E extends Error> (
6068
"X-RateLimit-Interval": parseInt(error.response.headers["x-ratelimit-interval"], 10),
6169
}
6270
}
71+
// define status code and headers as non-enumerable properties on data
6372
Object.defineProperty(result, "$ResponseMetadata", {
6473
value: responseMetadata,
6574
enumerable: false,

0 commit comments

Comments
 (0)