Skip to content

Commit 333e076

Browse files
authored
Add server error handling (#31)
* add error handling in endpoint * add more try catch
1 parent 0be5523 commit 333e076

1 file changed

Lines changed: 59 additions & 42 deletions

File tree

server/src/routes/bolt.ts

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -63,54 +63,71 @@ router.post("/webhook", verifySignature, async (req, res) => {
6363
});
6464

6565
router.get("/products/:sku/checkout-link", async (req, res) => {
66-
const { sku } = req.params;
67-
68-
res.json({
69-
success: true,
70-
data: {
71-
link: env.bolt.links[sku],
72-
},
73-
});
66+
try {
67+
const { sku } = req.params;
68+
69+
res.json({
70+
success: true,
71+
data: {
72+
link: env.bolt.links[sku],
73+
},
74+
});
75+
} catch (error) {
76+
console.error("Error getting checkout link:", error);
77+
res.status(500).json({
78+
success: false,
79+
error: "Internal server error. " + (error as Error).message,
80+
});
81+
}
7482
});
7583

7684
router.post("/products/:sku/payment-link", authenticateToken, (req, res) => {
77-
const { sku } = req.params;
85+
try {
86+
const { sku } = req.params;
7887

79-
const product = db.getProductBySku(sku);
80-
if (!product) {
81-
return res.status(404).json({ error: "Product not found" });
82-
}
88+
const product = db.getProductBySku(sku);
89+
if (!product) {
90+
return res.status(404).json({ error: "Product sku not found" });
91+
}
92+
93+
const paymentLinkRequest: CreatePaymentLinkRequest = {
94+
item: {
95+
price: Math.floor(product.price * 100),
96+
name: product.name,
97+
currency: "USD",
98+
image_url: getAssetUrlForSku(sku),
99+
},
100+
redirect_url: "https://example.com/checkout/success",
101+
user_id: req.user!.id,
102+
game_id: env.bolt.gameId,
103+
metadata: {
104+
sku: product.sku,
105+
},
106+
};
83107

84-
const paymentLinkRequest: CreatePaymentLinkRequest = {
85-
item: {
86-
price: Math.floor(product.price * 100),
87-
name: product.name,
88-
currency: "USD",
89-
image_url: getAssetUrlForSku(sku),
90-
} as CreatePaymentLinkRequest["item"],
91-
redirect_url: "https://example.com/checkout/success",
92-
user_id: req.user!.id,
93-
game_id: env.bolt.gameId,
94-
metadata: {
95-
sku: product.sku,
96-
},
97-
};
98-
99-
boltApi.gaming
100-
.createPaymentLink(paymentLinkRequest)
101-
.then((response) => {
102-
console.log("Created payment link:", response);
103-
res.json({ success: true, data: response });
104-
})
105-
.catch((error) => {
106-
const {} = error;
107-
console.error("Error creating payment link");
108-
console.error("Response headers:", error?.response?.headers);
109-
console.error("Response body:", error?.response?.data);
110-
res
111-
.status(500)
112-
.json({ success: false, error: "Failed to create payment link" });
108+
boltApi.gaming
109+
.createPaymentLink(paymentLinkRequest)
110+
.then((response) => {
111+
console.log("Created payment link:", response);
112+
res.json({ success: true, data: response });
113+
})
114+
.catch((error) => {
115+
const {} = error;
116+
console.error("Error creating payment link");
117+
console.error("Response headers:", error?.response?.headers);
118+
console.error("Response body:", error?.response?.data);
119+
res.status(500).json({
120+
success: false,
121+
error: "Failed to create payment link. " + (error as Error).message,
122+
});
123+
});
124+
} catch (error) {
125+
console.error("Error creating payment link:", error);
126+
res.status(500).json({
127+
success: false,
128+
error: "Internal server error. " + (error as Error).message,
113129
});
130+
}
114131
});
115132

116133
router.get("/verify", authenticateToken, async (req, res) => {

0 commit comments

Comments
 (0)