Automattic\WooCommerce\Api\Infrastructure

GraphQLControllerBase::format_exceptionprivateWC 1.0

Format a caught exception into a GraphQL error array.

Method of the class: GraphQLControllerBase{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->format_exception( $e, $request, ?object $principal ): array;
$e(Throwable) (required)
The caught exception.
$request(WP_REST_Request) (required)
The REST request.
?object $principal(required)
.

GraphQLControllerBase::format_exception() code WC 10.9.4

private function format_exception( \Throwable $e, \WP_REST_Request $request, ?object $principal ): array {
	if ( $e instanceof ApiException ) {
		// Caller-supplied extensions come first so the canonical
		// getErrorCode() can't be silently overridden by an extensions
		// entry keyed 'code'. Mirrors the same invariant enforced by
		// Utils::translate_exceptions() for the execute/authorize paths.
		$error = array(
			'message'    => $e->getMessage(),
			'extensions' => array_merge(
				$e->getExtensions(),
				array( 'code' => $e->getErrorCode() )
			),
		);
	} elseif ( $e instanceof \InvalidArgumentException ) {
		$error = array(
			'message'    => $e->getMessage(),
			'extensions' => array( 'code' => 'INVALID_ARGUMENT' ),
		);
	} else {
		$error = array(
			'message'    => 'An unexpected error occurred.',
			'extensions' => array( 'code' => 'INTERNAL_ERROR' ),
		);
	}

	if ( $this->is_debug_mode( $principal, $request ) ) {
		$error['extensions']['debug'] = array(
			'message' => $e->getMessage(),
			'file'    => $e->getFile(),
			'line'    => $e->getLine(),
			'trace'   => $e->getTraceAsString(),
		);

		$chain = $this->extract_previous_chain( $e );
		if ( ! empty( $chain ) ) {
			$error['extensions']['debug']['previous'] = $chain;
		}
	}

	return $error;
}