Automattic\WooCommerce\Api\Infrastructure

MetadataController::build_metadata_query_authorization_errorprivate staticWC 1.0

Build the GraphQL error thrown when _apiMetadata is queried by a principal that cannot. Mirrors ResolverHelpers::build_authorization_error()'s UNAUTHORIZED / FORBIDDEN distinction so clients can branch on extensions.code the same way they do for field-level denies.

Method of the class: MetadataController{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = MetadataController::build_metadata_query_authorization_error( ?object $principal ): Error;
?object $principal(required)
.

MetadataController::build_metadata_query_authorization_error() code WC 10.9.1

private static function build_metadata_query_authorization_error( ?object $principal ): Error {
	$is_anonymous = null === $principal
		|| ( method_exists( $principal, 'is_authenticated' ) && ! $principal->is_authenticated() );
	return new Error(
		$is_anonymous ? 'Authentication required.' : 'You do not have permission to perform this action.',
		extensions: array( 'code' => $is_anonymous ? 'UNAUTHORIZED' : 'FORBIDDEN' )
	);
}