Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 359d0e0

Browse files
Tr3ffeldekelev
andauthored
fixed UniqueViolationError bug for mysql clients (#141)
* fixed UniqueViolationError bug when using mysql * fixed a typo and added a test * Update index.test.js * Update error-handler.js Co-authored-by: Dekel Barzilay <[email protected]>
1 parent e02c769 commit 359d0e0

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/error-handler.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ export default function errorHandler (error) {
3939
} else if (error instanceof NotFoundError) {
4040
feathersError = new errors.NotFound(message);
4141
} else if (error instanceof UniqueViolationError) {
42-
feathersError = new errors.Conflict(`${error.columns.join(', ')} must be unique`, {
43-
columns: error.columns,
44-
table: error.table,
45-
constraint: error.constraint
46-
});
42+
if (error.client === 'mysql') {
43+
feathersError = new errors.Conflict(error.nativeError.sqlMessage, {
44+
constraint: error.constraint
45+
});
46+
} else {
47+
feathersError = new errors.Conflict(`${error.columns.join(', ')} must be unique`, {
48+
columns: error.columns,
49+
table: error.table,
50+
constraint: error.constraint
51+
});
52+
}
4753
} else if (error instanceof NotNullViolationError) {
4854
feathersError = new errors.BadRequest(`${error.column} must not be null`, {
4955
column: error.column,

test/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ describe('Feathers Objection Service', () => {
373373
expect(errorHandler.bind(null, error)).to.throw(errors.Conflict);
374374
});
375375

376+
it('UniqueViolation error mysql', () => {
377+
const error = new UniqueViolationError({
378+
nativeError: { sqlMessage: 'test' },
379+
client: 'mysql',
380+
table: undefined,
381+
columns: undefined,
382+
constraint: 'test_constraint'
383+
});
384+
385+
expect(errorHandler.bind(null, error)).to.throw(error.Conflict, 'test');
386+
});
387+
376388
it('ConstraintViolation error', () => {
377389
const error = new ConstraintViolationError({
378390
nativeError: new Error(),

0 commit comments

Comments
 (0)