SQL Database Exceptions
Unified Errors Handler can parse SQL Exceptions and mapped them to unified structure.
These feature compatible and tested with:
- MYSQL with TypeORM
- Postgres with TypeORM
- MYSQL with Sequelize
- Postgres with Sequelize
- MYSQL with ObjectionJS
- Postgres with ObjectionJS
- MYSQL with KnexJS
- Postgres with KnexJS
Important Note
You Must configure options to (enable/disable) parsing for database errors (depends on your ORM) this is disabled by default. See Supported Options
Exceptions
-
UniqueViolationException
- Status code - 400
// output
[
{
fields: ['name'],
code: 'DATA_ALREADY_EXIST',
message: 'name already exist',
},
]
-
ForeignKeyViolationException
- Status code - 400
// output
// foreign key is not exist as primary key in another table
// trying insert value with invalid foreign key
[
code: 'INVALID_DATA',
message: 'Invalid data',
details: {
reason: 'violates foreign key constraint',
constraint: 'pet_user_id_foreign',
},
]
// foreign key has reference in another table
[
code: 'DATA_HAS_REFERENCE',
message: 'Data has reference',
details: {
reason: 'violates foreign key constraint',
constraint: 'pet_user_id_foreign',
},
]
-
NotNullViolationException
- Status code - 400
// output
[
{
fields: ['age'],
code: 'INVALID_DATA',
message: 'age is invalid',
details: { reason: 'age must not be NULL' },
},
]
-
CheckViolationException
- Status code - 400
- Example - Invalid enum value
// output
[{
code: 'INVALID_VALUES',
message: 'Invalid Values',
details: {
constraint: 'user_gender_check',
},
}]
-
OutOfRangeViolationException
- Status code - 400
- Example - numeric value out of range
// output
[{
{
code: 'OUT_OF_RANGE',
message: 'Out of range',
},
}]