Send extra request headers and throw PreconditionFailedException on 412

This commit is contained in:
envoyr committed 2026-11-06 19:25:51 +00:00
1 parent 9c505842cb
commit d76095cc94
5 files changed
+164 -18

No files matched your search

+7 -1
View File
@@ -28,10 +28,16 @@ class ApiException extends ResourceException
public static function fromResult(Result $result): static
{
$body = is_object($result->data) ? $result->data : null;
$status = $result->response()?->getStatusCode() ?? 0;
// a failed conditional write; subclasses (CollectionException) keep their own type
if ($status === 412 && static::class === self::class) {
return PreconditionFailedException::fromResult($result);
}
return new static(
rtrim((string) ($body->message ?? $result->error()), '.'),
$result->response()?->getStatusCode() ?? 0,
$status,
$body->error ?? null,
$result->exception,
);
@@ -0,0 +1,21 @@
<?php
namespace Anikeen\Id\Exceptions;
/**
* A conditional write failed (412): the stored version wasn't the one sent with `If-Match`
* ("preference_version_mismatch"), or the key was already set and the write was create-only
* ("preference_exists"). Reload, merge and retry with currentVersion().
*/
class PreconditionFailedException extends ApiException
{
/**
* The version stored now, null when the key isn't set (or Anikeen ID didn't send one).
*/
public function currentVersion(): ?int
{
$version = $this->body()->current_version ?? null;
return is_numeric($version) ? (int) $version : null;
}
}