Skip to content

Commit 049c6ca

Browse files
committed
fix(table): return 400 instead of 500 for malformed sort/filter input
1 parent 0f09310 commit 049c6ca

3 files changed

Lines changed: 44 additions & 7 deletions

File tree

apps/sim/app/api/table/[tableId]/rows/route.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
validateRowData,
3131
validateRowSize,
3232
} from '@/lib/table'
33-
import { buildFilterClause, buildSortClause } from '@/lib/table/sql'
33+
import { buildFilterClause, buildSortClause, TableQueryValidationError } from '@/lib/table/sql'
3434
import { accessError, checkAccess } from '@/app/api/table/utils'
3535

3636
const logger = createLogger('TableRowsAPI')
@@ -336,6 +336,10 @@ export const GET = withRouteHandler(
336336
return validationErrorResponse(error)
337337
}
338338

339+
if (error instanceof TableQueryValidationError) {
340+
return NextResponse.json({ error: error.message }, { status: 400 })
341+
}
342+
339343
logger.error(`[${requestId}] Error querying rows:`, error)
340344
return NextResponse.json({ error: 'Failed to query rows' }, { status: 500 })
341345
}
@@ -421,6 +425,10 @@ export const PUT = withRouteHandler(
421425
return validationErrorResponse(error)
422426
}
423427

428+
if (error instanceof TableQueryValidationError) {
429+
return NextResponse.json({ error: error.message }, { status: 400 })
430+
}
431+
424432
const errorMessage = toError(error).message
425433

426434
if (
@@ -520,6 +528,10 @@ export const DELETE = withRouteHandler(
520528
return validationErrorResponse(error)
521529
}
522530

531+
if (error instanceof TableQueryValidationError) {
532+
return NextResponse.json({ error: error.message }, { status: 400 })
533+
}
534+
523535
const errorMessage = toError(error).message
524536

525537
if (errorMessage.includes('Filter is required')) {

apps/sim/app/api/v1/tables/[tableId]/rows/route.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
validateRowData,
3131
validateRowSize,
3232
} from '@/lib/table'
33-
import { buildFilterClause, buildSortClause } from '@/lib/table/sql'
33+
import { buildFilterClause, buildSortClause, TableQueryValidationError } from '@/lib/table/sql'
3434
import { accessError, checkAccess } from '@/app/api/table/utils'
3535
import {
3636
checkRateLimit,
@@ -240,6 +240,10 @@ export const GET = withRouteHandler(async (request: NextRequest, context: TableR
240240
const validationResponse = validationErrorResponseFromError(error)
241241
if (validationResponse) return validationResponse
242242

243+
if (error instanceof TableQueryValidationError) {
244+
return NextResponse.json({ error: error.message }, { status: 400 })
245+
}
246+
243247
logger.error(`[${requestId}] Error querying rows:`, error)
244248
return NextResponse.json({ error: 'Failed to query rows' }, { status: 500 })
245249
}
@@ -407,6 +411,10 @@ export const PUT = withRouteHandler(async (request: NextRequest, context: TableR
407411
const validationResponse = validationErrorResponseFromError(error)
408412
if (validationResponse) return validationResponse
409413

414+
if (error instanceof TableQueryValidationError) {
415+
return NextResponse.json({ error: error.message }, { status: 400 })
416+
}
417+
410418
const errorMessage = toError(error).message
411419

412420
if (
@@ -500,6 +508,10 @@ export const DELETE = withRouteHandler(
500508
const validationResponse = validationErrorResponseFromError(error)
501509
if (validationResponse) return validationResponse
502510

511+
if (error instanceof TableQueryValidationError) {
512+
return NextResponse.json({ error: error.message }, { status: 400 })
513+
}
514+
503515
const errorMessage = toError(error).message
504516

505517
if (errorMessage.includes('Filter is required')) {

apps/sim/lib/table/sql.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import { sql } from 'drizzle-orm'
1010
import { NAME_PATTERN } from './constants'
1111
import type { ColumnDefinition, ConditionOperators, Filter, JsonValue, Sort } from './types'
1212

13+
/**
14+
* Error thrown when caller-supplied filter or sort input is malformed.
15+
* Routes should map this to HTTP 400 with the message preserved.
16+
*/
17+
export class TableQueryValidationError extends Error {
18+
constructor(message: string) {
19+
super(message)
20+
this.name = 'TableQueryValidationError'
21+
}
22+
}
23+
1324
/**
1425
* Whitelist of allowed operators for query filtering.
1526
* Only these operators can be used in filter conditions.
@@ -133,7 +144,9 @@ export function buildSortClause(
133144
validateFieldName(field)
134145

135146
if (direction !== 'asc' && direction !== 'desc') {
136-
throw new Error(`Invalid sort direction "${direction}". Must be "asc" or "desc".`)
147+
throw new TableQueryValidationError(
148+
`Invalid sort direction "${direction}". Must be "asc" or "desc".`
149+
)
137150
}
138151

139152
const columnType = columnTypeMap.get(field)
@@ -152,11 +165,11 @@ export function buildSortClause(
152165
*/
153166
function validateFieldName(field: string): void {
154167
if (!field || typeof field !== 'string') {
155-
throw new Error('Field name must be a non-empty string')
168+
throw new TableQueryValidationError('Field name must be a non-empty string')
156169
}
157170

158171
if (!NAME_PATTERN.test(field)) {
159-
throw new Error(
172+
throw new TableQueryValidationError(
160173
`Invalid field name "${field}". Field names must start with a letter or underscore, followed by alphanumeric characters or underscores.`
161174
)
162175
}
@@ -170,7 +183,7 @@ function validateFieldName(field: string): void {
170183
*/
171184
function validateOperator(operator: string): void {
172185
if (!ALLOWED_OPERATORS.has(operator)) {
173-
throw new Error(
186+
throw new TableQueryValidationError(
174187
`Invalid operator "${operator}". Allowed operators: ${Array.from(ALLOWED_OPERATORS).join(', ')}`
175188
)
176189
}
@@ -261,7 +274,7 @@ function buildFieldCondition(
261274

262275
default:
263276
// This should never happen due to validateOperator, but added for completeness
264-
throw new Error(`Unsupported operator: ${op}`)
277+
throw new TableQueryValidationError(`Unsupported operator: ${op}`)
265278
}
266279
}
267280
} else {

0 commit comments

Comments
 (0)