File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 10
10
namespace Templado \Engine ;
11
11
12
12
use LibXMLError ;
13
+ use function sprintf ;
14
+ use function trim ;
13
15
14
16
final class ParsingException extends DocumentException {
15
17
/** @psalm-param list<LibXMLError> */
@@ -23,4 +25,21 @@ public function __construct(LibXMLError ...$errors) {
23
25
public function errors (): array {
24
26
return $ this ->errors ;
25
27
}
28
+
29
+ public function __toString (): string {
30
+ $ msg = $ this ->getMessage () . "\n" ;
31
+
32
+ foreach ($ this ->errors as $ error ) {
33
+ $ msg .= sprintf (
34
+ "[line: %d / column: %d] %s \n" ,
35
+ $ error ->line ,
36
+ $ error ->column ,
37
+ $ error ->message
38
+ );
39
+ }
40
+
41
+ $ msg .= $ this ->getTraceAsString ();
42
+
43
+ return $ msg ;
44
+ }
26
45
}
Original file line number Diff line number Diff line change @@ -14,4 +14,21 @@ public function testLibXMLErrorsCanBeRetrieved(): void {
14
14
$ this ->assertCount (1 , $ exception ->errors ());
15
15
$ this ->assertEquals ('Error(s) during parse ' , $ exception ->getMessage ());
16
16
}
17
+
18
+ public function testToStringAlsoReturnsLibXMLErrorsAsString (): void {
19
+ \libxml_use_internal_errors (true );
20
+ (new \DOMDocument ())->loadXML ('<?xml version="1.0" ?><parseerror> ' );
21
+ $ exception = new ParsingException (...libxml_get_errors ());
22
+ $ exceptionString = (string )$ exception ;
23
+
24
+ $ this ->assertStringContainsString (
25
+ $ exception ->getMessage () . "\n" ,
26
+ $ exceptionString
27
+ );
28
+
29
+ $ this ->assertStringContainsString (
30
+ '[line: 1 / column: 35] Premature end of data in tag parseerror line 1 ' ,
31
+ $ exceptionString
32
+ );
33
+ }
17
34
}
You can’t perform that action at this time.
0 commit comments