API Results Failure - evansims/openfga-php GitHub Wiki
Concrete implementation of a failed result containing an error. This class represents the failed outcome of an operation, storing the error that caused the failure and providing safe access through the Result pattern's fluent interface.
Table of Contents
OpenFGA\Results
- FailureInterface (interface)
public function err(): Throwable
Retrieves the error from a failed result. This method should only be called on Failure results. Use failed() to check the result type before calling this method to avoid exceptions.
Throwable
— The error that caused the failure
public function failed(): bool
Determines if this result represents a failure.
bool
— True if this is a Failure result, false if it's a Success
public function failure(callable $fn): OpenFGA\Results\ResultInterface
Executes a callback when the result is a failure and continues the chain. The callback receives the error as its parameter and is only executed for Failure results. This method always returns the original result unchanged.
Name | Type | Description |
---|---|---|
$fn |
callable |
ResultInterface
— The original result for method chaining
public function recover(callable $fn): OpenFGA\Results\ResultInterface
Recovers from a failure by transforming it into a success or different failure. The callback is only executed for Failure results and can return either a new Result or a plain value (which becomes a Success). Success results pass through unchanged.
Name | Type | Description |
---|---|---|
$fn |
callable |
ResultInterface
— The recovered result or original success
public function rethrow(?Throwable $throwable = NULL): OpenFGA\Results\ResultInterface
Throws the contained error or continues the chain. For Failure results, this throws either the provided throwable or the contained error. For Success results, this method has no effect and returns the result unchanged.
Name | Type | Description |
---|---|---|
$throwable |
Throwable | null
|
Optional throwable to throw instead of the contained error |
ResultInterface
— The original result for method chaining
public function succeeded(): bool
Determines if this result represents a success.
bool
— True if this is a Success result, false if it's a Failure
public function success(callable $fn): OpenFGA\Results\ResultInterface
Executes a callback when the result is a success and continues the chain. The callback receives the success value (specific response interface) as its parameter and is only executed for Success results. This method always returns the original result unchanged.
Name | Type | Description |
---|---|---|
$fn |
callable |
ResultInterface
— The original result for method chaining
public function then(callable $fn): OpenFGA\Results\ResultInterface
Transforms a successful result using a callback and continues the chain. The callback is only executed for Success results and receives the specific response interface as its parameter. It can return either a new Result or a plain value (which becomes a Success). Failure results pass through unchanged.
Name | Type | Description |
---|---|---|
$fn |
callable |
ResultInterface
— The transformed result or original failure
public function unwrap(?callable $fn = NULL): mixed
Extracts the value from the result or applies a transformation. Without a callback, this returns the success value (specific response interface) or throws the failure error. With a callback, the function is called with either the response interface or failure error, and its return value is returned instead of throwing.
Name | Type | Description |
---|---|---|
$fn |
callable | null
|
mixed
— The response interface, callback result, or throws the error
public function val(): never
Retrieves the value from a successful result. This method should only be called on Success results. Use succeeded() to check the result type before calling this method to avoid exceptions. Returns the specific response interface documented in the calling method's @return annotation.
never
— The response interface (for example CheckResponseInterface, StoreInterface)