Automattic\WooCommerce\Api\Infrastructure
ResolverHelpers::translate_exceptions
Invoke a callable, translating any thrown exception into a spec-compliant GraphQL error with a machine-readable code.
- ApiException → its own code + extensions, with the original message.
- InvalidArgumentException → INVALID_ARGUMENT, with the original message.
- Any other Throwable → INTERNAL_ERROR, with a generic message; the original throwable is attached as
previousfor debug-mode surfacing.
Public so that generated resolvers can wrap Code-API calls that happen outside the execute()/authorize() pair (e.g. the Connection::slice() call emitted for nested paginated connection fields, which can throw InvalidArgumentException when pagination bounds are exceeded).
Method of the class: ResolverHelpers{}
No Hooks.
Returns
Mixed. The return value of the callable.
Usage
$result = ResolverHelpers::translate_exceptions( $operation ): mixed;
- $operation(callable) (required)
- Callable to invoke.
ResolverHelpers::translate_exceptions() ResolverHelpers::translate exceptions code WC 10.9.1
public static function translate_exceptions( callable $operation ): mixed {
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Not HTML; serialized as JSON.
try {
return $operation();
} catch ( \Automattic\WooCommerce\Api\ApiException $e ) {
// Caller-supplied extensions come first so the canonical
// getErrorCode() can't be silently overridden by an extensions
// entry keyed 'code'. The invariant "the code on the wire
// equals ApiException::getErrorCode()" is worth enforcing.
throw new Error(
$e->getMessage(),
extensions: array_merge(
$e->getExtensions(),
array( 'code' => $e->getErrorCode() )
)
);
} catch ( \InvalidArgumentException $e ) {
throw new Error(
$e->getMessage(),
extensions: array( 'code' => 'INVALID_ARGUMENT' )
);
} catch ( \Throwable $e ) {
throw new Error(
'An unexpected error occurred.',
previous: $e,
extensions: array( 'code' => 'INTERNAL_ERROR' )
);
}//end try
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}