Error handling
Invalid SQL throws a ParseError with the line, column, and surrounding text. Strictness is controlled by ErrorLevel.
Error levels
| Level |
Behavior |
immediate (default) |
Throw on the first error |
raise |
Collect, then throw once with every error |
warn |
Log via Laravel's logger and carry on |
ignore |
Silently carry on, best-effort tree |
Set globally in config:
'error_level' => env('SQL_PARSER_ERROR_LEVEL', 'immediate'),
Or per call:
use Kwasii\LaravelSqlParser\Errors\ErrorLevel;
Sql::parse($sql, 'mysql', ErrorLevel::Ignore);
Sql::transpile($sql, 'mysql', 'postgres', ['error_level' => ErrorLevel::Warn]);
Exception types
| Class |
When |
ParseError |
SQL cannot be parsed; includes line, column, context |
TokenError |
Tokenizer hits invalid input |
UnsupportedError |
Construct is valid but unsupported for the dialect |
OptimizeError |
Optimizer cannot rewrite the query |
SchemaError |
Schema is missing tables/columns the optimizer needs |
SqlParserException |
Base class for all of the above |
use Kwasii\LaravelSqlParser\Errors\ParseError;
use Kwasii\LaravelSqlParser\Errors\SqlParserException;
try {
Sql::parseOne('SELECT FROM WHERE', 'mysql');
} catch (ParseError $e) {
report($e); // line, column, snippet included
} catch (SqlParserException $e) {
report($e); // any other parser failure
}
Tips
- Validate user-supplied SQL with
parse() before running it — catch ParseError and show the message.
- Use
warn in migrations and bulk jobs where one bad statement should not abort the batch.
- Unknown dialect names throw PHP's
InvalidArgumentException (not a parser exception) — validate dialect input separately.