Skip to content

Commit 1981037

Browse files
committed
fix: error response when its not json
1 parent 5297d13 commit 1981037

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/Services/BillingoApiV3Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function createResponse(string $methodName, array $params, bool $metho
125125
$params
126126
);
127127
} catch (\Throwable $th) {
128-
echo ($this->error($th->getMessage())->getJson());
128+
echo ($this->error($th->getMessage())->response());
129129
exit;
130130
}
131131

src/Traits/ProcessErrorsTrait.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ public function error(string $error): self
2727
}
2828

2929
/**
30-
* Get error message json response
30+
* Get error message response
3131
*
3232
* @return string
3333
*/
34-
public function getJson(): string
34+
public function response(): string
3535
{
36-
return \json_encode(\json_decode($this->getJsonFromErrorString(), true), JSON_FORCE_OBJECT);
36+
if ($this->isJson($this->error)) {
37+
return \json_encode(\json_decode($this->getJsonFromErrorString(), true), JSON_FORCE_OBJECT);
38+
}
39+
40+
return $this->error;
3741
}
3842

3943
/**
@@ -49,4 +53,17 @@ public function getJsonFromErrorString(): string
4953
strpos($this->error, "}")
5054
);
5155
}
56+
57+
/**
58+
* Check that error string is json encoded
59+
*
60+
* @param string $error
61+
*
62+
* @return bool
63+
*/
64+
public function isJson(string $error): bool
65+
{
66+
json_decode($error);
67+
return json_last_error() === JSON_ERROR_NONE;
68+
}
5269
}

0 commit comments

Comments
 (0)