Skip to content

Commit 15ee5c7

Browse files
committed
enhance error construct
1 parent d2f6054 commit 15ee5c7

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/Dtos/TraitDtos.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Spotlibs\PhpLib\Dtos;
1717

18+
use Exception;
1819
use ReflectionClass;
1920

2021
/**
@@ -44,15 +45,23 @@ public function __construct(array $data = [])
4445
$this->aliases = [];
4546
}
4647
$reflector = new ReflectionClass(static::class);
47-
foreach ($data as $key => $value) {
48-
$temp_key = array_search($key, $this->aliases, true);
49-
$key = is_string($temp_key) ? $temp_key : $key;
50-
if (property_exists($this, $key)) {
51-
if (is_array($value)) {
52-
$this->convertArray($reflector, $key, $value);
48+
try {
49+
foreach ($data as $key => $value) {
50+
$temp_key = array_search($key, $this->aliases, true);
51+
$key = is_string($temp_key) ? $temp_key : $key;
52+
if (property_exists($this, $key)) {
53+
if (is_array($value)) {
54+
$this->convertArray($reflector, $key, $value);
55+
}
56+
$this->{$key} = $value;
5357
}
54-
$this->{$key} = $value;
5558
}
59+
} catch (\Throwable $th) {
60+
if (substr($th->getMessage(), 0, 30) == "property_exists(): Argument #2") {
61+
$jsonEncoded = json_encode($data);
62+
throw new Exception($th->getMessage() . '. Error constructing this array to DTO: ' . $jsonEncoded);
63+
}
64+
throw $th;
5665
}
5766
}
5867

0 commit comments

Comments
 (0)