* Maps known service-layer error messages onto HTTP responses; falls through * to a 500 with a generic message for anything unrecognized. The three * group-route handlers all surface the same error shapes from * `addWorkflowGroup` / `updateWorkflowGroup` / `deleteWorkflowGroup`, so they * share
(error: unknown, fallbackMessage: string)
| 30 | * share this mapper instead of repeating the if-chain three times. |
| 31 | */ |
| 32 | function mapWorkflowGroupError(error: unknown, fallbackMessage: string): NextResponse { |
| 33 | if (error instanceof Error) { |
| 34 | const msg = error.message |
| 35 | if (msg === 'Table not found' || msg.includes('not found')) { |
| 36 | return NextResponse.json({ error: msg }, { status: 404 }) |
| 37 | } |
| 38 | if ( |
| 39 | msg.includes('Schema validation') || |
| 40 | msg.includes('Missing column definition') || |
| 41 | msg.includes('already exists') || |
| 42 | msg.includes('exceed') |
| 43 | ) { |
| 44 | return NextResponse.json({ error: msg }, { status: 400 }) |
| 45 | } |
| 46 | } |
| 47 | logger.error(fallbackMessage, error) |
| 48 | return NextResponse.json({ error: fallbackMessage }, { status: 500 }) |
| 49 | } |
| 50 | |
| 51 | /** POST /api/table/[tableId]/groups — create a workflow group + its output columns. */ |
| 52 | export const POST = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => { |