Skip to main content

Overview

The DatabaseErrorParser class parses database errors from different engines (PostgreSQL, MySQL, SQL Server, MongoDB) and provides human-friendly error messages with actionable suggestions. It covers SELECT, INSERT, UPDATE, and DELETE errors including constraint violations, syntax errors, and data type issues.

Class Reference

Methods

parse()

Parse database exception and return structured error information.
\Throwable
required
The database exception to parse (PDOException, QueryException, etc.)
array
Structured error information with the following keys:

Example Usage


toExceptionError()

Convert parsed error to a DatabaseErrorParserException with JSON response format.
array
required
The parsed error array from parse() method
int
default:"400"
HTTP status code for the exception
DatabaseErrorParserException
Exception with JSON-encoded error details in the message

Example Usage


toPlainText()

Format parsed error as plain text for logging.
array
required
The parsed error array from parse() method
string
Formatted plain text error message

Example Usage


Supported Error Types

SELECT Errors

  • undefined_column / unknown_column - Column does not exist
  • undefined_table / unknown_table - Table does not exist
  • syntax_error - SQL syntax errors
  • duplicate_column - Column specified multiple times

INSERT/UPDATE Errors

  • unique_violation / duplicate_entry - Unique constraint violation
  • foreign_key_violation - Foreign key constraint violation
  • not_null_violation - NULL value in NOT NULL column
  • check_violation - Check constraint violation
  • invalid_text_representation - Invalid data type format
  • data_too_long - String data exceeds column length
  • numeric_value_out_of_range - Number exceeds allowed range

DELETE Errors

  • restrict_violation - Cannot delete referenced record
  • cannot_delete_parent - Foreign key prevents deletion

MongoDB Errors

  • duplicate_key - Duplicate key in unique index
  • validation_failed - Document validation failed
  • document_too_large - Document exceeds 16MB limit
  • immutable_field - Cannot update immutable field
  • unknown_operator - Invalid MongoDB operator

Error Type Mappings

PostgreSQL (SQLSTATE Codes)

  • 42703 - undefined_column
  • 42P01 - undefined_table
  • 42601 - syntax_error
  • 23505 - unique_violation
  • 23503 - foreign_key_violation
  • 23502 - not_null_violation
  • 23514 - check_violation
  • 22P02 - invalid_text_representation
  • 22001 - string_data_right_truncation
  • 22003 - numeric_value_out_of_range
  • 22012 - division_by_zero

MySQL (SQLSTATE Codes)

  • 42S22 - unknown_column
  • 42S02 - unknown_table
  • 42000 - syntax_error
  • 23000 - duplicate_entry / foreign_key_constraint / column_cannot_be_null
  • 22001 - data_too_long
  • 22007 - truncated_incorrect_value
  • 22003 - out_of_range
  • 22012 - division_by_zero

SQL Server (SQLSTATE Codes)

  • 42S22 - invalid_column
  • 42S02 - invalid_object
  • 42000 - syntax_error
  • 23000 - duplicate_key / foreign_key_conflict / null_constraint
  • 22001 - string_truncation
  • 22000 - conversion_failed
  • 22003 - arithmetic_overflow
  • 22012 - divide_by_zero

Complete Example


DatabaseErrorParserException

The exception thrown by toExceptionError() includes a helper method:

getDatabaseErrorParsed()

Retrieve the parsed error data from the exception.