Getting Started
Formatting Blade
Parsing Templates
Blade Documents
Compilation
Validation
Workspaces
The parser, and other related systems, are capable of reporting various errors under certain circumstances. For instance, the document parser will create errors if a @verbatim
directive is not paired correctly with an @endverbatim
directive.
Parser errors will not cause the parsing process to fail. Instead, parser errors are collected internally. We can retrieve them from a parser instance by calling the getErrors
method, which returns a Laravel collection instance. All parser errors will be an instance of Stillat\BladeParser\Errors\BladeError
.
Let us consider the following example, which contains invalid usage of the @verbatim
directive:
1<?php 2 3use Stillat\BladeParser\Errors\BladeError; 4use Stillat\BladeParser\Parser\DocumentParser; 5 6$template = <<<'BLADE' 7 8@verbatim 9 Some content10BLADE;11 12$parser = new DocumentParser;13$parser->parse($template);14 15/** @var BladeError $error */16foreach ($parser->getErrors() as $error) {17 echo $error->getErrorMessage();18}
Our example would produce the following output:
1[BLADE_P003001] Unexpected end of input while parsing verbatim on line 2
Each error produced by the library will have a resulting error code composed of a series of digits and characters following the BLADE_
prefix. The first character identifies which internal system produced the error, internally referred to as the "error family." The next three digits identify the context of the error, and the remaining three identify a specific type of error within a particular family of errors for a given context.
Error codes look similar to the following:
1[BLADE_PXXXYYY]
The P
character represents the error family, the XXX
section represents the error's context, and the YYY
section represents the specific error type.
Not all error messages will contain an error type, which is common for errors produced by the validation system.
The following error families may be reported:
Error Family | Code |
---|---|
Parser |
|
Validation |
|
Compiler |
|
Third-party extensions should not emit error codes in the parser family.
The following error contexts are available:
Error Context | Code |
---|---|
Echo |
|
Comment |
|
Verbatim |
|
Directive Arguments |
|
Blade PHP Block |
|
Component Tag |
|
Raw Echo |
|
Triple Echo |
|
PHP Short Open Tag ( |
|
PHP Open Tag ( |
|
Condition |
|
Literal |
|
Directive |
|
There are a variety of specific error types, each encoding a unique scenario:
Error Type | Code |
---|---|
Unknown |
|
Unexpected End of Input |
|
Unexpected Echo |
|
Unexpected Raw Echo |
|
Unexpected Triple Echo |
|
Unexpected Comment |
|
Unexpected Component Tag |
|
Unexpected Namespace Component Tag |
|
Unexpected Component Closing Tag |
|
Unexpected Namespace Component Closing Tag |
|
Unexpected PHP Short Open Tag |
|
Unexpected PHP Open Tag |
|
Unexpected PHP Closing Tag |
|
Validation Error |
|
Most validation errors reported under the validation (V
) error family will omit the trailing 013
when producing error messages.
Returns a formatted Blade error messages.
Error messages will contain a generated error code, a message indicating the failure as well as information such as the offending line number.
1<?php2 3public function getErrorMessage(): string;
Returns a Blade error code representing the current error instance.
1<?php2 3public function getErrorCode(): string;
Tests if the current error is considered a "fatal" error.
Fatal errors are those that are likely to cause issues when compiling the final PHP for a given template. Some parser generated errors are "warnings", and will not be reported as fatal errors. These exceptions are:
PHP tags not having their final closing ?>
tag.
PHP blocks not being matched to an @endphp
directive.
1<?php2 3public function isFatal(): bool;
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.