passing exception to controller without try/catch

I have made my custom Exception defined in $this->exception($response['error']); .

So in my service method:

$response = $this->request('POST', $endpoint, $options);
    if (array_key_exists('error', $response)) {
        $this->exception($response['error']);
    }

Which is returning error message if key error exists in response.

And in my controler:

$response = $this->serviceMethod();

if (!empty($response)) {
    return $this->apiError(['error' => 'VIOLATION']);
}

If service method pass, response will return empty array which is fine.

Problem occurs that if service method catches error in if() statement and I am dumping controller method response it never gets to that part. It finishes with that custom exception.

How can I accomplish to pass response in both ways so I can handle in in my apiError() method which is made to write error from response.

It’s like:

 protected function apiError(array $errors = [], $message = ''): void
{
    throw new ApiException($errors, $message)
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *