<?php


namespace Fliegerlogin\Error;


use Fliegerlogin\VereinsfliegerApi\Model\VereinsfliegerErrorDto;
use Fliegerlogin\VereinsfliegerApi\Model\VereinsfliegerExtendedErrorDto;
use Throwable;

class ApiError extends \Exception {

	/** @var string|null Raw error string returned by the Vereinsflieger API, without the wrapping noise added to getMessage(). */
	private $apiErrorMessage = null;

	/**
	 * ApiError constructor.
	 * @param                $response VereinsfliegerErrorDto|VereinsfliegerExtendedErrorDto|string Error response or message of error
	 * @param int            $code
	 * @param Throwable|null $previous
	 */
	public function __construct($response, int $code = 0, ?Throwable $previous = null) {
		if (is_string($response)) {
			parent::__construct($response, $code, $previous);
		} else {
			$this->apiErrorMessage = $response->getError();
			parent::__construct('Api Error:' . $response->getError() . "\r\n" . json_encode($response), $response->getHttpstatuscode());
		}
	}

	public function getApiErrorMessage(): ?string {
		return $this->apiErrorMessage;
	}

}