#How to Ignore TypeError in PHP Laravel?

43 messages · Page 1 of 1 (latest)

hushed basalt
#

Hi, so I am trying ignore some type errors in a livewire/filament app that is safe to ignore in a livewire app

However it is not working. I still keep getting notified from time to time

In my config/sentry.php I had this

'before_send' => [App\Exceptions\Sentry::class, 'beforeSend'],

In my Sentry class i had this

class Sentry
{
    public static function beforeSend(Event $event, ?EventHint $hint): ?Event
    {
        if ($hint === null || $hint->exception === null) {
            return $event;
        }

        $exceptionMsg = $hint->exception->getMessage();

        if (str_contains($exceptionMsg, 'App\Filament\PanelName\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\ModelName, null given')) {
            return null;
        }

        return $event;
    }
}

Yet i still keep getting TypeError notification in Sentry

foggy siren
#

Do all exceptions have the exact same message?

hushed basalt
#

but it is a TypeError, not an Exception so maybe that's why it didn't catch it

#

but not sure how do i catch a specific TypeError and not report it to sentry

foggy siren
#

I can't really see why this shouldn't work

hushed basalt
#

but fact is it didn't work 😦

foggy siren
#

does it work locally?

#

From the SDK PoV, if you return null from before_send, we drop the event

#

As you provide the callback code, can't really help here unfortunately

hushed basalt
#

i had other exceptions ignored, it seem that TypeError will always get reported to sentry regardless of what i ignore in this Sentry.php file so far

#

so my hunch is TypeError r not treated as exceptions

#

but i don't see any $hint->typeError avaliable

foggy siren
#

It's under exceptions

hushed basalt
#

Previously i used Flare i also had this issue, where TypeError are not treated as exceptions hence always get reported.

I eventually found a solution but i forgotten about it. Then i moved to Sentry and this happened again

foggy siren
#

I would actually access it via $event-getExceptions()

hushed basalt
#

if ur fine i can DM u more details

foggy siren
#
    'before_send' => function (Event $event, ?EventHint $hint) {
        dump($event->getExceptions());
        dump($hint);
        exit;
    },
#
    'before_send' => function (Event $event, ?EventHint $hint) {
        if ($hint === null || $hint->exception === null) {
            return $event;
        }

        $exceptionMsg = $hint->exception->getMessage();
        if (str_contains($exceptionMsg, 'App\Services\FooService::set(): Argument #1 ($bar) must be of type int, string given')) {
            return null;
        }

        return $event;
    },

works for me

#

Would be interesssting to see if the hint is empty in yourcase

hushed basalt
#

hmm this is kind of hard to reproduce locally bcos it usually only happen if the user stays on the same page for a really long time then try to open a livewire modal, but in production it happen enough times to be an annoyance

Will see what i can find out and get back to u again

foggy siren
#

Are you purging your config cache during deploys?
This could also be the curlpit.

hushed basalt
#

i clear and cache again during deploys

#

is ur before_send like mine or is urs inside breadcrumbs or tracing?

foggy siren
#

top level, your's is correct!

#

And using a callable, like in your case, is prefered, as without it, config caching no longer works.
My example was just a quick repro 🙂

hushed basalt
#

a screenshot of the error in sentry. it looks correct too. Really puzzling why ignoring doesn't work

#

As for my bootstrap/app.php (Laravel 11), it looks like this for the exceptions part

#

Integration::handles($exceptions) is the code sentry documentation asked me to do

foggy siren
#

Yep, this looks good as well

hushed basalt
#

so if i did everything correctly, this is a bug with the sentry library?

foggy siren
#

I can't rellay confirm this, in my testing I'm able to ignore type errors just fine.

hushed basalt
#

hmm i see

#

alright thank you. if i discover anything new i let u know again

foggy siren
#

Here ware are setting $hint->exception = $exception;, and this method is called by Integration::handles($exceptions)

#

So maybe the condition if (str_contains($exceptionMsg, 'App\Filament\PanelName\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\ModelName, null given')) never eveluates to true.

hushed basalt
#

is there any chance i need to do escaping like \\ & \( .etc?

#

Another exception i'm not able to ignore is deprecation notice.

My code is as follow

if (str_contains($exceptionMsg, 'Since web-token/jwt-library 3.3.0: The parameter "$contentEncryptionAlgorithmManager" is deprecated and will be removed in 4.0.0.')) {
            return null;
        }

But it doesn't work. In that library the deprecation notice code is this

trigger_deprecation(
                'web-token/jwt-library',
                '3.3.0',
                'The parameter "$contentEncryptionAlgorithmManager" is deprecated and will be removed in 4.0.0. Please set all algorithms in the first argument and set "null" instead.'
            );

and the trigger_deprecation function seem to be a helper function in symfony

function trigger_deprecation(string $package, string $version, string $message, mixed ...$args): void
    {
        @trigger_error(($package || $version ? "Since $package $version: " : '').($args ? vsprintf($message, $args) : $message), \E_USER_DEPRECATED);
    }

Are deprecation notice treated as exceptions that can be ignored too? I want to ignore this specific deprecation notice but my code doesn't seem to work for it

foggy siren
#

Do you want to not report some deprecations or all?

hushed basalt
#

is it possible to just ignore certain deprecations?

#

for example, deprecation in my own codebase most of the time i like to catch unless something special that makes me decide to ignore

But deprecation in external libraries i more likely want to ignore it