This commit is contained in:
seb
2026-10-01 22:17:17 +02:00
parent 6b58947064
commit ea51f30a8d
4 changed files with 55 additions and 8 deletions

View File

@@ -27,6 +27,7 @@ _No content_
| 401 | Unauthorized — `{"error":"…"}` | | 401 | Unauthorized — `{"error":"…"}` |
| 403 | Not authorized — `{"error":"…"}` | | 403 | Not authorized — `{"error":"…"}` |
| 404 | Delivery not found — `{"error":"…"}` | | 404 | Delivery not found — `{"error":"…"}` |
| 409 | Delivery in use in ledger — `{"error":"…"}` |
--- ---

View File

@@ -102,6 +102,7 @@ Updated delivery
| 401 | Unauthorized — `{"error":"…"}` | | 401 | Unauthorized — `{"error":"…"}` |
| 403 | Not authorized — `{"error":"…"}` | | 403 | Not authorized — `{"error":"…"}` |
| 404 | Delivery, supplier, or company not found — `{"error":"…"}` | | 404 | Delivery, supplier, or company not found — `{"error":"…"}` |
| 409 | Delivery lines already booked on ledger — `{"error":"…"}` |
--- ---

View File

@@ -20,8 +20,12 @@ function captureResponseBody(res) {
const originalSend = res.send.bind(res); const originalSend = res.send.bind(res);
res.json = (payload) => { res.json = (payload) => {
body = payload; let out = payload;
return originalJson(payload); if (res.statusCode >= 500 && payload && typeof payload === 'object' && 'error' in payload) {
out = { ...payload, error: 'Internal server error' };
}
body = out;
return originalJson(out);
}; };
res.send = (payload) => { res.send = (payload) => {

View File

@@ -172,6 +172,15 @@ function deliveryErrorStatus(error) {
if (error.message === 'Invalid JSON in "data" field') return 400; if (error.message === 'Invalid JSON in "data" field') return 400;
if (error.message === 'Only PDF or text files are accepted as attachments') return 400; if (error.message === 'Only PDF or text files are accepted as attachments') return 400;
if (error.message?.includes('father article') || error.message?.includes('A father article')) return 400; if (error.message?.includes('father article') || error.message?.includes('A father article')) return 400;
if (
error.message === 'Delivery in use in ledger' ||
error.message === 'Delivery lines already booked on ledger' ||
error.message?.includes('FOREIGN KEY constraint failed') ||
error.message?.includes('foreign key constraint') ||
error.message?.includes('REFERENCE constraint') ||
error.code === 'SQLITE_CONSTRAINT_FOREIGNKEY' ||
error.number === 547
) return 409;
return 500; return 500;
} }
@@ -348,7 +357,7 @@ export function register(app, basePath = '') {
res.locals.error = error; res.locals.error = error;
const status = deliveryErrorStatus(error); const status = deliveryErrorStatus(error);
if (status === 500) console.error('List deliveries failed:', error); if (status === 500) console.error('List deliveries failed:', error);
res.status(status).json({ error: error.message }); res.status(status).json({ error: status === 500 ? 'Internal server error' : error.message });
} }
}); });
@@ -399,7 +408,7 @@ export function register(app, basePath = '') {
console.error('[delivery diag] Failed to dump payload:', logErr.message); console.error('[delivery diag] Failed to dump payload:', logErr.message);
} }
} }
res.status(status).json({ error: error.message }); res.status(status).json({ error: status === 500 ? 'Internal server error' : error.message });
} }
}); });
@@ -550,6 +559,12 @@ export function register(app, basePath = '') {
* application/json: * application/json:
* schema: * schema:
* $ref: '#/components/schemas/Error' * $ref: '#/components/schemas/Error'
* 409:
* description: Delivery lines already booked on ledger
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
* delete: * delete:
* tags: [Delivery] * tags: [Delivery]
* summary: Delete delivery * summary: Delete delivery
@@ -591,6 +606,12 @@ export function register(app, basePath = '') {
* application/json: * application/json:
* schema: * schema:
* $ref: '#/components/schemas/Error' * $ref: '#/components/schemas/Error'
* 409:
* description: Delivery in use in ledger
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Error'
*/ */
app.get(`${basePath}/delivery/:id`, async (req, res) => { app.get(`${basePath}/delivery/:id`, async (req, res) => {
const selected = requireSelectedCompany(req); const selected = requireSelectedCompany(req);
@@ -610,7 +631,7 @@ export function register(app, basePath = '') {
res.locals.error = error; res.locals.error = error;
const status = deliveryErrorStatus(error); const status = deliveryErrorStatus(error);
if (status === 500) console.error('Get delivery failed:', error); if (status === 500) console.error('Get delivery failed:', error);
res.status(status).json({ error: error.message }); res.status(status).json({ error: status === 500 ? 'Internal server error' : error.message });
} }
}); });
@@ -656,7 +677,21 @@ export function register(app, basePath = '') {
if (status === 500) { if (status === 500) {
logStoreError('Update delivery failed', error); logStoreError('Update delivery failed', error);
} }
res.status(status).json({ error: error.message }); let errorMessage = error.message;
if (status === 500) {
errorMessage = 'Internal server error';
} else if (
status === 409 ||
error.message === 'Delivery lines already booked on ledger' ||
error.message?.includes('FOREIGN KEY constraint failed') ||
error.message?.includes('foreign key constraint') ||
error.message?.includes('REFERENCE constraint') ||
error.code === 'SQLITE_CONSTRAINT_FOREIGNKEY' ||
error.number === 547
) {
errorMessage = 'Delivery lines are already booked on the ledger and cannot be modified';
}
res.status(status).json({ error: errorMessage });
} }
}); });
@@ -678,7 +713,13 @@ export function register(app, basePath = '') {
res.locals.error = error; res.locals.error = error;
const status = deliveryErrorStatus(error); const status = deliveryErrorStatus(error);
if (status === 500) console.error('Delete delivery failed:', error); if (status === 500) console.error('Delete delivery failed:', error);
res.status(status).json({ error: error.message }); let errorMessage = error.message;
if (status === 500) {
errorMessage = 'Internal server error';
} else if (status === 409) {
errorMessage = 'Delivery in use in ledger';
}
res.status(status).json({ error: errorMessage });
} }
}); });
@@ -822,7 +863,7 @@ export function register(app, basePath = '') {
res.locals.error = error; res.locals.error = error;
const status = deliveryErrorStatus(error); const status = deliveryErrorStatus(error);
if (status === 500) console.error('Get delivery file failed:', error); if (status === 500) console.error('Get delivery file failed:', error);
res.status(status).json({ error: error.message }); res.status(status).json({ error: status === 500 ? 'Internal server error' : error.message });
} }
}; };