Skip to content

Commit 00a28a4

Browse files
authored
Merge pull request #23 from vaail/master
fix: catch convertAttribute exceptions
2 parents 2474809 + e730d4a commit 00a28a4

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

src/Collections/QueriesCollection.php

+38-25
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,45 @@ protected function convertAttribute($attribute)
118118
{
119119
try {
120120
return (string) $attribute;
121+
} catch (\Throwable $e) {
122+
return $this->handleConvertAttributeException($attribute);
121123
} catch (\Exception $e) {
122-
switch (true) {
123-
// Handle DateTime attribute pass.
124-
case $attribute instanceof \DateTime:
125-
return $attribute->format('Y-m-d H:i:s');
126-
127-
// Handle callables.
128-
case $attribute instanceof \Closure:
129-
return $this->convertAttribute($attribute());
130-
131-
// Handle arrays using json by default or print_r if error occurred.
132-
case is_array($attribute):
133-
$json = json_encode($attribute);
134-
135-
return json_last_error() === JSON_ERROR_NONE
136-
? $json
137-
: print_r($attribute);
138-
139-
// Handle all other object.
140-
case is_object($attribute):
141-
return get_class($attribute);
142-
143-
// For all unknown.
144-
default:
145-
return '?';
146-
}
124+
return $this->handleConvertAttributeException($attribute);
125+
}
126+
}
127+
128+
/**
129+
* Handle convert attribute exception and convert attribute to string.
130+
*
131+
* @param mixed $attribute
132+
* @return string
133+
*/
134+
protected function handleConvertAttributeException($attribute)
135+
{
136+
switch (true) {
137+
// Handle DateTime attribute pass.
138+
case $attribute instanceof \DateTime:
139+
return $attribute->format('Y-m-d H:i:s');
140+
141+
// Handle callables.
142+
case $attribute instanceof \Closure:
143+
return $this->convertAttribute($attribute());
144+
145+
// Handle arrays using json by default or print_r if error occurred.
146+
case is_array($attribute):
147+
$json = json_encode($attribute);
148+
149+
return json_last_error() === JSON_ERROR_NONE
150+
? $json
151+
: print_r($attribute);
152+
153+
// Handle all other object.
154+
case is_object($attribute):
155+
return get_class($attribute);
156+
157+
// For all unknown.
158+
default:
159+
return '?';
147160
}
148161
}
149162
}

0 commit comments

Comments
 (0)