Skip to main content

SQL Database Exceptions

Unified Errors Handler can parse SQL Exceptions and mapped them to unified structure.

These feature compatible and tested with:

  1. MYSQL with TypeORM
  2. Postgres with TypeORM
  3. MYSQL with Sequelize
  4. Postgres with Sequelize
  5. MYSQL with ObjectionJS
  6. Postgres with ObjectionJS
  7. MYSQL with KnexJS
  8. 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

  1. UniqueViolationException

  • Status code - 400
// output
[
{
fields: ['name'],
code: 'DATA_ALREADY_EXIST',
message: 'name already exist',
},
]
  1. 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',
},
]
  1. NotNullViolationException

  • Status code - 400
// output
[
{
fields: ['age'],
code: 'INVALID_DATA',
message: 'age is invalid',
details: { reason: 'age must not be NULL' },
},
]
  1. CheckViolationException

  • Status code - 400
  • Example - Invalid enum value
// output
[{
code: 'INVALID_VALUES',
message: 'Invalid Values',
details: {
constraint: 'user_gender_check',
},
}]
  1. OutOfRangeViolationException

  • Status code - 400
  • Example - numeric value out of range
// output
[{
{
code: 'OUT_OF_RANGE',
message: 'Out of range',
},
}]