UNPKG

powr-sdk-api

Version:

Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.

40 lines (35 loc) 1.13 kB
"use strict"; const errorCatcher = fn => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(error => { // Extract error message and status code const message = error.message || 'Internal Server Error'; const statusCode = error.statusCode || 500; // Pass error details to res.error return res.error(message, statusCode, error); }); }; const errorHandler = (err, req, res, next) => { // Extract error message and status code const message = err.message || 'Internal Server Error'; const statusCode = err.statusCode || 500; // Check if res.error is available (from requestHandler) if (res.error) { return res.error(message, statusCode, err); } // Fallback error handling if res.error is not available console.error('Error:', err); const response = { success: false, message: message, requestId: req.requestId || 'unknown' }; // Include error details in development if (process.env.NODE_ENV === 'development') { response.error = err.stack; } return res.status(statusCode).json(response); }; module.exports = { errorCatcher, errorHandler };