diff options
| author | Daniel Weipert <git@mail.dweipert.de> | 2025-09-19 17:03:47 +0200 |
|---|---|---|
| committer | Daniel Weipert <git@mail.dweipert.de> | 2025-09-19 17:03:47 +0200 |
| commit | b08047ba485f86038f33175162943c0a9878ab1a (patch) | |
| tree | 49980e78eb3e641b04ddcc0817f74e89dc07f2b7 /matrix-specification/Errors | |
| parent | 2ae0c2fa2a0bb5a7cd1fd9da1c6d2a6090126e67 (diff) | |
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, + ]; + } +} |
