Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed UniqueViolationError bug for mysql clients #141

Merged
merged 4 commits into from
Mar 2, 2021
Merged
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
16 changes: 11 additions & 5 deletions src/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ export default function errorHandler (error) {
} else if (error instanceof NotFoundError) {
feathersError = new errors.NotFound(message);
} else if (error instanceof UniqueViolationError) {
feathersError = new errors.Conflict(`${error.columns.join(', ')} must be unique`, {
columns: error.columns,
table: error.table,
constraint: error.constraint
});
if (error.client === 'mysql') {
feathersError = new errors.Conflict(error.nativeError.sqlMessage, {
constraint: error.constraint
});
} else {
feathersError = new errors.Conflict(`${error.columns.join(', ')} must be unique`, {
columns: error.columns,
table: error.table,
constraint: error.constraint
});
}
} else if (error instanceof NotNullViolationError) {
feathersError = new errors.BadRequest(`${error.column} must not be null`, {
column: error.column,
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,18 @@ describe('Feathers Objection Service', () => {
expect(errorHandler.bind(null, error)).to.throw(errors.Conflict);
});

it('UniqueViolation error mysql', () => {
const error = new UniqueViolationError({
nativeError: { sqlMessage: 'test' },
client: 'mysql',
table: undefined,
columns: undefined,
constraint: 'test_constraint'
});

expect(errorHandler.bind(null, error)).to.throw(error.Conflict, 'test');
});

it('ConstraintViolation error', () => {
const error = new ConstraintViolationError({
nativeError: new Error(),
Expand Down