Skip to content

Commit

Permalink
Fix invalid error handling from Expo: error is not an int but a string (
Browse files Browse the repository at this point in the history
#10)

* Fix invalid error handling from Expo: error is not an int but a string

* Append error code and cast empty data array

Co-authored-by: Joris Berthelot <[email protected]>
Co-authored-by: ctwillie <[email protected]>
  • Loading branch information
3 people authored Oct 26, 2021
1 parent 242df3b commit 040f5d0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/ExpoErrorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ public function getErrorFromResult(array $response, int $statusCode): ExpoExcept
}

$error = $response['errors'][0];
$message = $error['message'];
$code = $error['code'];

return new ExpoException($error['message'], $error['code']);
if (is_string($code)) {
$message = "{$code}: {$message}";
$code = $statusCode;
}

return new ExpoException($message, $code);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/ExpoMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,13 @@ public function setTo($tokens): self
*/
public function setData($data = null): self
{
if (gettype($data) === 'array' && empty($data)) {
$data = new \stdClass();
}

if ($data !== null && ! is_object($data) && ! Utils::isAssoc($data)) {
throw new ExpoMessageException(sprintf(
'Message data must be either an associative array, object or null. % given',
'Message data must be either an associative array, object or null. %s given',
gettype($data)
));
}
Expand Down
8 changes: 6 additions & 2 deletions tests/ExpoErrorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ExpoErrorManagerTest extends TestCase
'errors' => [
[
'message' => 'Some expo error',
'code' => 400,
'code' => 'VALIDATION_ERROR',
],
],
];
Expand Down Expand Up @@ -71,7 +71,11 @@ public function get_error_from_result_returns_exception()
$exception
);
$this->assertSame(
$this->errorResponse['errors'][0]['message'],
sprintf(
'%s: %s',
$this->errorResponse['errors'][0]['code'],
$this->errorResponse['errors'][0]['message']
),
$exception->getMessage()
);
$this->assertSame(400, $exception->getCode());
Expand Down
6 changes: 3 additions & 3 deletions tests/ExpoMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function throws_exception_if_data_is_not_null_object_or_assoc_array()
$data = ['foo'];

$this->expectExceptionMessage(sprintf(
'Message data must be either an associative array, object or null. % given',
'Message data must be either an associative array, object or null. %s given',
gettype($data)
));

Expand All @@ -80,15 +80,15 @@ public function can_create_message_from_array()
$message = (new ExpoMessage([
'title' => 'test title',
'body' => 'test body',
'data' => ['test' => 'data'],
'data' => [],
'to' => ['ExponentPushToken[valid-token]', 'invalid-token]'],
]))->toArray();
$expected = [
'mutableContent' => false,
'priority' => 'default',
'title' => 'test title',
'body' => 'test body',
'data' => ['test' => 'data'],
'data' => new \stdClass(),
'to' => ['ExponentPushToken[valid-token]'],
];

Expand Down

0 comments on commit 040f5d0

Please sign in to comment.