diff --git a/backend/errors/ConflictError.js b/backend/errors/ConflictError.js new file mode 100644 index 0000000..380cefd --- /dev/null +++ b/backend/errors/ConflictError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class ConflictError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 409; // Conflict + } +} + +module.exports = ConflictError; diff --git a/backend/errors/CustomError.js b/backend/errors/CustomError.js new file mode 100644 index 0000000..2282968 --- /dev/null +++ b/backend/errors/CustomError.js @@ -0,0 +1,13 @@ +class CustomError extends Error { + constructor(message) { + super(message); + this.name = this.constructor.name; + Error.captureStackTrace(this, this.constructor); + } + + statusCode() { + return 500; // Default to 500 Internal Server Error + } +} + +module.exports = CustomError; \ No newline at end of file diff --git a/backend/errors/ForbiddenError.js b/backend/errors/ForbiddenError.js new file mode 100644 index 0000000..004774d --- /dev/null +++ b/backend/errors/ForbiddenError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class ForbiddenError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 403; // Forbidden + } +} + +module.exports = ForbiddenError; diff --git a/backend/errors/InternalServerError.js b/backend/errors/InternalServerError.js new file mode 100644 index 0000000..26b636d --- /dev/null +++ b/backend/errors/InternalServerError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class InternalServerError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 500; // Internal Server Error + } +} + +module.exports = InternalServerError; diff --git a/backend/errors/NotFoundError.js b/backend/errors/NotFoundError.js new file mode 100644 index 0000000..cd798df --- /dev/null +++ b/backend/errors/NotFoundError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class NotFoundError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 404; // Not Found + } +} + +module.exports = NotFoundError; \ No newline at end of file diff --git a/backend/errors/UnauthorizedError.js b/backend/errors/UnauthorizedError.js new file mode 100644 index 0000000..ed99a13 --- /dev/null +++ b/backend/errors/UnauthorizedError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class UnauthorizedError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 401; // Unauthorized + } +} + +module.exports = UnauthorizedError; \ No newline at end of file diff --git a/backend/errors/ValidationError.js b/backend/errors/ValidationError.js new file mode 100644 index 0000000..109698c --- /dev/null +++ b/backend/errors/ValidationError.js @@ -0,0 +1,13 @@ +const CustomError = require('./CustomError'); + +class ValidationError extends CustomError { + constructor(message) { + super(message); + } + + statusCode() { + return 400; // Bad Request + } +} + +module.exports = ValidationError; \ No newline at end of file diff --git a/backend/middleware/errorHandler.js b/backend/middleware/errorHandler.js new file mode 100644 index 0000000..49c4449 --- /dev/null +++ b/backend/middleware/errorHandler.js @@ -0,0 +1,9 @@ +const CustomError = require('../errors/CustomError'); + +module.exports = (err, req, res, next) => { + if (err instanceof CustomError) { + res.status(err.statusCode()).json({ message: err.message }); + } else { + res.status(500).json({ message: 'Internal server error' }); + } +}; \ No newline at end of file