Skip to content

Commit

Permalink
feat: Handle errors in hono apps to prevent crashes (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle authored May 25, 2024
1 parent 6f49ee9 commit 02bb84a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
23 changes: 23 additions & 0 deletions apps/mail-bridge/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,32 @@ app.route('/postal', inboundApi);
// 404 handler
app.notFound((c) => c.json({ message: 'Not Found' }, 404));

// Global error handler
app.onError((err, c) => {
console.error(err);
return c.json({ message: 'Something went wrong' }, 500);
});

// Development error handlers
if (env.NODE_ENV === 'development') {
process.on('unhandledRejection', (err) => {
console.error(err);
});
process.on('uncaughtException', (err) => {
console.error(err);
});
}

// Start server
serve({
fetch: app.fetch,
port: env.PORT
}).on('listening', () => {
console.info(`Server listening on port ${env.PORT}`);
});

// Clean Exit
process.on('exit', () => {
console.info('Shutting down...');
process.exit(0);
});
23 changes: 23 additions & 0 deletions apps/platform/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,32 @@ app.use(
// 404 handler
app.notFound((c) => c.json({ message: 'Not Found' }, 404));

// Global error handler
app.onError((err, c) => {
console.error(err);
return c.json({ message: 'Something went wrong' }, 500);
});

// Development error handlers
if (env.NODE_ENV === 'development') {
process.on('unhandledRejection', (err) => {
console.error(err);
});
process.on('uncaughtException', (err) => {
console.error(err);
});
}

// Start server
serve({
fetch: app.fetch,
port: env.PORT
}).on('listening', () => {
console.info(`Server listening on port ${env.PORT}`);
});

// Clean Exit
process.on('exit', () => {
console.info('Shutting down...');
process.exit(0);
});
23 changes: 23 additions & 0 deletions apps/storage/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,32 @@ app.route('/api', deleteAttachmentsApi);
// 404 handler
app.notFound((c) => c.json({ message: 'Route Not Found' }, 404));

// Global error handler
app.onError((err, c) => {
console.error(err);
return c.json({ message: 'Something went wrong' }, 500);
});

// Development error handlers
if (env.NODE_ENV === 'development') {
process.on('unhandledRejection', (err) => {
console.error(err);
});
process.on('uncaughtException', (err) => {
console.error(err);
});
}

// Start server
serve({
fetch: app.fetch,
port: env.PORT
}).on('listening', () => {
console.info(`Server listening on port ${env.PORT}`);
});

// Clean Exit
process.on('exit', () => {
console.info('Shutting down...');
process.exit(0);
});
25 changes: 24 additions & 1 deletion ee/apps/billing/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { db } from '@u22n/database';
import { stripeData } from './stripe';
import { validateLicense } from './validateLicenseKey';

await validateLicense();

const app = new Hono<Ctx>();

// CORS middleware
Expand Down Expand Up @@ -48,11 +50,32 @@ app.use(
// 404 handler
app.notFound((c) => c.json({ message: 'Not Found' }, 404));

await validateLicense();
// Global error handler
app.onError((err, c) => {
console.error(err);
return c.json({ message: 'Something went wrong' }, 500);
});

// Development error handlers
if (env.NODE_ENV === 'development') {
process.on('unhandledRejection', (err) => {
console.error(err);
});
process.on('uncaughtException', (err) => {
console.error(err);
});
}

// Start server
serve({
fetch: app.fetch,
port: env.PORT
}).on('listening', () => {
console.info(`Server listening on port ${env.PORT}`);
});

// Clean Exit
process.on('exit', () => {
console.info('Shutting down...');
process.exit(0);
});

0 comments on commit 02bb84a

Please sign in to comment.