How to access identity object in ErrorController?

It seems all the identity attributes are missing inside the ErrorController. Why is that and how can I access identity object inside the ErrorController?

The ErrorController can be used like any other Controller.
Therefore if you need identity data you can use the Authentication Component like

$identity = $this->Authentication->getIdentity();

Are you sure? It’s not entirely like other controllers. It skips a lot of the things that might cause errors to happen so as to most reliably report errors instead of getting into an infinite loop of error handling.

The error controller will be created with the request object that the error handler middleware receives.

Authentication information is being added at a later point, and since the request object is immutable, the request object used by the error controller will never know about it.

Some details explaining what the identity information should be used for in the error controller / view might help suggesting possible alternatives. You might be tempted to for example access the session directly to fetch the information (in case you are using the session authenticator), but you should very much try to avoid that, as that would create a dependency to an implementation detail, making it harder to change the implementation.

Indeed my comment above is completely false. I didn’t check it before posting it, so shame on me.
So there is no way to get the current identity when an error occurs? :thinking:

No built-in ones, but there certainly are ways, though I don’t really know of any overly nice or straightforward ones.