generated from nhcarrigan/template
feat: error handling and logger
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { FastifyInstance, FastifyRequest } from 'fastify';
|
||||
import { logger } from '../../utils/logger';
|
||||
|
||||
interface LogBody {
|
||||
level: 'debug' | 'info' | 'warn' | 'error';
|
||||
message: string;
|
||||
context?: string;
|
||||
error?: {
|
||||
name: string;
|
||||
message: string;
|
||||
stack?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export default async function (fastify: FastifyInstance) {
|
||||
fastify.post('/log', async function (request: FastifyRequest<{ Body: LogBody }>) {
|
||||
const { level, message, context, error } = request.body;
|
||||
|
||||
if (level === 'error' && error) {
|
||||
const errorObj = new Error(error.message);
|
||||
errorObj.name = error.name;
|
||||
if (error.stack) {
|
||||
errorObj.stack = error.stack;
|
||||
}
|
||||
await logger.error(context || 'Frontend', errorObj);
|
||||
} else if (level === 'error') {
|
||||
await logger.log('warn', `[Frontend Error] ${message}`);
|
||||
} else {
|
||||
await logger.log(level, `[Frontend] ${message}`);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Logger } from "@nhcarrigan/logger";
|
||||
|
||||
export const logger = new Logger("Library", process.env.LOG_TOKEN ?? "");
|
||||
+34
-1
@@ -1,9 +1,42 @@
|
||||
import Fastify from 'fastify';
|
||||
import { app } from './app/app';
|
||||
import { logger } from './app/utils/logger';
|
||||
|
||||
const host = process.env.HOST ?? 'localhost';
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : 12321;
|
||||
|
||||
// Global error handlers
|
||||
process.on('uncaughtException', (error: Error) => {
|
||||
void logger.error('Uncaught Exception', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason: unknown) => {
|
||||
const error = reason instanceof Error ? reason : new Error(String(reason));
|
||||
void logger.error('Unhandled Rejection', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
process.on('warning', (warning: Error) => {
|
||||
void logger.log('warn', `Process Warning: ${warning.name} - ${warning.message}`);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
void logger.log('info', 'SIGTERM signal received: closing HTTP server');
|
||||
server.close(() => {
|
||||
void logger.log('info', 'HTTP server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => {
|
||||
void logger.log('info', 'SIGINT signal received: closing HTTP server');
|
||||
server.close(() => {
|
||||
void logger.log('info', 'HTTP server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
|
||||
// Instantiate Fastify with some config
|
||||
const server = Fastify({
|
||||
logger: true,
|
||||
@@ -19,6 +52,6 @@ server.listen({ port, host }, (err) => {
|
||||
server.log.error(err);
|
||||
process.exit(1);
|
||||
} else {
|
||||
console.log(`[ ready ] http://${host}:${port}`);
|
||||
void logger.log('info', `Server ready at http://${host}:${port}`);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user