Skip to content
Advertisement

Convert to Laravel exception?

Currently in a package, it has HttpException exception

namespace DigitalOceanV2Exception;

class HttpException extends RuntimeException implements ExceptionInterface
{

}

Is there a way to to convert it Laravel HttpResponseException uses without touching that exception from the package?

Advertisement

Answer

You can catch that exception and rethrow it.

In your app/Exceptions/Handler.php file.

public function render($request, Exception $exception)
{
    if ($exception instanceof DigitalOceanV2Exception) {
        throw new HttpResponseException;
    }

    return parent::render($request, $exception);
}

Edit: I haven’t tested this but according to the exception class. You can pass a response as a parameter to the constructor. So you should be able to do this:

$response = response($exception->getMessage());
throw new HttpResponseException($response);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement