Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/knex/src/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ export function errorHandler(error: any) {
} else if (typeof error.code === 'string' && error.severity && error.routine) {
// NOTE: Error codes taken from
// https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html
// Omit query information
const messages = (error.message || '').split('-')

error.message = messages[messages.length - 1]
// Omit query information. Knex prefixes the driver message with the SQL
// as "<sql> - <pg message>", so keep only the text after the last " - ".
const rawMessage: string = error.message || ''
const separator = rawMessage.lastIndexOf(' - ')
const message = separator === -1 ? rawMessage : rawMessage.slice(separator + 3) // if no separator, use as is, else move pointer to the right of the separator

switch (error.code.slice(0, 2)) {
case '22':
Expand Down
64 changes: 63 additions & 1 deletion packages/knex/test/error-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert'
import { errorHandler } from '../src'
import { errorHandler, ERROR } from '../src'

describe('Knex Error handler', () => {
it('sqlState', () => {
Expand Down Expand Up @@ -62,4 +62,66 @@ describe('Knex Error handler', () => {
name: 'GeneralError'
})
})

it('postgresqlError omits query information from the Feathers error', () => {
const pgError = (code: string, message: string) => ({
code,
message,
severity: 'ERROR',
routine: 'ExecConstraints'
})

assert.throws(
() =>
errorHandler(
pgError(
'22P02',
'select "users".* from "users" where "id" = $1 limit $2 - invalid input syntax for type uuid: "1"'
)
),
{
name: 'NotFound',
message: 'invalid input syntax for type uuid: "1"'
}
)

assert.throws(
() =>
errorHandler(
pgError(
'23505',
'insert into "users" ("email") values ($1) returning * - duplicate key value violates unique constraint "users_email_unique"'
)
),
{
name: 'BadRequest',
message: 'duplicate key value violates unique constraint "users_email_unique"'
}
)

// A " - " inside the SQL and a hyphen in the error text are both handled
assert.throws(
() => errorHandler(pgError('42703', 'select "a" - "b" from "t" - column "non-existent" does not exist')),
{
name: 'Unprocessable',
message: 'column "non-existent" does not exist'
}
)
})

it('postgresqlError keeps the raw error for server-side handling', () => {
try {
errorHandler({
code: '23505',
message: 'insert into "users" ("email") values ($1) - duplicate key value',
severity: 'ERROR',
routine: 'ExecConstraints',
constraint: 'users_email_unique'
})
assert.fail('should have thrown')
} catch (error: any) {
assert.strictEqual(error[ERROR].code, '23505')
assert.strictEqual(error[ERROR].constraint, 'users_email_unique')
}
})
})