Propagate error details to REST API responses

[MAILPOET-4659]
This commit is contained in:
Jan Jakes
2022-10-03 15:55:02 +02:00
committed by Jan Jakeš
parent 6638707282
commit 14eb8db540
2 changed files with 7 additions and 3 deletions

View File

@ -80,7 +80,7 @@ class API {
private function convertToErrorResponse(Throwable $e): ErrorResponse { private function convertToErrorResponse(Throwable $e): ErrorResponse {
$response = $e instanceof Exception $response = $e instanceof Exception
? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode()) ? new ErrorResponse($e->getStatusCode(), $e->getMessage(), $e->getErrorCode(), $e->getErrors())
: new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error'); : new ErrorResponse(500, __('An unknown error occurred.', 'mailpoet'), 'mailpoet_automation_unknown_error');
if ($response->get_status() >= 500) { if ($response->get_status() >= 500) {

View File

@ -6,13 +6,17 @@ class ErrorResponse extends Response {
public function __construct( public function __construct(
int $status, int $status,
string $message, string $message,
string $code string $code,
array $errors = []
) { ) {
parent::__construct(null, $status); parent::__construct(null, $status);
$this->set_data([ $this->set_data([
'code' => $code, 'code' => $code,
'message' => $message, 'message' => $message,
'data' => ['status' => $status], 'data' => [
'status' => $status,
'errors' => $errors,
],
]); ]);
} }
} }