Skip to content
This repository was archived by the owner on May 12, 2022. It is now read-only.

Commit 326d948

Browse files
author
Nik Barham
committed
Make Exceptions json-able for APIs
1 parent 7641a71 commit 326d948

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Automatorm/Exception/BaseException.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Automatorm\Exception;
33

4-
class BaseException extends \Exception
4+
class BaseException extends \Exception implements \JsonSerializable
55
{
66
protected $data;
77

@@ -15,4 +15,26 @@ public function getData()
1515
{
1616
return $this->data;
1717
}
18+
19+
public function jsonSerialize()
20+
{
21+
$response = [
22+
'code' => $this->getCode(),
23+
'message' => $this->getMessage(),
24+
'file' => $this->getFile(),
25+
'line' => $this->getLine(),
26+
'trace' => $this->getTrace(),
27+
'data' => $this->getData()
28+
];
29+
30+
if ($previous = $this->getPrevious()) {
31+
if ($previous instanceof \JsonSerializable) {
32+
$response['previous'] = $previous;
33+
} else {
34+
$response['previous'] = $previous->getMessage();
35+
}
36+
}
37+
38+
return $response;
39+
}
1840
}

0 commit comments

Comments
 (0)