Laravel - 在自定义处理程序中捕获异常

Laravel - Catch exception in custom handler

我正在使用 Laravel 5.5,我想处理来自自定义处理程序的自定义异常,而不是来自 app\Exceptions\Handler.php。现在,如果用户提交的表单中的某些字段为空,我会捕获异常。它像这样完美地工作:

  1. ProfileController.php:

    public function update(Request $request, $id){
        $this->guzzleService->put(
            $request,
            ApiEndPoints::UPDATE_PROFILE . $id,
            true
        );
    
        return back()->with('SavedCorrectly', 'Changes saved correctly');
    }
    
  2. 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);
    }
    

问题是我想重构它,让它保持这样:

  1. 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;
        }
    }
    
  2. 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"]
            );
        }
    }
    

但是我收到这个错误:

Type error: Argument 2 passed to App\Exceptions\HttpExceptions\UnprocessableEntityException::render() must be an instance of GuzzleHttp\Exception\ClientException, none given, called in ... \vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php on line 169

这是因为您传递了一个新的异常

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((string) $exception->getResponse()->getBody());
        if ($exception->getCode() == 422) throw new UnprocessableEntityException((string) $exception->getResponse()->getBody());
    }
}

<?php

namespace App\Exceptions\HttpExceptions;

use GuzzleHttp\Exception\ClientException;

class UnprocessableEntityException extends \Exception
{
    public function render($request)
    {
        return back()->withErrors(
            json_decode((string) $this->message, TRUE)["errors"]
        );
    }
}