Skip to content

Commit

Permalink
Make Result::assertErrorFree() return void
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Oct 10, 2024
1 parent 6a42958 commit c286eed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- Make `Result::assertErrorFree()` return void

## v0.33.1

### Fixed
Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,35 @@ $result = \Example\Api\Operations\HelloSailor::execute();
```

The returned `$result` is going to be a class that extends `\Spawnia\Sailor\Result` and
holds the decoded response returned from the server. You can just grab the `$data`, `$errors`
or `$extensions` off of it:
holds the decoded response returned from the server.
You can just grab the `$data`, `$errors` or `$extensions` off of it:

```php
$catchResult->data // `null` or a generated subclass of `\Spawnia\Sailor\ObjectLike`
$catchResult->errors // `null` or a list of `\Spawnia\Sailor\Error\Error`
$catchResult->extensions // `null` or an arbitrary map
$result->data // `null` or a generated subclass of `\Spawnia\Sailor\ObjectLike`
$result->errors // `null` or a list of `\Spawnia\Sailor\Error\Error`
$result->extensions // `null` or an arbitrary map
```

### Error handling

You can ensure your query returned the proper data and contained no errors:
You can ensure an operation returned the proper data and contained no errors:

```php
$errorFreeResult = \Example\Api\Operations\HelloSailor::execute()
->errorFree(); // Throws if there are errors or returns an error free result
```

If you don't need any data, but want to ensure a mutation succeeded:
The `$errorFreeResult` is going to be a class that extends `\Spawnia\Sailor\ErrorFreeResult`.
Given it can only be obtained by going through validation,
it is guaranteed to have non-null `$data` and does not have `$errors`:

```php
$errorFreeResult->data // a generated subclass of `\Spawnia\Sailor\ObjectLike`
$errorFreeResult->extensions // `null` or an arbitrary map
```

If you do not need to access the data and just want to ensure a mutation was successful,
the following is more efficient as it does not instantiate a new object:

```php
\Example\Api\Operations\SomeMutation::execute()
Expand Down
6 changes: 1 addition & 5 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,11 @@ static function (\stdClass $raw) use ($endpoint): Error {
* Throw an exception if errors are present in the result.
*
* @throws ResultErrorsException
*
* @return $this
*/
public function assertErrorFree(): self
public function assertErrorFree(): void
{
if (isset($this->errors)) {
throw new ResultErrorsException($this->errors, static::config(), static::endpoint());
}

return $this;
}
}

0 comments on commit c286eed

Please sign in to comment.