Skip to content

Commit

Permalink
send error message to log when failing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPSplunk committed May 31, 2024
1 parent d6636e3 commit 53bcbf8
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/paymentservice/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports.charge = async request => {
if (await OpenFeature.getClient().getBooleanValue("paymentServiceFailure", false)) {
throw new Error("PaymentService Fail Feature Flag Enabled");
}


const {
creditCardNumber: number,
Expand All @@ -33,6 +34,7 @@ module.exports.charge = async request => {

const card = cardValidator(number);
const { card_type: cardType, valid } = card.getCardDetails();
const { units, nanos, currencyCode } = request.amount;

span.setAttributes({
'app.payment.card_type': cardType,
Expand All @@ -51,6 +53,17 @@ module.exports.charge = async request => {
throw new Error(`The credit card (ending ${lastFourDigits}) expired on ${month}/${year}.`);
}

// if the environment variable PAYMENT_FAILS_PER_THOUSAND_CALLS is set, then randomly simulate a charge failure
let failRate = process.env['PAYMENT_FAILS_PER_THOUSAND_CALLS']
if (failRate) {
const randomNumber = Math.floor(Math.random() * 1000) + 1;
if (randomNumber <= failRate) {
let errorMsg="PaymentService charge failed. Bad API token (simulated).";
logger.error({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, errorMsg);
throw new Error(errorMsg);
}
}

// check baggage for synthetic_request=true, and add charged attribute accordingly
const baggage = propagation.getBaggage(context.active());
if (baggage && baggage.getEntry("synthetic_request") && baggage.getEntry("synthetic_request").value === "true") {
Expand All @@ -61,7 +74,6 @@ module.exports.charge = async request => {

span.end();

const { units, nanos, currencyCode } = request.amount;
logger.info({transactionId, cardType, lastFourDigits, amount: { units, nanos, currencyCode }}, "Transaction complete.");
transactionsCounter.add(1, {"app.payment.currency": currencyCode})
return { transactionId }
Expand Down

0 comments on commit 53bcbf8

Please sign in to comment.