Skip to content

Adds specific error codes to certain ParseExceptions #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Parse/Internal/ParseRelationOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function checkAndAssignClassName($objects)
$this->targetClassName = $object->getClassName();
}
if ($this->targetClassName != $object->getClassName()) {
throw new Exception('All objects in a relation must be of the same class.');
throw new Exception('All objects in a relation must be of the same class.', 103);
}
}
}
Expand Down Expand Up @@ -155,7 +155,8 @@ public function _apply($oldValue, $object, $key)
throw new Exception(
'Related object object must be of class '
.$this->targetClassName.', but '.$oldValue->getTargetClass()
.' was passed in.'
.' was passed in.',
103
);
}

Expand Down Expand Up @@ -187,7 +188,8 @@ public function _mergeWithPrevious($previous)
throw new Exception(
'Related object object must be of class '
.$this->targetClassName.', but '.$previous->targetClassName
.' was passed in.'
.' was passed in.',
103
);
}
$newRelationToAdd = self::convertToOneDimensionalArray(
Expand Down
24 changes: 13 additions & 11 deletions src/Parse/ParseACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static function _createACLFromJSON($data)
$acl = new self();
foreach ($data as $id => $permissions) {
if (!is_string($id)) {
throw new Exception('Tried to create an ACL with an invalid userId.');
throw new Exception('Tried to create an ACL with an invalid userId.', 104);
}
foreach ($permissions as $accessType => $value) {
if ($accessType !== 'read' && $accessType !== 'write') {
Expand Down Expand Up @@ -169,7 +169,8 @@ private function setAccess($accessType, $userId, $allowed)
}
if (!is_string($userId)) {
throw new ParseException(
'Invalid target for access control.'
'Invalid target for access control.',
104
);
}
if (!isset($this->permissionsById[$userId])) {
Expand Down Expand Up @@ -219,7 +220,7 @@ private function getAccess($accessType, $userId)
public function setReadAccess($userId, $allowed)
{
if (!$userId) {
throw new Exception('cannot setReadAccess for null userId');
throw new Exception('cannot setReadAccess for null userId', 104);
}
$this->setAccess('read', $userId, $allowed);
}
Expand All @@ -239,7 +240,7 @@ public function setReadAccess($userId, $allowed)
public function getReadAccess($userId)
{
if (!$userId) {
throw new Exception('cannot getReadAccess for null userId');
throw new Exception('cannot getReadAccess for null userId', 104);
}

return $this->getAccess('read', $userId);
Expand All @@ -256,7 +257,7 @@ public function getReadAccess($userId)
public function setWriteAccess($userId, $allowed)
{
if (!$userId) {
throw new Exception('cannot setWriteAccess for null userId');
throw new Exception('cannot setWriteAccess for null userId', 104);
}
$this->setAccess('write', $userId, $allowed);
}
Expand All @@ -276,7 +277,7 @@ public function setWriteAccess($userId, $allowed)
public function getWriteAccess($userId)
{
if (!$userId) {
throw new Exception('cannot getWriteAccess for null userId');
throw new Exception('cannot getWriteAccess for null userId', 104);
}

return $this->getAccess('write', $userId);
Expand Down Expand Up @@ -333,7 +334,7 @@ public function getPublicWriteAccess()
public function setUserReadAccess($user, $allowed)
{
if (!$user->getObjectId()) {
throw new Exception('cannot setReadAccess for a user with null id');
throw new Exception('cannot setReadAccess for a user with null id', 104);
}
$this->setReadAccess($user->getObjectId(), $allowed);
}
Expand All @@ -353,7 +354,7 @@ public function setUserReadAccess($user, $allowed)
public function getUserReadAccess($user)
{
if (!$user->getObjectId()) {
throw new Exception('cannot getReadAccess for a user with null id');
throw new Exception('cannot getReadAccess for a user with null id', 104);
}

return $this->getReadAccess($user->getObjectId());
Expand All @@ -370,7 +371,7 @@ public function getUserReadAccess($user)
public function setUserWriteAccess($user, $allowed)
{
if (!$user->getObjectId()) {
throw new Exception('cannot setWriteAccess for a user with null id');
throw new Exception('cannot setWriteAccess for a user with null id', 104);
}
$this->setWriteAccess($user->getObjectId(), $allowed);
}
Expand All @@ -390,7 +391,7 @@ public function setUserWriteAccess($user, $allowed)
public function getUserWriteAccess($user)
{
if (!$user->getObjectId()) {
throw new Exception('cannot getWriteAccess for a user with null id');
throw new Exception('cannot getWriteAccess for a user with null id', 104);
}

return $this->getWriteAccess($user->getObjectId());
Expand Down Expand Up @@ -459,7 +460,8 @@ private static function validateRoleState($role)
{
if (!$role->getObjectId()) {
throw new Exception(
'Roles must be saved to the server before they can be used in an ACL.'
'Roles must be saved to the server before they can be used in an ACL.',
104
);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Parse/ParseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ private static function assertParseInitialized()
{
if (self::$applicationId === null) {
throw new Exception(
'You must call ParseClient::initialize() before making any requests.'
'You must call ParseClient::initialize() before making any requests.',
109
);
}
}
Expand All @@ -643,7 +644,8 @@ private static function assertAppInitialized()
if (self::$accountKey === null || empty(self::$accountKey)) {
throw new Exception(
'You must call ParseClient::initialize(..., $accountKey) before making any app requests. '.
'Your account key must not be null or empty.'
'Your account key must not be null or empty.',
109
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parse/ParseFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getData()
return $this->data;
}
if (!$this->url) {
throw new ParseException('Cannot retrieve data for unsaved ParseFile.');
throw new ParseException('Cannot retrieve data for unsaved ParseFile.', 151);
}
$this->data = $this->download();

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

ParseClient::_request(
Expand Down
13 changes: 7 additions & 6 deletions src/Parse/ParseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public function __construct($className = null, $objectId = null, $isPointer = fa
if (empty(self::$registeredSubclasses)) {
throw new Exception(
'You must initialize the ParseClient using ParseClient::initialize '.
'and your Parse API keys before you can begin working with Objects.'
'and your Parse API keys before you can begin working with Objects.',
109
);
}
$subclass = static::getSubclass();
Expand Down Expand Up @@ -163,7 +164,7 @@ public function __set($key, $value)
) {
$this->set($key, $value);
} else {
throw new Exception('Protected field could not be set.');
throw new Exception('Protected field could not be set.', 139);
}
}

Expand Down Expand Up @@ -576,9 +577,9 @@ private static function toObjectIdArray(array $objects)
for ($i = 0; $i < $count; ++$i) {
$obj = $objects[$i];
if ($obj->getClassName() !== $className) {
throw new ParseException('All objects should be of the same class.');
throw new ParseException('All objects should be of the same class.', 103);
} elseif (!$obj->getObjectId()) {
throw new ParseException('All objects must have an ID.');
throw new ParseException('All objects must have an ID.', 104);
}
array_push($objectIds, $obj->getObjectId());
}
Expand All @@ -604,7 +605,7 @@ private static function updateWithFetchedResults(array $objects, array $fetched)
for ($i = 0; $i < $count; ++$i) {
$obj = $objects[$i];
if (!isset($fetchedObjectsById[$obj->getObjectId()])) {
throw new ParseException('All objects must exist on the server.');
throw new ParseException('All objects must exist on the server.', 101);
}
$obj->mergeFromObject($fetchedObjectsById[$obj->getObjectId()]);
}
Expand Down Expand Up @@ -1279,7 +1280,7 @@ public function getRelation($key, $className = null)
public function _toPointer()
{
if (!$this->objectId) {
throw new Exception("Can't serialize an unsaved Parse.Object");
throw new Exception("Can't serialize an unsaved ParseObject", 104);
}

return [
Expand Down
6 changes: 4 additions & 2 deletions src/Parse/ParsePush.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public static function send($data, $useMasterKey = false)
&& isset($data['expiration_interval'])
) {
throw new Exception(
'Both expiration_time and expiration_interval can\'t be set.'
'Both expiration_time and expiration_interval can\'t be set.',
138
);
}
if (isset($data['where'])) {
Expand All @@ -54,7 +55,8 @@ public static function send($data, $useMasterKey = false)
}
} else {
throw new Exception(
'Where parameter for Parse Push must be of type ParseQuery'
'Where parameter for Parse Push must be of type ParseQuery',
111
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parse/ParseQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ public static function orQueries($queryObjects)
$className = $queryObjects[$i]->className;
}
if ($className != $queryObjects[$i]->className) {
throw new Exception('All queries must be for the same class');
throw new Exception('All queries must be for the same class', 103);
}
}
$query = new self($className);
Expand Down
18 changes: 5 additions & 13 deletions src/Parse/ParseRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ public function getName()
public function setName($name)
{
if ($this->getObjectId()) {
throw new ParseException(
"A role's name can only be set before it has been saved."
);
throw new ParseException("A role's name can only be set before it has been saved.");
}
if (!is_string($name)) {
throw new ParseException(
"A role's name must be a string."
);
throw new ParseException("A role's name must be a string.", 139);
}

return $this->set('name', $name);
Expand Down Expand Up @@ -104,22 +100,18 @@ public function getRoles()
public function save($useMasterKey = false)
{
if (!$this->getACL()) {
throw new ParseException(
'Roles must have an ACL.'
);
throw new ParseException('Roles must have an ACL.', 123);
}
if (!$this->getName() || !is_string($this->getName())) {
throw new ParseException(
'Roles must have a name.'
);
throw new ParseException('Roles must have a name.', 139);
}
if ($this->getObjectId() === null) {
// Not yet saved, verify this name is not taken
// ParseServer does not validate duplicate role names as of parse-server v2.3.2
$query = new ParseQuery('_Role');
$query->equalTo('name', $this->getName());
if ($query->count(true) > 0) {
throw new ParseException("Cannot add duplicate role name of '{$this->getName()}'");
throw new ParseException("Cannot add duplicate role name of '{$this->getName()}'", 137);
}
}

Expand Down
Loading