From 39dede35f5c08642bd111766fc868d790a9c3add Mon Sep 17 00:00:00 2001 From: Lam Tran Date: Fri, 3 May 2019 15:14:32 +0200 Subject: [PATCH] Serialize parse error from CTP --- extension/package-lock.json | 6 ++++++ extension/package.json | 3 ++- extension/src/api/payment/payment.controller.js | 11 +++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/extension/package-lock.json b/extension/package-lock.json index 44d1632af..e084d8552 100644 --- a/extension/package-lock.json +++ b/extension/package-lock.json @@ -3928,6 +3928,12 @@ "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", "dev": true }, + "serialize-error": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/serialize-error/-/serialize-error-3.0.0.tgz", + "integrity": "sha512-+y3nkkG/go1Vdw+2f/+XUXM1DXX1XcxTl99FfiD/OEPUNw4uo0i6FKABfTAN5ZcgGtjTRZcEbxcE/jtXbEY19A==", + "dev": true + }, "shebang-command": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-1.2.0.tgz", diff --git a/extension/package.json b/extension/package.json index d691f7ab0..17a61cbd8 100644 --- a/extension/package.json +++ b/extension/package.json @@ -66,6 +66,7 @@ "bluebird": "^3.5.3", "bunyan": "^1.8.12", "lodash": "^4.17.11", - "node-fetch": "^2.3.0" + "node-fetch": "^2.3.0", + "serialize-error": "^3.0.0" } } diff --git a/extension/src/api/payment/payment.controller.js b/extension/src/api/payment/payment.controller.js index 75e77b5c5..a07d2275f 100644 --- a/extension/src/api/payment/payment.controller.js +++ b/extension/src/api/payment/payment.controller.js @@ -1,3 +1,4 @@ +const serializeError = require('serialize-error') const httpUtils = require('../../utils') const creditCardHandler = require('../../paymentHandler/creditCard/credit-card.handler') const paypalHandler = require('../../paymentHandler/paypal/paypal.handler') @@ -47,8 +48,14 @@ async function processRequest (request, response) { } async function _getPaymentObject (request) { - const requestBody = JSON.parse(await httpUtils.collectRequestData(request)) - return requestBody.resource.obj + const body = await httpUtils.collectRequestData(request) + try { + const requestBody = JSON.parse(body) + return requestBody.resource.obj + } catch (err) { + throw new Error(`Error during parsing CTP request: '${body}'. Ending the process. ` + + `Error: ${JSON.stringify(serializeError(err))}`) + } } function _getPaymentHandler (paymentObject) {