From 4b710bf850f33bb34a91d739a7ccc9dd18008279 Mon Sep 17 00:00:00 2001 From: goncy Date: Wed, 29 Sep 2021 11:03:05 -0300 Subject: [PATCH] Log checkout error --- .../api/endpoints/checkout/get-checkout.ts | 48 ++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/framework/ordercloud/api/endpoints/checkout/get-checkout.ts b/framework/ordercloud/api/endpoints/checkout/get-checkout.ts index 90e33813d..d5a1cd5ec 100644 --- a/framework/ordercloud/api/endpoints/checkout/get-checkout.ts +++ b/framework/ordercloud/api/endpoints/checkout/get-checkout.ts @@ -2,7 +2,7 @@ import type { CheckoutEndpoint } from '.' const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ res, - body: {cartId}, + body: { cartId }, config: { restFetch }, }) => { // Return an error if no item is present @@ -13,23 +13,39 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ }) } - // Register credit card - const payments = await restFetch('GET', `/orders/Outgoing/${cartId}/payments`).then( - (response: {Items: unknown[]}) => response.Items - ) + try { + // Register credit card + const payments = await restFetch( + 'GET', + `/orders/Outgoing/${cartId}/payments` + ).then((response: { Items: unknown[] }) => response.Items) - const address = await restFetch('GET', `/orders/Outgoing/${cartId}`).then( - (response: {ShippingAddressID: string}) => response.ShippingAddressID - ) + const address = await restFetch('GET', `/orders/Outgoing/${cartId}`).then( + (response: { ShippingAddressID: string }) => response.ShippingAddressID + ) - // Return cart and errors - res.status(200).json({ - data: { - hasPayment: payments.length > 0, - hasShipping: Boolean(address) - }, - errors: [] - }) + // Return cart and errors + res.status(200).json({ + data: { + hasPayment: payments.length > 0, + hasShipping: Boolean(address), + }, + errors: [], + }) + } catch (error: any) { + console.log(error, error.toString(), JSON.stringify(error)) + + // Return cart and errors + res.status(500).json({ + data: null, + errors: [ + { + message: error.message, + code: error.statusCode || error.code || error.status, + }, + ], + }) + } } export default getCheckout