Skip to content

Commit

Permalink
Added stack trace to error logs and fatal logs (#1150)
Browse files Browse the repository at this point in the history
* feat: added stack trace to error logs and fatal logs

* chore: linting

* chore: reverting countries.json to initial state
  • Loading branch information
zenit2001 authored Aug 23, 2024
1 parent 8dff9d1 commit 9482e8f
Show file tree
Hide file tree
Showing 29 changed files with 77 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ server.post('Save', server.middleware.https, (req, res, next) => {
});
} catch (error) {
AdyenLogs.error_log(
`Error while saving settings in BM configuration: ${error}`,
'Error while saving settings in BM configuration:',
error,
);
res.json({
success: false,
Expand Down Expand Up @@ -67,7 +68,7 @@ server.post('TestConnection', server.middleware.https, (req, res, next) => {
error: false,
});
} catch (error) {
AdyenLogs.error_log(`Error while testing API credentials: ${error}`);
AdyenLogs.error_log('Error while testing API credentials:', error);
res.json({
error: true,
message: 'an unknown error has occurred',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,8 @@ function donate(donationReference, donationAmount, orderToken) {
}
});
return response;
} catch (e) {
AdyenLogs.error_log(
`Adyen: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.error_log('/donations call failed:', error);
return { error: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ function saveExpressShopperDetails(req, res, next) {
);
res.json({ shippingMethods });
return next();
} catch (e) {
AdyenLogs.error_log('Could not save amazon express shopper details');
} catch (error) {
AdyenLogs.error_log(
'Could not save amazon express shopper details:',
error,
);
res.redirect(URLUtils.url('Error-ErrorCode', 'err', 'general'));
return next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ exports[`POS Authorize should return error if createTerminalPayment fails 1`] =
}
`;

exports[`POS Authorize should return error if createTerminalPayment fails 2`] = `
[
[
"POS Authorise error, result: mockedResponse",
],
]
`;

exports[`POS Authorize should return error if there is no terminal ID 1`] = `
{
"authorized": false,
Expand All @@ -30,14 +22,6 @@ exports[`POS Authorize should return error if there is no terminal ID 1`] = `
}
`;

exports[`POS Authorize should return error if there is no terminal ID 2`] = `
[
[
"No terminal selected",
],
]
`;

exports[`POS Authorize should return success response when createTerminalPayment passes 1`] = `
{
"response": "mockedSuccessResponse",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@ describe('POS Authorize', () => {
paymentProcessor,
);
expect(authorizeResult).toMatchSnapshot();
expect(Logger.fatal.mock.calls).toMatchSnapshot();
expect(Logger.fatal.mock.calls.length).toBe(1);
});

it('should return error if createTerminalPayment fails', () => {
const {
createTerminalPayment,
} = require('*/cartridge/adyen/scripts/payments/adyenTerminalApi');
createTerminalPayment.mockImplementation(() => ({
error: true,
response: 'mockedResponse',
}));

const mockError = new Error('API error');
createTerminalPayment.mockImplementation(() => {
throw mockError;
});
const authorizeResult = posAuthorize(
orderNumber,
paymentInstrument,
paymentProcessor,
);
expect(Logger.fatal.mock.calls.length).toBe(1);
expect(authorizeResult).toMatchSnapshot();
expect(Logger.fatal.mock.calls).toMatchSnapshot();
});

it('should return success response when createTerminalPayment passes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function errorHandler() {
}

function paymentErrorHandler(result) {
AdyenLogs.error_log(`Payment failed, result: ${JSON.stringify(result)}`);
AdyenLogs.error_log('Payment failed:', JSON.stringify(result));
Transaction.rollback();
return { error: true };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@ const AdyenLogs = require('*/cartridge/adyen/logs/adyenCustomLogs');
* Authorize
*/
function posAuthorize(order, paymentInstrument, paymentProcessor) {
Transaction.wrap(() => {
paymentInstrument.paymentTransaction.transactionID = order.orderNo;
paymentInstrument.paymentTransaction.paymentProcessor = paymentProcessor;
});
try {
Transaction.wrap(() => {
paymentInstrument.paymentTransaction.transactionID = order.orderNo;
paymentInstrument.paymentTransaction.paymentProcessor = paymentProcessor;
});

const adyenPaymentForm = server.forms.getForm('billing').adyenPaymentFields;
const terminalId = adyenPaymentForm.terminalId.value;
const adyenPaymentForm = server.forms.getForm('billing').adyenPaymentFields;
const terminalId = adyenPaymentForm?.terminalId.value;

if (!terminalId) {
AdyenLogs.fatal_log('No terminal selected');
if (!terminalId) {
throw new Error('No terminal selected');
}
return adyenTerminalApi.createTerminalPayment(
order,
paymentInstrument,
terminalId,
);
} catch (error) {
AdyenLogs.fatal_log('POS Authorise error', error);
const errors = [
Resource.msg('error.payment.processor.not.supported', 'checkout', null),
];
Expand All @@ -28,25 +37,6 @@ function posAuthorize(order, paymentInstrument, paymentProcessor) {
error: true,
};
}

const result = adyenTerminalApi.createTerminalPayment(
order,
paymentInstrument,
terminalId,
);
if (result.error) {
AdyenLogs.fatal_log(`POS Authorise error, result: ${result.response}`);
const errors = [
Resource.msg('error.payment.processor.not.supported', 'checkout', null),
];
return {
authorized: false,
fieldErrors: [],
serverErrors: errors,
error: true,
};
}
return result;
}

module.exports = posAuthorize;
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function getPaymentMethodFromForm(paymentForm) {
return JSON.parse(paymentForm.adyenPaymentFields?.adyenStateData?.value)
.paymentMethod;
} catch (error) {
AdyenLogs.error_log('Failed to parse payment form stateData');
AdyenLogs.error_log('Failed to parse payment form stateData:', error);
return {};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ function cancelPartialPaymentOrder(req, res, next) {
amount,
});
} catch (error) {
AdyenLogs.error_log(
`Could not cancel partial payments order.. ${error.toString()}`,
);
AdyenLogs.error_log('Could not cancel partial payments order:', error);
res.json({
error: true,
errorMessage: Resource.msg('error.technical', 'checkout', null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ function callCheckBalance(req, res, next) {
...getFormattedProperties(checkBalanceResponse, orderAmount),
});
} catch (error) {
AdyenLogs.error_log(
`Failed to check gift card balance ${error.toString()}`,
);
AdyenLogs.error_log('Failed to check gift card balance:', error);
res.json({ error: true });
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function fetchGiftCards(req, res, next) {
totalDiscountedAmount,
});
} catch (error) {
AdyenLogs.error_log(`Failed to fetch gift cards ${error.toString()}`);
AdyenLogs.error_log('Failed to fetch gift cards:', error);
const currentBasket = BasketMgr.getCurrentBasket();
clearForms.clearAdyenBasketData(currentBasket);
res.json({ error: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ function makePartialPayment(req, res, next) {
),
});
} catch (error) {
AdyenLogs.error_log(
`Failed to create partial payment.. ${error.toString()}`,
);
AdyenLogs.error_log('Failed to create partial payment:', error);
res.json({ error: true });
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ function createPartialPaymentsOrder(req, res, next) {

res.json(responseData);
} catch (error) {
AdyenLogs.error_log(
`Failed to create partial payments order.. ${error.toString()}`,
);
AdyenLogs.error_log('Failed to create partial payments order:', error);
res.json({ error: true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ function doPaymentsCall(order, paymentInstrument, paymentRequest) {
AdyenLogs.info_log('Payment result: Refused');
}
return paymentResponse;
} catch (e) {
AdyenLogs.fatal_log(
`Adyen: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.fatal_log('Payments call failed:', error);
return {
error: true,
args: {
Expand Down Expand Up @@ -269,12 +267,8 @@ function createPaymentRequest(args) {
paymentRequest.paymentMethod,
);
return doPaymentsCall(order, paymentInstrument, paymentRequest);
} catch (e) {
AdyenLogs.error_log(
`error processing payment. Error message: ${
e.message
} more details: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.error_log('Error processing payment:', error);
return { error: true };
}
}
Expand All @@ -285,8 +279,8 @@ function doPaymentsDetailsCall(paymentDetailsRequest) {
constants.SERVICE.PAYMENTDETAILS,
paymentDetailsRequest,
);
} catch (ex) {
AdyenLogs.error_log(`error parsing response object ${ex.message}`);
} catch (error) {
AdyenLogs.error_log('Error parsing response object:', error);
return { error: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ function deleteRecurringPayment(args) {
constants.SERVICE.RECURRING_DISABLE,
requestObject,
);
} catch (e) {
AdyenLogs.fatal_log(
`Adyen: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.fatal_log('/disable call failed', error);
return { error: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,8 @@ function getMethods(basket, customer, countryCode) {
constants.SERVICE.CHECKOUTPAYMENTMETHODS,
paymentMethodsRequest,
);
} catch (e) {
AdyenLogs.fatal_log(
`Adyen: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.fatal_log('/paymentMethods call failed', error);
return { error: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ function getTerminals() {

requestObject.request = getTerminalRequest;
return executeCall(constants.SERVICE.CONNECTEDTERMINALS, requestObject);
} catch (e) {
AdyenLogs.fatal_log(
`Adyen getTerminals: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.fatal_log('/getTerminals call failed', error);
return { error: true, response: '{}' };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,8 @@ function zeroAuthPayment(customer, paymentInstrument) {
paymentInstrument,
zeroAuthRequest,
);
} catch (e) {
AdyenLogs.error_log(
`error processing zero auth payment. Error message: ${
e.message
} more details: ${e.toString()} in ${e.fileName}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.error_log('error processing zero auth payment:', error);
return { error: true };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,8 @@ function getCheckoutPaymentMethods(req, res, next) {
countryCode,
applicationInfo: AdyenHelper.getApplicationInfo(),
});
} catch (err) {
AdyenLogs.fatal_log(
`Failed to fetch payment methods ${JSON.stringify(err)}`,
);
} catch (error) {
AdyenLogs.fatal_log('Failed to fetch payment methods', error);
res.json({ error: true });
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@ function paymentsDetails(req, res, next) {

res.json(response);
return next();
} catch (e) {
AdyenLogs.error_log(
`Could not verify /payment/details: ${e.toString()} in ${e.fileName}:${
e.lineNumber
}`,
);
} catch (error) {
AdyenLogs.error_log('Could not verify /payment/details:', error);
res.redirect(URLUtils.url('Error-ErrorCode', 'err', 'general'));
return next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ function redirect(req, res, next) {
}

return next();
} catch (e) {
AdyenLogs.error_log(
`Error during 3ds1 response verification: ${e.toString()} in ${
e.fileName
}:${e.lineNumber}`,
);
} catch (error) {
AdyenLogs.error_log('Error during 3ds1 response verification:', error);
res.redirect(URLUtils.url('Error-ErrorCode', 'err', 'general'));
return next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ function updateSavedCards(args) {
if (
!(customer && customer.getProfile() && customer.getProfile().getWallet())
) {
AdyenLogs.error_log(
'Error while updating saved cards, could not get customer data',
);
return { error: true };
throw new Error('Error while updating saved cards, could not get customer data');
}

if (AdyenConfigs.getAdyenRecurringPaymentsEnabled()) {
Expand Down Expand Up @@ -127,8 +124,8 @@ function updateSavedCards(args) {
});
}
return { error: false };
} catch (ex) {
AdyenLogs.error_log(`${ex.toString()} in ${ex.fileName}:${ex.lineNumber}`);
} catch (error) {
AdyenLogs.error_log('Error while updating saved cards:', error);
return { error: true };
}
}
Expand Down
Loading

0 comments on commit 9482e8f

Please sign in to comment.