Better error handling
This commit is contained in:
35
index.ts
35
index.ts
@@ -6,7 +6,15 @@ const server = Bun.serve({
|
||||
port: PORT,
|
||||
async fetch(req) {
|
||||
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();
|
||||
@@ -17,21 +25,26 @@ const server = Bun.serve({
|
||||
${JSON.stringify(submittedData, null, 2)}
|
||||
\`\`\``)
|
||||
|
||||
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendMessage?chat_id=${CHAT_ID}&text=${text}&parse_mode=markdown`)
|
||||
try {
|
||||
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', {
|
||||
headers: {
|
||||
'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}...`);
|
||||
|
||||
Reference in New Issue
Block a user