diff options
Diffstat (limited to 'matrix-specification/Errors')
-rw-r--r-- | matrix-specification/Errors/Error.php | 41 | ||||
-rw-r--r-- | matrix-specification/Errors/RateLimitError.php | 20 |
2 files changed, 61 insertions, 0 deletions
diff --git a/matrix-specification/Errors/Error.php b/matrix-specification/Errors/Error.php new file mode 100644 index 0000000..2adc642 --- /dev/null +++ b/matrix-specification/Errors/Error.php @@ -0,0 +1,41 @@ +<?php + +namespace Matrix\Errors; + +use Matrix\Enums\ErrorCode; + +abstract class Error extends \RuntimeException implements \JsonSerializable +{ + public function __construct( + private ErrorCode $errorCode, + string $message, + int $httpCode + ) + { + parent::__construct($message, $httpCode); + } + + public function getErrorCode(): ErrorCode + { + return $this->errorCode; + } + + public function getHttpCode(): int + { + return $this->getCode(); + } + + /** + * @return array<string, mixed> + */ + abstract public function getAdditionalData(): array; + + public function jsonSerialize(): array + { + return [ + "errcode" => $this->getErrorCode(), + "error" => $this->getMessage(), + ...$this->getAdditionalData(), + ]; + } +} diff --git a/matrix-specification/Errors/RateLimitError.php b/matrix-specification/Errors/RateLimitError.php new file mode 100644 index 0000000..2f4193c --- /dev/null +++ b/matrix-specification/Errors/RateLimitError.php @@ -0,0 +1,20 @@ +<?php + +namespace Matrix\Errors; + +use Matrix\Enums\ErrorCode; + +class RateLimitError extends Error +{ + public function __construct(private int $retryAfterMilliseconds) + { + parent::__construct(ErrorCode::LIMIT_EXCEEDED, "Too many requests", 429); + } + + public function getAdditionalData(): array + { + return [ + "retry_after_ms" => $this->retryAfterMilliseconds, + ]; + } +} |