fix(#27): Implement uniform error handling with standardized format
Some checks are pending
Docker Test / test (push) Waiting to run

This commit is contained in:
OpenClaw 2026-03-07 00:17:34 +00:00
parent ad03a47ed8
commit e4098e3e2a
3 changed files with 91 additions and 10 deletions

View file

@ -2,6 +2,7 @@ import { Router } from 'express';
import { z } from 'zod';
import { pool } from '../db/connection.js';
import { requireAuth } from '../middleware/auth.js';
import { createError, sendError } from '../utils/errorHandler.js';
const router = Router();
@ -19,7 +20,7 @@ const getDealParticipants = async (dealId) => {
return rows[0] || null;
} catch (error) {
console.error('Database error while fetching deal participants for dealId:', dealId, error);
throw new Error('Database error while fetching deal participants');
throw createError('DATABASE_ERROR', 'Database error while fetching deal participants');
}
};
@ -31,20 +32,20 @@ router.post('/request', requireAuth, async (req, res) => {
}).safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: 'Invalid input data', details: parsed.error.flatten() });
throw createError('VALIDATION_ERROR', 'Invalid input data', parsed.error.flatten());
}
const { dealId, targetUserId } = parsed.data;
const deal = await getDealParticipants(dealId);
if (!deal) {
return res.status(404).json({ error: 'Deal not found' });
throw createError('NOT_FOUND_ERROR', 'Deal not found');
}
const participants = [deal.requester_id, deal.helper_id];
if (!participants.includes(req.user.userId) || !participants.includes(targetUserId) || req.user.userId === targetUserId) {
return res.status(403).json({ error: 'Forbidden' });
throw createError('AUTHORIZATION_ERROR', 'Forbidden');
}
const [existing] = await pool.query(
@ -54,7 +55,7 @@ router.post('/request', requireAuth, async (req, res) => {
);
if (existing.length) {
return res.status(409).json({ error: 'Request already exists' });
throw createError('CONFLICT_ERROR', 'Request already exists');
}
const [result] = await pool.query(
@ -66,7 +67,7 @@ router.post('/request', requireAuth, async (req, res) => {
res.status(201).json({ id: result.insertId });
} catch (error) {
console.error('Error in contacts request route:', error);
res.status(500).json({ error: 'Internal server error' });
sendError(res, error);
}
});
@ -78,7 +79,7 @@ router.post('/respond', requireAuth, async (req, res) => {
}).safeParse(req.body);
if (!parsed.success) {
return res.status(400).json({ error: 'Invalid input data', details: parsed.error.flatten() });
throw createError('VALIDATION_ERROR', 'Invalid input data', parsed.error.flatten());
}
const { requestId, accept } = parsed.data;
@ -90,11 +91,11 @@ router.post('/respond', requireAuth, async (req, res) => {
const row = rows[0];
if (!row) {
return res.status(404).json({ error: 'Request not found' });
throw createError('NOT_FOUND_ERROR', 'Request not found');
}
if (row.target_id !== req.user.userId) {
return res.status(403).json({ error: 'Forbidden' });
throw createError('AUTHORIZATION_ERROR', 'Forbidden');
}
if (accept) {
@ -106,7 +107,7 @@ router.post('/respond', requireAuth, async (req, res) => {
res.json({ status: 'rejected' });
} catch (error) {
console.error('Error in contacts respond route:', error);
res.status(500).json({ error: 'Internal server error' });
sendError(res, error);
}
});