Search
Blade Parser

Blade Parser

Parser Errors

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 content
10BLADE;
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

#Interpreting Error Codes

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.

#Error Families

The following error families may be reported:

Error Family

Code

Parser

P

Validation

V

Compiler

C

Third-party extensions should not emit error codes in the parser family.

#Error Contexts

The following error contexts are available:

Error Context

Code

Echo

001

Comment

002

Verbatim

003

Directive Arguments

004

Blade PHP Block

005

Component Tag

006

Raw Echo

007

Triple Echo

008

PHP Short Open Tag (<?=)

009

PHP Open Tag (<?php)

010

Condition

011

Literal

012

Directive

013

#Error Types

There are a variety of specific error types, each encoding a unique scenario:

Error Type

Code

Unknown

000

Unexpected End of Input

001

Unexpected Echo

002

Unexpected Raw Echo

003

Unexpected Triple Echo

004

Unexpected Comment

005

Unexpected Component Tag

006

Unexpected Namespace Component Tag

007

Unexpected Component Closing Tag

008

Unexpected Namespace Component Closing Tag

009

Unexpected PHP Short Open Tag

010

Unexpected PHP Open Tag

011

Unexpected PHP Closing Tag

012

Validation Error

013

Most validation errors reported under the validation (V) error family will omit the trailing 013 when producing error messages.

#Available Methods

#getErrorMessage

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<?php
2 
3public function getErrorMessage(): string;

#getErrorCode

Returns a Blade error code representing the current error instance.

1<?php
2 
3public function getErrorCode(): string;

#isFatal

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<?php
2 
3public function isFatal(): bool;

Some absolutely amazing
people

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.