Skip to content

Commit b736aed

Browse files
committed
__toString converts exceptions to errors
1 parent febcc41 commit b736aed

File tree

6 files changed

+30
-6
lines changed

6 files changed

+30
-6
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public function __construct(string $name = null, PhpNamespace $namespace = null)
8686

8787
public function __toString(): string
8888
{
89-
return (new Printer)->printClass($this, $this->namespace);
89+
try {
90+
return (new Printer)->printClass($this, $this->namespace);
91+
} catch (\Throwable $e) {
92+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
93+
}
9094
}
9195

9296

src/PhpGenerator/Closure.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public static function from(\Closure $closure): self
3737

3838
public function __toString(): string
3939
{
40-
return (new Printer)->printClosure($this);
40+
try {
41+
return (new Printer)->printClosure($this);
42+
} catch (\Throwable $e) {
43+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
44+
}
4145
}
4246

4347

src/PhpGenerator/GlobalFunction.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static function from(string $function): self
3535

3636
public function __toString(): string
3737
{
38-
return (new Printer)->printFunction($this);
38+
try {
39+
return (new Printer)->printFunction($this);
40+
} catch (\Throwable $e) {
41+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
42+
}
3943
}
4044
}

src/PhpGenerator/Method.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public static function from($method): self
5050

5151
public function __toString(): string
5252
{
53-
return (new Printer)->printMethod($this);
53+
try {
54+
return (new Printer)->printMethod($this);
55+
} catch (\Throwable $e) {
56+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
57+
}
5458
}
5559

5660

src/PhpGenerator/PhpFile.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function getNamespaces(): array
7676

7777
public function __toString(): string
7878
{
79-
return (new Printer)->printFile($this);
79+
try {
80+
return (new Printer)->printFile($this);
81+
} catch (\Throwable $e) {
82+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
83+
}
8084
}
8185
}

src/PhpGenerator/PhpNamespace.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ public function getClasses(): array
190190

191191
public function __toString(): string
192192
{
193-
return (new Printer)->printNamespace($this);
193+
try {
194+
return (new Printer)->printNamespace($this);
195+
} catch (\Throwable $e) {
196+
trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
197+
}
194198
}
195199
}

0 commit comments

Comments
 (0)