Skip to content

Add a way to cleanup error and exception handlers #1698

@simPod

Description

@simPod
Contributor

Problem Statement

PHPUnit 11 checks for any leftovers in error handlers sebastianbergmann/phpunit#5619

Solution Brainstorm

When \Sentry\ErrorHandler::registerOnceErrorHandler() is called, error handler is registered.

It would be nice to add degisterOnceErrorHandler(). Since the error handler is kept in ErrorHandler's state, it would be easy to deregister it.

Activity

moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Feb 7, 2024
changed the title [-]Add a way to cleanup error handler[/-] [+]Add a way to cleanup error and exception handlers[/+] on Feb 7, 2024
cleptric

cleptric commented on Feb 7, 2024

@cleptric
Member

That makes sense, let me try to use PHPUnit 11 in the SDK tests and see what happens 😄

moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Feb 7, 2024
moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Feb 7, 2024
moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Feb 7, 2024
self-assigned this
on Feb 7, 2024

4 remaining items

simPod

simPod commented on Feb 20, 2024

@simPod
ContributorAuthor

When doing full app integration tests I want to simulate the same environment as is in prod in order to test exactly the same thing that runs there.

moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Feb 20, 2024
stayallive

stayallive commented on Feb 20, 2024

@stayallive
Collaborator

And how would you have setup the degisterOnceErrorHandler in your test suite if it existed?

Is not registering the error handler integrations not an good compromise here? I'm assuming your integration tests don't test for unhandled or fatal errors caught by Sentry? Or do they?

moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Feb 20, 2024
simPod

simPod commented on Feb 22, 2024

@simPod
ContributorAuthor

Is not registering the error handler integrations not an good compromise here? I'm assuming your integration tests don't test for unhandled or fatal errors caught by Sentry? Or do they?

Sure but then my tests skip some part of code that get's executed in prod and therefore do not cover it.


Currently my workaround in teardown looks similar to this:

        $res = [];

        while (true) {
            $previousHandler = set_exception_handler(static fn () => null);
            restore_exception_handler();

            if ($previousHandler instanceof Closure) {
                $reflection = new ReflectionFunction($previousHandler);
                if (
                    $reflection->getClosureThis() instanceof \Sentry\ErrorHandler
                    && $reflection->getName() === 'handleException'
                ) {
                    restore_exception_handler();

                    continue;
                }
            }

            if ($previousHandler === null) {
                break;
            }

            $res[] = $previousHandler;
            restore_exception_handler();
        }

        $res = array_reverse($res);

        foreach ($res as $handler) {
            set_exception_handler($handler);
        }

as imperfect as it may be, it allowed me to run my tests with phpunit 11 without compromising coverage.

moved this to Waiting for: Product Owner in GitHub Issues with 👀 2on Feb 22, 2024
moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 2on Feb 23, 2024
simPod

simPod commented on May 15, 2024

@simPod
ContributorAuthor

@cleptric what is the sentry's statement on this?

cleptric

cleptric commented on May 15, 2024

@cleptric
Member

What @stayallive said.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @simPod@stayallive@cleptric@stephanie-anderson

      Issue actions

        Add a way to cleanup error and exception handlers · Issue #1698 · getsentry/sentry-php