Skip to content

Add TYPEPHP_DEBUG to surface exceptions TypePHP swallows - #130

Closed
naxvog wants to merge 1 commit into
typephp-php:mainfrom
naxvog:feat/debug-swallowed-errors
Closed

naxvog wants to merge 1 commit into
typephp-php:mainfrom
naxvog:feat/debug-swallowed-errors

Conversation

@naxvog

@naxvog naxvog commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

src/ has 60 catch (\Throwable) blocks (outside the CLI) that discard the exception so TypePHP stays fail-open: 45 have an empty body, 15 return a fallback. When one fires, a check is skipped or a whole file is left uninstrumented, and there is no way to find out why. StreamWrapper::transformSource() returning the untouched source after a parse error is one example.

Change

  • New TypePHP\Internal\Diagnostic\DebugLog::swallowed(\Throwable $e). It does nothing unless the TYPEPHP_DEBUG environment variable is truthy. When set, it writes the exception class, message, throw site and catch site to the PHP error log:

    [TypePHP Debug] Swallowed PhpParser\Error: Syntax error, unexpected '{', expecting T_VARIABLE on line 1 (thrown at …/ParserAbstract.php:370, swallowed at …/StreamWrapper.php:277)
    
  • Every one of the 60 catch blocks calls it as its first statement. Blocks that used catch (\Throwable) now name the variable. No other logic is touched.

  • New Config::isDebugEnabled(), env only, read like the other TYPEPHP_* flags.

Default behaviour is unchanged. The extra cost is one static call on a path that already threw.

Verification

  • New tests/Internal/Diagnostic/DebugLogTest.php: silent by default, logs when enabled, and reports an unparsable source file.
  • Full suite on PHP 8.4: 2584 passed. PHPStan: no errors. Pint: no new findings.

Notes

  • The diff is wide (20 source files) but mechanical. Blank lines after the new call come from Pint.
  • Many of these catches fire on malformed docblocks by design, so debug output can be noisy. That is why it is opt-in and not tied to a violation mode.
  • The variable is not documented in the README yet.

60 catch (\Throwable) blocks across the transformer, docblock parser and
runtime checkers discard the exception to stay fail-open. When one of
them fires, a check is skipped or a file is left uninstrumented with no
trace of why.

Route every one of them through DebugLog::swallowed(), a no-op unless
the TYPEPHP_DEBUG environment variable is truthy. When set, the
exception class, message, throw site and catch site go to the PHP error
log. Default behaviour is unchanged.
@naxvog
naxvog force-pushed the feat/debug-swallowed-errors branch from 3d35b6a to 3db4b2a Compare November 11, 2026 08:35
@rcalicdan

Copy link
Copy Markdown
Member

@naxvog Hello I made a lot of changes on Internal code on the library in my branch and this PR will cause a lot of of merge conflict.. I'm Going to close this PR for now and defer this feature after the next release..

@rcalicdan rcalicdan closed this Nov 12, 2026
@rcalicdan
rcalicdan self-requested a review November 12, 2026 06:22
@rcalicdan rcalicdan self-assigned this Nov 12, 2026
@rcalicdan rcalicdan reopened this Dec 2, 2026
@rcalicdan

Copy link
Copy Markdown
Member

@naxvog Can you implement a recent version of this PR.. so this library can have feature for surfacing exceptions that this tool swallows..

@naxvog

naxvog commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Done, the new version is #144. It is rebuilt on current main and now covers all 89 catch blocks that discard their exception (the refactoring added about 30 since this PR). Behaviour is the same as here: nothing changes unless TYPEPHP_DEBUG is set.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants