nhance/app/Libraries/DigitMotor/DigitApiException.php

89 lines
2.3 KiB
PHP

<?php
namespace App\Libraries\DigitMotor;
use Exception;
class DigitApiException extends Exception
{
protected $digitCode;
protected $digitMessage;
protected $httpStatus;
protected $responseBody;
protected $requestUrl;
protected $requestBody;
public function __construct(
string $message,
$digitCode = null,
int $httpStatus = 0,
$responseBody = null,
?Exception $previous = null,
?string $requestUrl = null,
$requestBody = null
) {
parent::__construct($message, 0, $previous);
$this->digitCode = $digitCode;
$this->digitMessage = $message;
$this->httpStatus = $httpStatus;
$this->responseBody = $responseBody;
$this->requestUrl = $requestUrl;
$this->requestBody = $requestBody;
}
public function getDigitCode()
{
return $this->digitCode;
}
public function getHttpStatus(): int
{
return $this->httpStatus;
}
public function getResponseBody()
{
return $this->responseBody;
}
public function getRequestUrl(): ?string
{
return $this->requestUrl;
}
public function getRequestBody()
{
return $this->requestBody;
}
public function isInfraError(): bool
{
$code = (string) $this->digitCode;
return in_array($code, ['403', '999'], true) || in_array($this->httpStatus, [403], true);
}
/**
* Compact log line with url / request / response for debugging.
*/
public function toLogMessage(): string
{
$encode = static function ($value): string {
if ($value === null) {
return 'null';
}
if (is_string($value)) {
return $value;
}
$json = json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
return $json !== false ? $json : '[unencodable]';
};
return 'DigitMotor API error: ' . $this->getMessage()
. ' | code=' . $this->digitCode
. ' | http=' . $this->httpStatus
. ' | url=' . ($this->requestUrl ?? 'n/a')
. ' | request=' . $encode($this->requestBody)
. ' | response=' . $encode($this->responseBody);
}
}