UNPKG

@defikitdotnet/education-module-ai

Version:
66 lines (65 loc) 2.15 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiError = void 0; exports.errorHandler = errorHandler; /** * Custom error class for API errors */ var ApiError = /** @class */ (function (_super) { __extends(ApiError, _super); function ApiError(message, statusCode) { if (statusCode === void 0) { statusCode = 500; } var _this = _super.call(this, message) || this; _this.statusCode = statusCode; _this.name = "ApiError"; return _this; } return ApiError; }(Error)); exports.ApiError = ApiError; /** * Centralized error handling middleware */ function errorHandler(err, req, res, next) { console.error("Error:", err); // Handle custom API errors if (err instanceof ApiError) { res.status(err.statusCode).json({ error: err.message, }); return; } // Handle JWT errors if (err.name === "JsonWebTokenError" || err.name === "TokenExpiredError") { res.status(401).json({ error: "Invalid or expired token", }); return; } // Handle validation errors if (err.name === "ValidationError") { res.status(400).json({ error: err.message, }); return; } // Handle other errors res.status(500).json({ error: "Internal Server Error", }); }