Better error handling

This commit is contained in:
2024-02-04 13:48:24 +03:00
parent 8cd20b529f
commit ed742d6d32

View File

@@ -6,7 +6,15 @@ const server = Bun.serve({
port: PORT, port: PORT,
async fetch(req) { async fetch(req) {
if (!req.body) { if (!req.body) {
throw new Error("No body provided"); return new Response(
'Bad request',
{
status: 400,
headers: {
"Content-Type": "text/html",
},
}
)
} }
const submittedData = await req.json(); const submittedData = await req.json();
@@ -17,21 +25,26 @@ const server = Bun.serve({
${JSON.stringify(submittedData, null, 2)} ${JSON.stringify(submittedData, null, 2)}
\`\`\``) \`\`\``)
try {
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${text}&parse_mode=markdown`) await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${text}&parse_mode=markdown`)
} catch (error) {
return new Response(
'Internal server error',
{
status: 500,
headers: {
"Content-Type": "text/html",
},
}
)
}
return new Response('success', { return new Response('success', {
headers: { headers: {
'Access-Control-Allow-Origin': '*' 'Access-Control-Allow-Origin': '*'
} }
}); });
}, }
error(error) { })
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
},
});
console.log(`Listening on http://localhost:${server.port}...`); console.log(`Listening on http://localhost:${server.port}...`);