API Results FailureInterface - evansims/openfga-php GitHub Wiki
Represents a failed result containing an error. Failure results indicate that an operation encountered an error and contain the throwable that caused the failure. They provide safe access to error information while maintaining compatibility with the Result pattern's fluent interface. Failure results behave predictably in all Result operations: - succeeded()
always returns false - failed()
always returns true - err()
returns the contained error safely - val()
throws since failures have no values - failure()
executes callbacks with the error - success()
skips callbacks and returns unchanged - then()
skips transformations and returns unchanged - recover()
applies recovery functions to the error - rethrow()
throws the error or provided throwable
Table of Contents
OpenFGA\Results
- Failure (implementation)
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): 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): 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|null $throwable = NULL): 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): 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): 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(): mixed
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.
mixed
— The response interface (for example CheckResponseInterface, StoreInterface)