graphql-php icon indicating copy to clipboard operation
graphql-php copied to clipboard

Unable to overload Error 'path' when extending errors

Open megbailey opened this issue 2 years ago • 5 comments

Hi!

I'm working on creating a set of custom defined errors according to advice @simPod gave awhile back here #549. For some of my errors, I would like to give a more robust path for what failed during query execution. For example, my NotFoundError I would like to be able to pass a custom $path to tell the api user what field the query failed on.

/**
 * 404 Not found.
 * User tried to get, reference, or update a record that does not exist.
 */
class NotFoundError extends Error implements ClientAware {

	public function __construct(string $message, array $path) {
		parent::__construct(
			$message,
			null,
			null,
			null,
			$path,
			null,
			[ 'status_code' => 404 ]
		);
	}

	public function isClientSafe(): bool { return true; }

}

But when I receive the NotFoundError on the client-side the error's path !== the path that was given to the error constructor. Is this a bug or is overriding path functionality that does not exist? Thanks!

megbailey avatar Nov 13 '24 19:11 megbailey

I'd implementing ClientAware and ProvidesExtensions and pass the custom path via extensions.

simPod avatar Nov 13 '24 20:11 simPod

Oh ok that makes sense. Im curious though in what situation is Error's path used? Because it seems like the path from createLocatedError will always take precedent.

megbailey avatar Nov 13 '24 20:11 megbailey

But when I receive the NotFoundError on the client-side the error's path !== the path

I can't really help, this does not seem right. But also I don't know everything about graphql-php internals. You should debug your app why is it so.

I looked into Error class and did not found any place where passed $path would get overridden.

simPod avatar Nov 13 '24 20:11 simPod

Ok no worries I appreciate your help and thoughts thus far. I have a path forward with using extensions and others will to, but the Error path bit does seem to be unexpected behavior.

megbailey avatar Nov 13 '24 20:11 megbailey

The path is always determined from the field that was being executed as the error is thrown. As @simPod, you may add additional information through extensions.

spawnia avatar Nov 18 '24 08:11 spawnia