我正在使用Laravel 5.5,我想处理来自自定义处理程序的自定义异常,而不是来自 app\Exceptions\Handler.php
. 现在,我 grab 了一个异常,如果某些字段's empty in the form submitted by the user. It'完全像这样工作:
- ProfileController.php :
public function update(Request $request, $id){
$this->guzzleService->put(
$request,
ApiEndPoints::UPDATE_PROFILE . $id,
true
);
return back()->with('SavedCorrectly', 'Changes saved correctly');
}
- app\Exceptions\Handler.php
public function render($request, Exception $exception)
{
if($exception instanceof ClientException && $exception->getCode() == 422)
return back()->withErrors(
json_decode((string) $exception->getResponse()->getBody(), TRUE)["errors"]
);
return parent::render($request, $exception);
}
问题是我想重构它,所以它仍然是这样的:
- ProfileController.php
public function update(Request $request, $id){
try {
$this->guzzleService->put(
$request,
ApiEndPoints::UPDATE_PROFILE . $id,
true
);
return back()->with('SavedCorrectly', 'Cambios guardados correctamente');
} catch(ClientException $exception) {
if ($exception->getCode() == 500) throw new InternalServerErrorException;
if ($exception->getCode() == 422) throw new UnprocessableEntityException;
}
}
- app\Exceptions\HttpExceptions\UnprocessableEntityException.php
<?php
namespace App\Exceptions\HttpExceptions;
use GuzzleHttp\Exception\ClientException;
class UnprocessableEntityException extends \Exception
{
public function render($request, ClientException $exception)
{
return back()->withErrors(
json_decode((string) $exception->getResponse()->getBody(), TRUE)["errors"]
);
}
}
但是我收到了这个错误:
类型错误:参数2传递给App \ Exceptions \ HttpExceptions \ UnprocessableEntityException :: render()必须是GuzzleHttp \ Exception \ ClientException的实例,没有给出,在... \ vendor \ laravel \ framework \ src \ Illuminate \中调用第169行的Foundation \ Exceptions \ Handler.php
1 回答
这是因为您传递了一个新的异常
和