webmozarts / assert

Assertions to validate method input/output with nice error messages.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add context to exceptions

denis-chmel opened this issue · comments

Hi @webmozart and guys,
Are you interested in adding context to a thrown exception?
E.g.

        Assert::null($error, 'Api request must not return error', [
            'userId' => $currentUser->getId(),
            'apiRequest' => $request,
            'apiResponce' => $response,
            'error' => $error, // why not in the message - it could be too unique message, causing say Sentry to create a lot of duplicates, say usually happens to SQL unique index errors.
        ]);

this is possible to add now to custom exception that support context, thanks to
https://github.com/webmozart/assert/pull/19/files

The context is some payload which is usually useful (when stacktrace is not enough).
Basically, I need to add array $context = [] as 3rd (sometimes 4th) argi to all assert::whatever() methods to then pass it further to throw new InvalidArgumentException($message, 0, null, $context ?: null);

I will draw a PR if this aligns with your thoughts.

Adding a new parameter to the createInvalidArgumentException, even an optional one, would be a BC break, so i'm not a big fan of doing so currently.

It may be possible to add it in version 2.0, though there are no real plans for that right now

Makes sense, thank you.