<?php

declare(strict_types=1);
/**
 *  +----------------------------------------------------------------------
 *  | 闄€铻哄尃 [ 璧嬭兘寮€鍙戣€咃紝鍔╁姏浼佷笟鍙戝睍 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2024 https://www.tuoluojiang.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed 闄€铻哄尃骞朵笉鏄嚜鐢辫蒋浠讹紝鏈粡璁稿彲涓嶈兘鍘绘帀闄€铻哄尃鐩稿叧鐗堟潈
 *  +----------------------------------------------------------------------
 *  | Author: 闄€铻哄尃 Team <admin@tuoluojiang.com>
 *  +----------------------------------------------------------------------
 */

namespace crmeb\exceptions;

use crmeb\services\ApiResponseService;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * Class ApiRequestException.
 */
class ApiRequestException extends \RuntimeException
{
    /**
     * @var int
     */
    protected $statusCode;

    public function __construct($message = '', $code = 0, ?\Throwable $previous = null, int $statusCode = 200)
    {
        parent::__construct($message, $code, $previous);
        $this->statusCode = $statusCode;
    }

    /**
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function render()
    {
        /** @var ApiResponseService $response */
        $response = app()->get(ApiResponseService::class);
        return $response->httpStatus($this->code)->make($this->code, $this->getMessage(), $this->getTrace());
    }
}