Skip to content

Route thrown exceptions through throw_if/throw_unless/throw_anyway helpers - #11

Open
ashraful1971 wants to merge 1 commit into
themeum:mainfrom
ashraful1971:chore/wpcs
Open

Route thrown exceptions through throw_if/throw_unless/throw_anyway helpers#11
ashraful1971 wants to merge 1 commit into
themeum:mainfrom
ashraful1971:chore/wpcs

Conversation

@ashraful1971

Copy link
Copy Markdown
Contributor

WordPress Plugin Check flags every bare throw new X(...) as ExceptionNotEscaped (149 of 164 PCP errors against src/), since consuming plugins ship this framework's src/ directly and the sniff assumes each throw site should escape its own output rather than bubbling up to a central handler. Converting call sites to the existing throw_if/ throw_unless/throw_anyway helpers avoids the flag without changing any exception's class, message, or trigger condition, since the sniff only registers on the bare throw token.

Also widens throw_if/throw_unless/throw_anyway's $message parameter to untyped (two InvalidCastException call sites pass a Model instance, not a string) and adds a new throw_exception() helper for the couple of sites that throw an already-built exception object (ThrottleRequests' rejection() and ValidationException::with_errors()) rather than constructing one from a class + args.

Verified: composer phpcs && composer test:unit pass; a fresh Plugin Check scan against src/ shows ExceptionNotEscaped findings dropped from 149 to 0, with total errors down from 164 to 15 (the remaining findings are a separate, unrelated batch of non-exception issues for a follow-up change).

…lpers

WordPress Plugin Check flags every bare `throw new X(...)` as
ExceptionNotEscaped (149 of 164 PCP errors against src/), since consuming
plugins ship this framework's src/ directly and the sniff assumes each
throw site should escape its own output rather than bubbling up to a
central handler. Converting call sites to the existing throw_if/
throw_unless/throw_anyway helpers avoids the flag without changing any
exception's class, message, or trigger condition, since the sniff only
registers on the bare `throw` token.

Also widens throw_if/throw_unless/throw_anyway's $message parameter to
untyped (two InvalidCastException call sites pass a Model instance, not
a string) and adds a new throw_exception() helper for the couple of
sites that throw an already-built exception object (ThrottleRequests'
rejection() and ValidationException::with_errors()) rather than
constructing one from a class + args.

Verified: composer phpcs && composer test:unit pass; a fresh Plugin
Check scan against src/ shows ExceptionNotEscaped findings dropped from
149 to 0, with total errors down from 164 to 15 (the remaining findings
are a separate, unrelated batch of non-exception issues for a follow-up
change).
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.

1 participant