/*
 * Libre WebUI
 * Copyright (C) 2025 Kroonen AI, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export const errorHandler = (error, req, res, _next) => {
    console.error('Error:', error);
    // Default error response
    let statusCode = 500;
    let message = 'Internal server error';
    // Handle specific error types
    if (error && typeof error === 'object') {
        const err = error;
        if (err.name === 'ValidationError') {
            statusCode = 400;
            message = 'Validation error';
        }
        else if (err.name === 'UnauthorizedError') {
            statusCode = 401;
            message = 'Unauthorized';
        }
        else if (err.message) {
            message = err.message;
        }
    }
    res.status(statusCode).json({
        success: false,
        error: message,
        ...(process.env.NODE_ENV === 'development' && {
            stack: error && typeof error === 'object' && 'stack' in error
                ? error.stack
                : undefined,
        }),
    });
};
export const notFoundHandler = (req, res, _next) => {
    res.status(404).json({
        success: false,
        error: 'Route not found',
        path: req.originalUrl,
    });
};
export const requestLogger = (req, res, next) => {
    const start = Date.now();
    res.on('finish', () => {
        const duration = Date.now() - start;
        console.log(`${req.method} ${req.originalUrl} - ${res.statusCode} - ${duration}ms`);
    });
    next();
};
//# sourceMappingURL=index.js.map