72 lines
1.7 KiB
JavaScript
72 lines
1.7 KiB
JavaScript
class ErrorHandler {
|
|
/**
|
|
* Error in HTTP Response umwandeln
|
|
*/
|
|
handleError(error, res) {
|
|
console.error('Error:', error);
|
|
|
|
// Validation Errors
|
|
if (error.message.startsWith('VALIDATION_ERROR:')) {
|
|
const message = error.message.replace('VALIDATION_ERROR: ', '');
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: message,
|
|
type: 'VALIDATION_ERROR'
|
|
});
|
|
}
|
|
|
|
// Business Logic Errors
|
|
switch (error.message) {
|
|
case 'USER_NOT_FOUND':
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Benutzer nicht gefunden',
|
|
type: 'NOT_FOUND'
|
|
});
|
|
|
|
case 'INVALID_CURRENT_PASSWORD':
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: 'Aktuelles Passwort ist falsch',
|
|
type: 'INVALID_PASSWORD'
|
|
});
|
|
|
|
case 'EMAIL_ALREADY_EXISTS':
|
|
return res.status(409).json({
|
|
success: false,
|
|
message: 'E-Mail-Adresse bereits vorhanden',
|
|
type: 'DUPLICATE_EMAIL'
|
|
});
|
|
|
|
default:
|
|
return res.status(500).json({
|
|
success: false,
|
|
message: 'Ein interner Fehler ist aufgetreten',
|
|
type: 'INTERNAL_ERROR'
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Success Response erstellen
|
|
*/
|
|
successResponse(res, data, message = 'Erfolgreich', statusCode = 200) {
|
|
return res.status(statusCode).json({
|
|
success: true,
|
|
message: message,
|
|
data: data
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Async Error Wrapper für Controller
|
|
*/
|
|
asyncHandler(fn) {
|
|
return (req, res, next) => {
|
|
Promise.resolve(fn(req, res, next)).catch(next);
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = new ErrorHandler();
|