Php 转换为Laravel异常?

Php 转换为Laravel异常?,php,laravel,laravel-5,laravel-5.4,Php,Laravel,Laravel 5,Laravel 5.4,当前在包中,它有HttpExceptionexception namespace DigitalOceanV2\Exception; class HttpException extends \RuntimeException implements ExceptionInterface { } 有没有一种方法可以将它转换为LavelHttpResponseException使用的异常,而不必从包中触及该异常?您可以捕获该异常并重新播放它 在app/Exceptions/Handler.php

当前在包中,它有
HttpException
exception

namespace DigitalOceanV2\Exception;

class HttpException extends \RuntimeException implements ExceptionInterface
{

}

有没有一种方法可以将它转换为Lavel
HttpResponseException
使用的异常,而不必从包中触及该异常?

您可以捕获该异常并重新播放它

app/Exceptions/Handler.php
文件中

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

    return parent::render($request, $exception);
}
编辑:我还没有测试这个,但根据。您可以将响应作为参数传递给构造函数。因此,您应该能够做到这一点:

$response = response($exception->getMessage());
throw new HttpResponseException($response);

您可以捕获该异常并重新显示它

app/Exceptions/Handler.php
文件中

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

    return parent::render($request, $exception);
}
编辑:我还没有测试这个,但根据。您可以将响应作为参数传递给构造函数。因此,您应该能够做到这一点:

$response = response($exception->getMessage());
throw new HttpResponseException($response);

它会将消息传递到HttpResponseException吗?是的,它会自动添加消息。@Jeff谢谢你的回答。只是想知道是否有办法通过HttpResponseException返回json响应?您不需要重新引发异常,只需从
render()
方法返回响应即可。例如,
如果($E异常实例){return response(['json'=>'data']);}
它会将消息传递给
HttpResponseException
?是的,它会自动添加消息。@Jeff谢谢你的回答。只是想知道是否有办法通过HttpResponseException返回json响应?您不需要重新引发异常,只需从
render()
方法返回响应即可。例如,
if($E异常实例){返回响应(['json'=>'data']);}