fix(#27): Implement uniform error handling with standardized format
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit is contained in:
parent
ad03a47ed8
commit
e4098e3e2a
3 changed files with 91 additions and 10 deletions
56
backend/src/utils/errorHandler.js
Normal file
56
backend/src/utils/errorHandler.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* Einheitliche Fehlerstruktur für die Anwendung
|
||||
*/
|
||||
export const createError = (code, message, details = null) => {
|
||||
const error = new Error(message);
|
||||
error.code = code;
|
||||
error.details = details;
|
||||
error.requestId = generateRequestId();
|
||||
return error;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generiert eine eindeutige Request-ID
|
||||
*/
|
||||
export const generateRequestId = () => {
|
||||
return 'req_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 5);
|
||||
};
|
||||
|
||||
/**
|
||||
* Sendet eine konsistente Fehlermeldung an den Client
|
||||
*/
|
||||
export const sendError = (res, error) => {
|
||||
const errorResponse = {
|
||||
code: error.code,
|
||||
message: error.message,
|
||||
details: error.details,
|
||||
requestId: error.requestId
|
||||
};
|
||||
|
||||
// Logge den Fehler für Debugging
|
||||
console.error('API Error:', errorResponse);
|
||||
|
||||
res.status(getStatusCode(error.code)).json(errorResponse);
|
||||
};
|
||||
|
||||
/**
|
||||
* Bestimmt den HTTP-Statuscode basierend auf dem Fehlercode
|
||||
*/
|
||||
export const getStatusCode = (code) => {
|
||||
switch (code) {
|
||||
case 'VALIDATION_ERROR':
|
||||
return 400;
|
||||
case 'AUTHENTICATION_ERROR':
|
||||
return 401;
|
||||
case 'AUTHORIZATION_ERROR':
|
||||
return 403;
|
||||
case 'NOT_FOUND_ERROR':
|
||||
return 404;
|
||||
case 'CONFLICT_ERROR':
|
||||
return 409;
|
||||
case 'DATABASE_ERROR':
|
||||
return 500;
|
||||
default:
|
||||
return 500;
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue