HBankException.php

PHP SDK · 1.2 KB · 41 lines

1<?php
2
3declare(strict_types=1);
4
5namespace HBank;
6
7/**
8 * The API answered with an error, or couldn't be reached.
9 *
10 * - `$errorCode`: machine-readable ("invalid_amount", "transaction_not_found", "invalid_api_key", "missing_api_key",
11 * "connection_failed", "timeout", ...). Exception::getCode() is the HTTP status.
12 * - `$status`: the HTTP status, 0 when HBank couldn't be reached. After a connection error the request may or may not
13 * have reached HBank: before retrying a refund, check `refunded` with getPayment().
14 * - `$detail`: the readable message some errors carry.
15 */
16final class HBankException extends \RuntimeException
17{
18 public function __construct(
19 public readonly string $errorCode,
20 public readonly int $status,
21 public readonly ?string $detail = null,
22 public readonly ?array $body = null,
23 ) {
24 parent::__construct($detail === null ? $errorCode : "{$errorCode}: {$detail}", $status);
25 }
26
27 public function errorCode(): string
28 {
29 return $this->errorCode;
30 }
31
32 public function isAuthentication(): bool
33 {
34 return $this->status === 401 || $this->status === 403;
35 }
36
37 public function isConnection(): bool
38 {
39 return $this->status === 0;
40 }
41}