Skip to content

Commit

Permalink
fix internal server error handling in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
manu4543 committed Mar 23, 2023
1 parent a7ae5a1 commit 3f2e121
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekit",
"version": "4.1.2",
"version": "4.1.3",
"description": "Offical NodeJS SDK for ImageKit.io integration",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
26 changes: 26 additions & 0 deletions tests/response-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const dummyAPIErrorResponse = {
message: "message"
}

const dummyAPIErrorResponseString = "Internal server error"

const responseHeaders = {
'x-request-id': "request-id"
}
Expand Down Expand Up @@ -65,4 +67,28 @@ describe("Promise", function () {

return Promise.reject();
});

it('Server unhandled error', async function () {
var requestId = "sdfdsfksjfldsjfjsdf";

const scope = nock('https://api.imagekit.io')
.get(`/v1/files/purge/${requestId}`)
.basicAuth({ user: initializationParams.privateKey, pass: '' })
.reply(500, dummyAPIErrorResponseString, responseHeaders)

try {
await imagekit.getPurgeCacheStatus(requestId);
} catch (ex) {
expect(ex).to.be.deep.equal({
help: dummyAPIErrorResponseString
});
expect(ex.$ResponseMetadata.statusCode).to.be.equal(500);
expect(ex.$ResponseMetadata.headers).to.be.deep.equal({
...responseHeaders
});
return Promise.resolve();
}

return Promise.reject();
});
});
15 changes: 12 additions & 3 deletions utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ImageKitOptions } from "../libs/interfaces/";
import { IKCallback } from "../libs/interfaces/IKCallback";
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";

export default function request<T, E extends Error> (
export default function request<T, E extends Error>(
requestOptions: RequestOptions,
defaultOptions: ImageKitOptions,
callback?: IKCallback<T, E>,
Expand Down Expand Up @@ -50,8 +50,16 @@ export default function request<T, E extends Error> (
statusCode: error.response.status,
headers: error.response.headers
}
// define status code and headers as non-enumerable properties on data
var result = error.response.data ? error.response.data : {} as any;

var result = {} as Object;
if (error.response.data && typeof error.response.data === "object") {
result = error.response.data
} else if (error.response.data && typeof error.response.data === "string") {
result = {
help: error.response.data
}
}

if (error.response.status === 429) {
result = {
...result,
Expand All @@ -60,6 +68,7 @@ export default function request<T, E extends Error> (
"X-RateLimit-Interval": parseInt(error.response.headers["x-ratelimit-interval"], 10),
}
}
// define status code and headers as non-enumerable properties on data
Object.defineProperty(result, "$ResponseMetadata", {
value: responseMetadata,
enumerable: false,
Expand Down

0 comments on commit 3f2e121

Please sign in to comment.