|
@@ -1,8 +1,3 @@
|
|
|
-// ---------------------------------------------------------------------------
|
|
|
|
|
-// Ordner: lib/api
|
|
|
|
|
-// Datei: errors.js
|
|
|
|
|
-// Relativer Pfad: lib/api/errors.js
|
|
|
|
|
-// ---------------------------------------------------------------------------
|
|
|
|
|
/**
|
|
/**
|
|
|
* Central API response helpers for consistent JSON output and standardized errors.
|
|
* Central API response helpers for consistent JSON output and standardized errors.
|
|
|
*
|
|
*
|
|
@@ -176,23 +171,23 @@ export function toApiErrorResponse(err) {
|
|
|
* @returns {Function}
|
|
* @returns {Function}
|
|
|
*/
|
|
*/
|
|
|
export function withErrorHandling(handler, { logPrefix = "[api]" } = {}) {
|
|
export function withErrorHandling(handler, { logPrefix = "[api]" } = {}) {
|
|
|
|
|
+ const shouldLog = process.env.NODE_ENV !== "test";
|
|
|
|
|
+
|
|
|
return async (...args) => {
|
|
return async (...args) => {
|
|
|
try {
|
|
try {
|
|
|
return await handler(...args);
|
|
return await handler(...args);
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
- // Log unexpected errors always.
|
|
|
|
|
- // For ApiErrors, only log server-side failures (5xx) by default
|
|
|
|
|
- // to avoid noisy logs for client mistakes (4xx).
|
|
|
|
|
if (err instanceof ApiError) {
|
|
if (err instanceof ApiError) {
|
|
|
- if (err.status >= 500) {
|
|
|
|
|
|
|
+ if (shouldLog && err.status >= 500) {
|
|
|
console.error(`${logPrefix} ${err.code}:`, err.cause || err);
|
|
console.error(`${logPrefix} ${err.code}:`, err.cause || err);
|
|
|
}
|
|
}
|
|
|
return toApiErrorResponse(err);
|
|
return toApiErrorResponse(err);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- console.error(`${logPrefix} Unhandled error:`, err);
|
|
|
|
|
|
|
+ if (shouldLog) {
|
|
|
|
|
+ console.error(`${logPrefix} Unhandled error:`, err);
|
|
|
|
|
+ }
|
|
|
return toApiErrorResponse(err);
|
|
return toApiErrorResponse(err);
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
-// ---------------------------------------------------------------------------
|
|
|