Skip to content

Commit 82907cb

Browse files
author
Benjamin Wilson Friedman
authored
Adds specific error codes to certain ParseExceptions (#334)
* Adds specific error codes to certain ParseExceptions * lint! * Added additional error codes, reviewed and corrected/removed others
1 parent 75f1199 commit 82907cb

12 files changed

+108
-88
lines changed

src/Parse/Internal/ParseRelationOperation.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private function checkAndAssignClassName($objects)
8383
$this->targetClassName = $object->getClassName();
8484
}
8585
if ($this->targetClassName != $object->getClassName()) {
86-
throw new Exception('All objects in a relation must be of the same class.');
86+
throw new Exception('All objects in a relation must be of the same class.', 103);
8787
}
8888
}
8989
}
@@ -155,7 +155,8 @@ public function _apply($oldValue, $object, $key)
155155
throw new Exception(
156156
'Related object object must be of class '
157157
.$this->targetClassName.', but '.$oldValue->getTargetClass()
158-
.' was passed in.'
158+
.' was passed in.',
159+
103
159160
);
160161
}
161162

@@ -187,7 +188,8 @@ public function _mergeWithPrevious($previous)
187188
throw new Exception(
188189
'Related object object must be of class '
189190
.$this->targetClassName.', but '.$previous->targetClassName
190-
.' was passed in.'
191+
.' was passed in.',
192+
103
191193
);
192194
}
193195
$newRelationToAdd = self::convertToOneDimensionalArray(

src/Parse/ParseACL.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function _createACLFromJSON($data)
9595
$acl = new self();
9696
foreach ($data as $id => $permissions) {
9797
if (!is_string($id)) {
98-
throw new Exception('Tried to create an ACL with an invalid userId.');
98+
throw new Exception('Tried to create an ACL with an invalid userId.', 104);
9999
}
100100
foreach ($permissions as $accessType => $value) {
101101
if ($accessType !== 'read' && $accessType !== 'write') {
@@ -169,7 +169,8 @@ private function setAccess($accessType, $userId, $allowed)
169169
}
170170
if (!is_string($userId)) {
171171
throw new ParseException(
172-
'Invalid target for access control.'
172+
'Invalid target for access control.',
173+
104
173174
);
174175
}
175176
if (!isset($this->permissionsById[$userId])) {
@@ -219,7 +220,7 @@ private function getAccess($accessType, $userId)
219220
public function setReadAccess($userId, $allowed)
220221
{
221222
if (!$userId) {
222-
throw new Exception('cannot setReadAccess for null userId');
223+
throw new Exception('cannot setReadAccess for null userId', 104);
223224
}
224225
$this->setAccess('read', $userId, $allowed);
225226
}
@@ -239,7 +240,7 @@ public function setReadAccess($userId, $allowed)
239240
public function getReadAccess($userId)
240241
{
241242
if (!$userId) {
242-
throw new Exception('cannot getReadAccess for null userId');
243+
throw new Exception('cannot getReadAccess for null userId', 104);
243244
}
244245

245246
return $this->getAccess('read', $userId);
@@ -256,7 +257,7 @@ public function getReadAccess($userId)
256257
public function setWriteAccess($userId, $allowed)
257258
{
258259
if (!$userId) {
259-
throw new Exception('cannot setWriteAccess for null userId');
260+
throw new Exception('cannot setWriteAccess for null userId', 104);
260261
}
261262
$this->setAccess('write', $userId, $allowed);
262263
}
@@ -276,7 +277,7 @@ public function setWriteAccess($userId, $allowed)
276277
public function getWriteAccess($userId)
277278
{
278279
if (!$userId) {
279-
throw new Exception('cannot getWriteAccess for null userId');
280+
throw new Exception('cannot getWriteAccess for null userId', 104);
280281
}
281282

282283
return $this->getAccess('write', $userId);
@@ -333,7 +334,7 @@ public function getPublicWriteAccess()
333334
public function setUserReadAccess($user, $allowed)
334335
{
335336
if (!$user->getObjectId()) {
336-
throw new Exception('cannot setReadAccess for a user with null id');
337+
throw new Exception('cannot setReadAccess for a user with null id', 104);
337338
}
338339
$this->setReadAccess($user->getObjectId(), $allowed);
339340
}
@@ -353,7 +354,7 @@ public function setUserReadAccess($user, $allowed)
353354
public function getUserReadAccess($user)
354355
{
355356
if (!$user->getObjectId()) {
356-
throw new Exception('cannot getReadAccess for a user with null id');
357+
throw new Exception('cannot getReadAccess for a user with null id', 104);
357358
}
358359

359360
return $this->getReadAccess($user->getObjectId());
@@ -370,7 +371,7 @@ public function getUserReadAccess($user)
370371
public function setUserWriteAccess($user, $allowed)
371372
{
372373
if (!$user->getObjectId()) {
373-
throw new Exception('cannot setWriteAccess for a user with null id');
374+
throw new Exception('cannot setWriteAccess for a user with null id', 104);
374375
}
375376
$this->setWriteAccess($user->getObjectId(), $allowed);
376377
}
@@ -390,7 +391,7 @@ public function setUserWriteAccess($user, $allowed)
390391
public function getUserWriteAccess($user)
391392
{
392393
if (!$user->getObjectId()) {
393-
throw new Exception('cannot getWriteAccess for a user with null id');
394+
throw new Exception('cannot getWriteAccess for a user with null id', 104);
394395
}
395396

396397
return $this->getWriteAccess($user->getObjectId());
@@ -459,7 +460,8 @@ private static function validateRoleState($role)
459460
{
460461
if (!$role->getObjectId()) {
461462
throw new Exception(
462-
'Roles must be saved to the server before they can be used in an ACL.'
463+
'Roles must be saved to the server before they can be used in an ACL.',
464+
104
463465
);
464466
}
465467
}

src/Parse/ParseClient.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ private static function assertParseInitialized()
628628
{
629629
if (self::$applicationId === null) {
630630
throw new Exception(
631-
'You must call ParseClient::initialize() before making any requests.'
631+
'You must call ParseClient::initialize() before making any requests.',
632+
109
632633
);
633634
}
634635
}
@@ -643,7 +644,8 @@ private static function assertAppInitialized()
643644
if (self::$accountKey === null || empty(self::$accountKey)) {
644645
throw new Exception(
645646
'You must call ParseClient::initialize(..., $accountKey) before making any app requests. '.
646-
'Your account key must not be null or empty.'
647+
'Your account key must not be null or empty.',
648+
109
647649
);
648650
}
649651
}

src/Parse/ParseFile.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function getData()
5656
return $this->data;
5757
}
5858
if (!$this->url) {
59-
throw new ParseException('Cannot retrieve data for unsaved ParseFile.');
59+
throw new ParseException('Cannot retrieve data for unsaved ParseFile.', 151);
6060
}
6161
$this->data = $this->download();
6262

@@ -93,7 +93,7 @@ public function getName()
9393
public function delete($useMasterKey = true)
9494
{
9595
if (!$this->url) {
96-
throw new ParseException('Cannot delete file that has not been saved.');
96+
throw new ParseException('Cannot delete file that has not been saved.', 151);
9797
}
9898

9999
ParseClient::_request(

src/Parse/ParseObject.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function __construct($className = null, $objectId = null, $isPointer = fa
110110
if (empty(self::$registeredSubclasses)) {
111111
throw new Exception(
112112
'You must initialize the ParseClient using ParseClient::initialize '.
113-
'and your Parse API keys before you can begin working with Objects.'
113+
'and your Parse API keys before you can begin working with Objects.',
114+
109
114115
);
115116
}
116117
$subclass = static::getSubclass();
@@ -163,7 +164,7 @@ public function __set($key, $value)
163164
) {
164165
$this->set($key, $value);
165166
} else {
166-
throw new Exception('Protected field could not be set.');
167+
throw new Exception('Protected field could not be set.', 139);
167168
}
168169
}
169170

@@ -576,9 +577,9 @@ private static function toObjectIdArray(array $objects)
576577
for ($i = 0; $i < $count; ++$i) {
577578
$obj = $objects[$i];
578579
if ($obj->getClassName() !== $className) {
579-
throw new ParseException('All objects should be of the same class.');
580+
throw new ParseException('All objects should be of the same class.', 103);
580581
} elseif (!$obj->getObjectId()) {
581-
throw new ParseException('All objects must have an ID.');
582+
throw new ParseException('All objects must have an ID.', 104);
582583
}
583584
array_push($objectIds, $obj->getObjectId());
584585
}
@@ -604,7 +605,7 @@ private static function updateWithFetchedResults(array $objects, array $fetched)
604605
for ($i = 0; $i < $count; ++$i) {
605606
$obj = $objects[$i];
606607
if (!isset($fetchedObjectsById[$obj->getObjectId()])) {
607-
throw new ParseException('All objects must exist on the server.');
608+
throw new ParseException('All objects must exist on the server.', 101);
608609
}
609610
$obj->mergeFromObject($fetchedObjectsById[$obj->getObjectId()]);
610611
}
@@ -1279,7 +1280,7 @@ public function getRelation($key, $className = null)
12791280
public function _toPointer()
12801281
{
12811282
if (!$this->objectId) {
1282-
throw new Exception("Can't serialize an unsaved Parse.Object");
1283+
throw new Exception("Can't serialize an unsaved ParseObject", 104);
12831284
}
12841285

12851286
return [

src/Parse/ParsePush.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public static function send($data, $useMasterKey = false)
4040
&& isset($data['expiration_interval'])
4141
) {
4242
throw new Exception(
43-
'Both expiration_time and expiration_interval can\'t be set.'
43+
'Both expiration_time and expiration_interval can\'t be set.',
44+
138
4445
);
4546
}
4647
if (isset($data['where'])) {
@@ -54,7 +55,8 @@ public static function send($data, $useMasterKey = false)
5455
}
5556
} else {
5657
throw new Exception(
57-
'Where parameter for Parse Push must be of type ParseQuery'
58+
'Where parameter for Parse Push must be of type ParseQuery',
59+
111
5860
);
5961
}
6062
}

src/Parse/ParseQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ public static function orQueries($queryObjects)
865865
$className = $queryObjects[$i]->className;
866866
}
867867
if ($className != $queryObjects[$i]->className) {
868-
throw new Exception('All queries must be for the same class');
868+
throw new Exception('All queries must be for the same class', 103);
869869
}
870870
}
871871
$query = new self($className);

src/Parse/ParseRole.php

+5-13
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,10 @@ public function getName()
5757
public function setName($name)
5858
{
5959
if ($this->getObjectId()) {
60-
throw new ParseException(
61-
"A role's name can only be set before it has been saved."
62-
);
60+
throw new ParseException("A role's name can only be set before it has been saved.");
6361
}
6462
if (!is_string($name)) {
65-
throw new ParseException(
66-
"A role's name must be a string."
67-
);
63+
throw new ParseException("A role's name must be a string.", 139);
6864
}
6965

7066
return $this->set('name', $name);
@@ -104,22 +100,18 @@ public function getRoles()
104100
public function save($useMasterKey = false)
105101
{
106102
if (!$this->getACL()) {
107-
throw new ParseException(
108-
'Roles must have an ACL.'
109-
);
103+
throw new ParseException('Roles must have an ACL.', 123);
110104
}
111105
if (!$this->getName() || !is_string($this->getName())) {
112-
throw new ParseException(
113-
'Roles must have a name.'
114-
);
106+
throw new ParseException('Roles must have a name.', 139);
115107
}
116108
if ($this->getObjectId() === null) {
117109
// Not yet saved, verify this name is not taken
118110
// ParseServer does not validate duplicate role names as of parse-server v2.3.2
119111
$query = new ParseQuery('_Role');
120112
$query->equalTo('name', $this->getName());
121113
if ($query->count(true) > 0) {
122-
throw new ParseException("Cannot add duplicate role name of '{$this->getName()}'");
114+
throw new ParseException("Cannot add duplicate role name of '{$this->getName()}'", 137);
123115
}
124116
}
125117

0 commit comments

Comments
 (0)