Skip to content

Commit

Permalink
fix: rejected promise on Klarna error
Browse files Browse the repository at this point in the history
  • Loading branch information
HormCodes committed Jan 5, 2022
1 parent 2439f6d commit 4ba5b4c
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 196 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"dotenv": "8.2.0",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/commit-analyzer": "^8.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3",
"dotenv": "8.2.0",
"husky": "^4.3.0",
"nock": "^13.2.1",
"standard-version": "^9.0.0",
"tsdx": "^0.13.3",
"tslib": "^2.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/api/checkout-v3/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ export interface IOrder {

export interface IOrderResponse extends IResponse {
statusCode: number;
error?: Error;
error?: Error | any;
response?: IOrder;
}
35 changes: 22 additions & 13 deletions src/http-request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as https from 'https';
import * as http from 'http';
import { parseJSON } from './utils';

export interface IOptions {
authorization: string;
Expand All @@ -8,8 +9,8 @@ export interface IOptions {

export interface IResponse {
statusCode: number;
error?: Error;
response?: any;
error?: string | Error | any;
response?: string | any;
}

export class HttpRequest {
Expand All @@ -26,7 +27,7 @@ export class HttpRequest {
path: string,
requestBody = {}
): Promise<IResponse> {
return new Promise((resolve, reject) => {
return new Promise(resolve => {
const options: http.RequestOptions = {
method: httpMethod,
hostname: this.hostname,
Expand Down Expand Up @@ -56,16 +57,24 @@ export class HttpRequest {
);
}

if (res.statusCode) {
if (res.statusCode < 200 || res.statusCode >= 300) {
reject({ statusCode: res.statusCode, error: apiResponse });
} else {
resolve({
statusCode: res.statusCode,
response: apiResponse !== '' && JSON.parse(apiResponse),
});
}
if (!res.statusCode) {
return;
}

const parsedJSON = parseJSON(apiResponse);
const response = parsedJSON ? parsedJSON : apiResponse;

if (res.statusCode < 200 || res.statusCode >= 300) {
return resolve({
statusCode: res.statusCode,
error: response,
});
}

resolve({
statusCode: res.statusCode,
response: response,
});
});
});

Expand All @@ -75,7 +84,7 @@ export class HttpRequest {

req.on('error', function(error): void {
console.error('Klarna request errored: ' + error.message);
reject({ statusCode: 500, error });
resolve({ statusCode: 500, error });
});

req.end();
Expand Down
16 changes: 16 additions & 0 deletions src/utils/__tests__/json.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { parseJSON } from '../json';

describe('json utils', () => {
describe('parseJSON', () => {
it('should return object for valid JSON string', () => {
expect(parseJSON('{"test": [1, true, "hello", null]}')).toEqual({
test: [1, true, 'hello', null],
});
});

it('should return null for invalid JSON string', () => {
expect(parseJSON('')).toEqual(null);
expect(parseJSON('test')).toEqual(null);
});
});
});
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { parseJSON } from './json';
7 changes: 7 additions & 0 deletions src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function parseJSON(jsonString: string): any | null {
try {
return JSON.parse(jsonString);
} catch {
return null;
}
}
57 changes: 57 additions & 0 deletions test/http-request.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { HttpRequest } from '../src/http-request';
import nock from 'nock';

describe('HttpRequest', () => {
const httpRequest = new HttpRequest({
authorization: 'none',
apiEndpoint: 'example.com',
});

describe('invoke', () => {
it('should not reject promise when there is an error about wrong hostname', async () => {
const originalError = new Error('Test');
nock('https://example.com')
.get('/test')
.replyWithError(originalError);

// Test should not throw error
const { statusCode, error, response } = await httpRequest.invoke(
'GET',
'/test'
);
expect(statusCode).toEqual(500);
expect(error).toEqual(originalError);
expect(response).toEqual(undefined);
});

it('should not parse non JSON response', async () => {
nock('https://example.com')
.get('/test')
.reply(200, 'hello');

// Test should not throw error
const { statusCode, error, response } = await httpRequest.invoke(
'GET',
'/test'
);
expect(statusCode).toEqual(200);
expect(error).toEqual(undefined);
expect(response).toEqual('hello');
});

it('should return not successful response as error', async () => {
nock('https://example.com')
.get('/test')
.reply(404, { notFound: true });

// Test should not throw error
const { statusCode, error, response } = await httpRequest.invoke(
'GET',
'/test'
);
expect(statusCode).toEqual(404);
expect(error).toEqual({ notFound: true });
expect(response).toEqual(undefined);
});
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"noFallthroughCasesInSwitch": true,
},
"include": ["src/**/*.ts"],
"exclude": ["test", "node_modules", "dist"]
"exclude": ["test", "node_modules", "dist", "**/__tests__/**"]
}
Loading

0 comments on commit 4ba5b4c

Please sign in to comment.