Overview
TheDatabaseErrorParser 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() methodint
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() methodstring
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_column42P01- undefined_table42601- syntax_error23505- unique_violation23503- foreign_key_violation23502- not_null_violation23514- check_violation22P02- invalid_text_representation22001- string_data_right_truncation22003- numeric_value_out_of_range22012- division_by_zero
MySQL (SQLSTATE Codes)
42S22- unknown_column42S02- unknown_table42000- syntax_error23000- duplicate_entry / foreign_key_constraint / column_cannot_be_null22001- data_too_long22007- truncated_incorrect_value22003- out_of_range22012- division_by_zero
SQL Server (SQLSTATE Codes)
42S22- invalid_column42S02- invalid_object42000- syntax_error23000- duplicate_key / foreign_key_conflict / null_constraint22001- string_truncation22000- conversion_failed22003- arithmetic_overflow22012- divide_by_zero
Complete Example
DatabaseErrorParserException
The exception thrown bytoExceptionError() includes a helper method: