How to use render( method of custom exception?

21 views Asked by At

Reading at docs https://laravel.com/docs/10.x/errors#renderable-exceptions in laravel 10 app I added render method in custom exception :

namespace App\Exceptions;

use Exception;

class MailchimpDataFetchingException extends Exception
{
    public $action;
    public $title;
    public $detail;
    public $status;
    public function __construct(string $action, string $title, string $detail, int $status)
    {
        parent::__construct();
        $this->action = $action;
        $this->title = $title;
        $this->detail = $detail;
        $this->status = $status;
    }

    public function render()
    {
        return response()->json([
            'success' => $this->status === 2000,
            'action' => $this->action,
            'title' => $this->title,
            'detail' => $this->detail,
            'status' => $this->status,
        ], $this->status);
    }
}

I use class MailchimpDataFetchingException as :

try {
    $response = $this->getApiClient()->lists->addListMember($this->mailchimpListId, [
        "email_address" => $email,
        "status" => "subscribed",
        'merge_fields' => $userMergeFields,
    ]);
} catch (ClientException $e) {
    $errorResponse = json_decode($e->getResponse()->getBody()->getContents(), true);
    throw new MailchimpDataFetchingException(action: 'user_subscribe', title: $errorResponse['title'],
        detail: $errorResponse['detail'], status: $errorResponse['status']);
}

But I do not use render( method actiually. How can I call it to get use of it ?

0

There are 0 answers