Implemented a production-ready TypeScript/Express.js service to receive and log MailGun webhook events (delivered, failed, opened, clicked, etc.). Key features: - Webhook endpoint (POST /webhook) with comprehensive event logging - Full TypeScript type definitions for all MailGun event types - Prometheus metrics integration for monitoring - Health check endpoint (GET /ping) - Comprehensive Jest test suite with 87.76% coverage - Docker containerization with build scripts Removed template/example code: - All SQL/MSSQL dependencies and related code - Example auth router and middleware - PRTG metrics support (simplified to Prometheus only) - Unused middleware (CORS, IP whitelist, request parsing/validation) - Template documentation (kept only MailGun webhook API spec) The service is clean, minimal, and focused solely on receiving and logging MailGun webhook events to the console. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { ErrorRequestHandler, Request, Response } from "express";
|
|
import { HttpError } from "http-errors";
|
|
import { logError } from '../lib/logger';
|
|
|
|
/**
|
|
* Final error handler that executes when an unhandled error occurs.
|
|
* This prevents the server from crashing and ensures proper response handling.
|
|
*
|
|
* @param err - HTTP error object
|
|
* @param req - Express request object
|
|
* @param res - Express response object
|
|
* @param next - Express next function
|
|
*/
|
|
export const finalErrorRouter:ErrorRequestHandler = async (err:HttpError, req, res, next) => {
|
|
|
|
const errorLogText:string = JSON.stringify({ message:err.message, name:err.name, stack:err.stack });
|
|
|
|
logError("server error", `${err.status}; n${errorLogText}`);
|
|
|
|
// `headersSent` will be TRUE if the router where the error occurred has already sent headers
|
|
// If we try to set them again, it will throw an error and CRASH THE SERVER - we must prevent this
|
|
if(!res.headersSent) {
|
|
res.status(err.status);
|
|
res.setHeader('Content-Type', "text/html");
|
|
res.end(`unhandled server error`);
|
|
} else {
|
|
// If `end` hasn't been called - call it to finish processing the request
|
|
// Otherwise the connection will remain open until timeout
|
|
if(!res.writableEnded) {
|
|
res.end();
|
|
}
|
|
}
|
|
}; |