Skip to content

Commit cb9705f

Browse files
committed
update: better signature.
1 parent 2e4ac09 commit cb9705f

40 files changed

Lines changed: 124 additions & 134 deletions

File tree

src/Appwrite/Extend/Exception.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ public function __construct(
389389
string $message = null,
390390
int|string $code = null,
391391
\Throwable $previous = null,
392-
?string $view = null
392+
?string $view = null,
393+
array $params = []
393394
) {
394395
$this->errors = Config::getParam('errors');
395396
$this->type = $type;
@@ -405,30 +406,19 @@ public function __construct(
405406
}
406407
}
407408

408-
$this->message = $message ?? $this->errors[$type]['description'];
409+
// Format message with params if provided
410+
if (!empty($params) && $message === null) {
411+
$description = $this->errors[$type]['description'] ?? '';
412+
$this->message = !empty($description) ? sprintf($description, ...$params) : '';
413+
} else {
414+
$this->message = $message ?? $this->errors[$type]['description'];
415+
}
409416

410417
$this->publish = $this->errors[$type]['publish'] ?? ($this->code >= 500);
411418

412419
parent::__construct($this->message, $this->code, $previous);
413420
}
414421

415-
/**
416-
* Create an exception with formatted parameters.
417-
*/
418-
public static function withParams(string $type, mixed ...$params): self
419-
{
420-
$errors = Config::getParam('errors');
421-
$description = $errors[$type]['description'] ?? '';
422-
423-
if (!empty($params) && !empty($description)) {
424-
$message = sprintf($description, ...$params);
425-
} else {
426-
$message = $description;
427-
}
428-
429-
return new self($type, $message);
430-
}
431-
432422
/**
433423
* Get the type of the exception.
434424
*

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Action.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,19 @@ protected function createAttribute(string $databaseId, string $collectionId, Doc
307307
$options = $attribute->getAttribute('options', []);
308308

309309
if (in_array($type, Database::SPATIAL_TYPES) && !$dbForProject->getAdapter()->getSupportForSpatialAttributes()) {
310-
throw Exception::withParams($this->getSpatialTypeNotSupportedException(), $type);
310+
throw new Exception($this->getSpatialTypeNotSupportedException(), params: [$type]);
311311
}
312312

313313
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
314314

315315
if ($db->isEmpty()) {
316-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
316+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
317317
}
318318

319319
$collection = $dbForProject->getDocument('database_' . $db->getSequence(), $collectionId);
320320

321321
if ($collection->isEmpty()) {
322-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
322+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
323323
}
324324

325325
if (!empty($format)) {
@@ -382,9 +382,9 @@ protected function createAttribute(string $databaseId, string $collectionId, Doc
382382
$dbForProject->checkAttribute($collection, $attribute);
383383
$attribute = $dbForProject->createDocument('attributes', $attribute);
384384
} catch (DuplicateException) {
385-
throw Exception::withParams($this->getDuplicateException(), $key);
385+
throw new Exception($this->getDuplicateException(), params: [$key]);
386386
} catch (LimitException) {
387-
throw Exception::withParams($this->getLimitException(), $collectionId);
387+
throw new Exception($this->getLimitException(), params: [$collectionId]);
388388
} catch (StructureException $e) {
389389
throw new Exception($this->getStructureException(), $e->getMessage());
390390
} catch (Throwable $e) {
@@ -426,9 +426,9 @@ protected function createAttribute(string $databaseId, string $collectionId, Doc
426426
$dbForProject->checkAttribute($relatedCollection, $twoWayAttribute);
427427
$dbForProject->createDocument('attributes', $twoWayAttribute);
428428
} catch (DuplicateException) {
429-
throw Exception::withParams($this->getDuplicateException(), $twoWayKey);
429+
throw new Exception($this->getDuplicateException(), params: [$twoWayKey]);
430430
} catch (LimitException) {
431-
throw Exception::withParams($this->getLimitException(), $relatedCollection->getId());
431+
throw new Exception($this->getLimitException(), params: [$relatedCollection->getId()]);
432432
} catch (StructureException) {
433433
throw new Exception($this->getStructureException());
434434
} catch (Throwable $e) {
@@ -477,19 +477,19 @@ protected function updateAttribute(string $databaseId, string $collectionId, str
477477
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
478478

479479
if ($db->isEmpty()) {
480-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
480+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
481481
}
482482

483483
$collection = $dbForProject->getDocument('database_' . $db->getSequence(), $collectionId);
484484

485485
if ($collection->isEmpty()) {
486-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
486+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
487487
}
488488

489489
$attribute = $dbForProject->getDocument('attributes', $db->getSequence() . '_' . $collection->getSequence() . '_' . $key);
490490

491491
if ($attribute->isEmpty()) {
492-
throw Exception::withParams($this->getNotFoundException(), $key);
492+
throw new Exception($this->getNotFoundException(), params: [$key]);
493493
}
494494

495495
if ($attribute->getAttribute('status') !== 'available') {
@@ -590,7 +590,7 @@ protected function updateAttribute(string $databaseId, string $collectionId, str
590590
} catch (IndexException) {
591591
throw new Exception(Exception::INDEX_INVALID);
592592
} catch (LimitException) {
593-
throw Exception::withParams($this->getLimitException(), $collectionId);
593+
throw new Exception($this->getLimitException(), params: [$collectionId]);
594594
} catch (RelationshipException $e) {
595595
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage());
596596
} catch (StructureException $e) {
@@ -624,11 +624,11 @@ protected function updateAttribute(string $databaseId, string $collectionId, str
624624
newKey: $newKey ?? null
625625
);
626626
} catch (DuplicateException) {
627-
throw Exception::withParams($this->getDuplicateException(), $key);
627+
throw new Exception($this->getDuplicateException(), params: [$key]);
628628
} catch (IndexException $e) {
629629
throw new Exception($this->getInvalidIndexException(), $e->getMessage());
630630
} catch (LimitException) {
631-
throw Exception::withParams($this->getLimitException(), $collectionId);
631+
throw new Exception($this->getLimitException(), params: [$collectionId]);
632632
} catch (TruncateException) {
633633
throw new Exception($this->getInvalidResizeException());
634634
}
@@ -644,7 +644,7 @@ protected function updateAttribute(string $databaseId, string $collectionId, str
644644
try {
645645
$dbForProject->updateDocument('attributes', $originalUid, $attribute);
646646
} catch (DuplicateException) {
647-
throw Exception::withParams($this->getDuplicateException(), $newKey);
647+
throw new Exception($this->getDuplicateException(), params: [$newKey]);
648648
}
649649

650650
/**

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Delete.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ public function action(string $databaseId, string $collectionId, string $key, Ut
7474
{
7575
$db = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
7676
if ($db->isEmpty()) {
77-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
77+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
7878
}
7979

8080
$collection = $dbForProject->getDocument('database_' . $db->getSequence(), $collectionId);
8181
if ($collection->isEmpty()) {
82-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
82+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
8383
}
8484

8585
$attribute = $dbForProject->getDocument('attributes', $db->getSequence() . '_' . $collection->getSequence() . '_' . $key);
8686
if ($attribute->isEmpty()) {
87-
throw Exception::withParams($this->getNotFoundException(), $key);
87+
throw new Exception($this->getNotFoundException(), params: [$key]);
8888
}
8989

9090
$validator = new IndexDependencyValidator(
@@ -93,7 +93,7 @@ public function action(string $databaseId, string $collectionId, string $key, Ut
9393
);
9494

9595
if (!$validator->isValid($attribute)) {
96-
throw Exception::withParams($this->getIndexDependencyException(), $key);
96+
throw new Exception($this->getIndexDependencyException(), params: [$key]);
9797
}
9898

9999
if ($attribute->getAttribute('status') === 'available') {
@@ -108,12 +108,12 @@ public function action(string $databaseId, string $collectionId, string $key, Ut
108108
if ($options['twoWay']) {
109109
$relatedCollection = $dbForProject->getDocument('database_' . $db->getSequence(), $options['relatedCollection']);
110110
if ($relatedCollection->isEmpty()) {
111-
throw Exception::withParams($this->getParentNotFoundException(), $options['relatedCollection']);
111+
throw new Exception($this->getParentNotFoundException(), params: [$options['relatedCollection']]);
112112
}
113113

114114
$relatedAttribute = $dbForProject->getDocument('attributes', $db->getSequence() . '_' . $relatedCollection->getSequence() . '_' . $options['twoWayKey']);
115115
if ($relatedAttribute->isEmpty()) {
116-
throw Exception::withParams($this->getNotFoundException(), $options['twoWayKey']);
116+
throw new Exception($this->getNotFoundException(), params: [$options['twoWayKey']]);
117117
}
118118

119119
if ($relatedAttribute->getAttribute('status') === 'available') {

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Get.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ public function action(string $databaseId, string $collectionId, string $key, Ut
7575
{
7676
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
7777
if ($database->isEmpty()) {
78-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
78+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
7979
}
8080

8181
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
8282
if ($collection->isEmpty()) {
83-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
83+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
8484
}
8585

8686
$attribute = $dbForProject->getDocument('attributes', $database->getSequence() . '_' . $collection->getSequence() . '_' . $key);
8787
if ($attribute->isEmpty()) {
88-
throw Exception::withParams($this->getNotFoundException(), $key);
88+
throw new Exception($this->getNotFoundException(), params: [$key]);
8989
}
9090

9191
$type = $attribute->getAttribute('type');

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/Relationship/Create.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,19 @@ public function action(string $databaseId, string $collectionId, string $related
9393

9494
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
9595
if ($database->isEmpty()) {
96-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
96+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
9797
}
9898

9999
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
100100
$collection = $dbForProject->getCollection('database_' . $database->getSequence() . '_collection_' . $collection->getSequence());
101101
if ($collection->isEmpty()) {
102-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
102+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
103103
}
104104

105105
$relatedCollectionDocument = $dbForProject->getDocument('database_' . $database->getSequence(), $relatedCollectionId);
106106
$relatedCollection = $dbForProject->getCollection('database_' . $database->getSequence() . '_collection_' . $relatedCollectionDocument->getSequence());
107107
if ($relatedCollection->isEmpty()) {
108-
throw Exception::withParams($this->getParentNotFoundException(), $relatedCollectionId);
108+
throw new Exception($this->getParentNotFoundException(), params: [$relatedCollectionId]);
109109
}
110110

111111
$attributes = $collection->getAttribute('attributes', []);
@@ -115,14 +115,14 @@ public function action(string $databaseId, string $collectionId, string $related
115115
}
116116

117117
if (\strtolower($attribute->getId()) === \strtolower($key)) {
118-
throw Exception::withParams($this->getDuplicateException(), $key);
118+
throw new Exception($this->getDuplicateException(), params: [$key]);
119119
}
120120

121121
if (
122122
\strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) &&
123123
$attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
124124
) {
125-
throw Exception::withParams($this->getDuplicateException(), $twoWayKey);
125+
throw new Exception($this->getDuplicateException(), params: [$twoWayKey]);
126126
}
127127

128128
if (

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Attributes/XList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ public function action(string $databaseId, string $collectionId, array $queries,
7171
{
7272
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
7373
if ($database->isEmpty()) {
74-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
74+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
7575
}
7676

7777
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
7878
if ($collection->isEmpty()) {
79-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
79+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
8080
}
8181

8282
try {

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Create.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function action(string $databaseId, string $collectionId, string $name, ?
9393
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
9494

9595
if ($database->isEmpty()) {
96-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
96+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
9797
}
9898

9999
$collectionId = $collectionId === 'unique()' ? ID::unique() : $collectionId;
@@ -113,11 +113,11 @@ public function action(string $databaseId, string $collectionId, string $name, ?
113113
'search' => \implode(' ', [$collectionId, $name]),
114114
]));
115115
} catch (DuplicateException) {
116-
throw Exception::withParams($this->getDuplicateException(), $collectionId);
116+
throw new Exception($this->getDuplicateException(), params: [$collectionId]);
117117
} catch (LimitException) {
118-
throw Exception::withParams($this->getLimitException(), $databaseId);
118+
throw new Exception($this->getLimitException(), params: [$databaseId]);
119119
} catch (NotFoundException) {
120-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
120+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
121121
}
122122

123123
$collectionKey = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence();
@@ -205,13 +205,13 @@ public function action(string $databaseId, string $collectionId, string $name, ?
205205
);
206206
} catch (DuplicateException) {
207207
$dbForProject->deleteDocument($databaseKey, $collection->getId());
208-
throw Exception::withParams($this->getDuplicateException(), $collectionId);
208+
throw new Exception($this->getDuplicateException(), params: [$collectionId]);
209209
} catch (IndexException $e) {
210210
$dbForProject->deleteDocument($databaseKey, $collection->getId());
211211
throw new Exception($this->getInvalidIndexException(), $e->getMessage());
212212
} catch (LimitException) {
213213
$dbForProject->deleteDocument($databaseKey, $collection->getId());
214-
throw Exception::withParams($this->getLimitException(), $collectionId);
214+
throw new Exception($this->getLimitException(), params: [$collectionId]);
215215
} catch (\Throwable $e) {
216216
$dbForProject->deleteDocument($databaseKey, $collection->getId());
217217
throw $e;
@@ -227,7 +227,7 @@ public function action(string $databaseId, string $collectionId, string $name, ?
227227
}
228228
} catch (DuplicateException) {
229229
$this->cleanup($dbForProject, $databaseKey, $collectionKey, $collection->getId());
230-
throw Exception::withParams($this->getDuplicateException(), $collectionId);
230+
throw new Exception($this->getDuplicateException(), params: [$collectionId]);
231231
} catch (\Throwable $e) {
232232
$this->cleanup($dbForProject, $databaseKey, $collectionKey, $collection->getId());
233233
throw $e;

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ public function action(string $databaseId, string $collectionId, UtopiaResponse
7171
{
7272
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
7373
if ($database->isEmpty()) {
74-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
74+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
7575
}
7676

7777
$collection = $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId);
7878
if ($collection->isEmpty()) {
79-
throw Exception::withParams($this->getNotFoundException(), $collectionId);
79+
throw new Exception($this->getNotFoundException(), params: [$collectionId]);
8080
}
8181

8282
if (!$dbForProject->deleteDocument('database_' . $database->getSequence(), $collectionId)) {

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Decrement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public function action(string $databaseId, string $collectionId, string $documen
9595

9696
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
9797
if ($database->isEmpty()) {
98-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
98+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
9999
}
100100

101101
$collection = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId));
102102
if ($collection->isEmpty()) {
103-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
103+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
104104
}
105105

106106
// Handle transaction staging

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Attribute/Increment.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public function action(string $databaseId, string $collectionId, string $documen
9595

9696
$database = Authorization::skip(fn () => $dbForProject->getDocument('databases', $databaseId));
9797
if ($database->isEmpty()) {
98-
throw Exception::withParams(Exception::DATABASE_NOT_FOUND, $databaseId);
98+
throw new Exception(Exception::DATABASE_NOT_FOUND, params: [$databaseId]);
9999
}
100100

101101
$collection = Authorization::skip(fn () => $dbForProject->getDocument('database_' . $database->getSequence(), $collectionId));
102102
if ($collection->isEmpty()) {
103-
throw Exception::withParams($this->getParentNotFoundException(), $collectionId);
103+
throw new Exception($this->getParentNotFoundException(), params: [$collectionId]);
104104
}
105105

106106
// Handle transaction staging

0 commit comments

Comments
 (0)