<?php

namespace App\Exceptions;

use Exception;

class FbrApiException extends Exception
{
    public ?string $fbrErrorCode;
    public array $context;

    public function __construct(
        string $message,
        ?string $fbrErrorCode = null,
        array $context = [],
        int $code = 0
    ) {
        parent::__construct($message, $code);
        $this->fbrErrorCode = $fbrErrorCode;
        $this->context = $context;
    }

    public function render()
    {
        return response()->json([
            'success' => false,
            'message' => $this->getMessage(),
            'fbr_error_code' => $this->fbrErrorCode,
            'context' => $this->context,
        ], $this->code ?: 422);
    }
}
