summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-07 20:00:19 +0100
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-07 20:00:19 +0100
commite5d2bd1cde4e948f137e62fbf334d437e5d66221 (patch)
tree2146160fc018625ceff3ea8fa51b60c00e19527b /api
parent500997042e98784c9d3b7a5ad684e36bf67cd0fa (diff)
Catches errors in createPaypalOrder
Diffstat (limited to 'api')
-rw-r--r--api/createPaypalOrder.ts18
1 files changed, 12 insertions, 6 deletions
diff --git a/api/createPaypalOrder.ts b/api/createPaypalOrder.ts
index 1a6e810..c0053d4 100644
--- a/api/createPaypalOrder.ts
+++ b/api/createPaypalOrder.ts
@@ -6,7 +6,7 @@ export default async function handler(request: VercelRequest, response: VercelRe
const { productDescription, shortDescription, totalPrice }: Partial<Order> = request.body;
if (request.method !== 'POST') {
- response.status(400);
+ response.status(404);
return;
}
@@ -17,12 +17,18 @@ export default async function handler(request: VercelRequest, response: VercelRe
} else if (!totalPrice) {
response.status(400).send('Expected body to contain totalPrice, but none provided.');
} else {
- const paypalResponse = await paypalCreateOrder({ productDescription, shortDescription, totalPrice });
+ try {
+ const paypalResponse = await paypalCreateOrder({ productDescription, shortDescription, totalPrice });
- if (paypalResponse.ok) {
- response.status(200).json({
- orderId: paypalResponse.id,
- });
+ if (paypalResponse.ok) {
+ response.status(200).json({
+ orderId: paypalResponse.id,
+ });
+ } else {
+ throw new Error(`PayPal returned ${paypalResponse.statusCode} ${paypalResponse.statusMessage}`);
+ }
+ } catch (err) {
+ response.status(500).send(`Internal server error. ${err}`);
}
}
}