#42742 closed enhancement (fixed)
Add a has_errors() method to WP_Error
Reported by: | DrewAPicture | Owned by: | robdxw |
---|---|---|---|
Milestone: | 5.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | has-unit-tests has-dev-note |
Focuses: | Cc: |
Description
We should add a method to easily check whether a WP_Error
object contains any errors.
There isn't any one standard way to perform such an operation, and in fact, a cursory search of core yielded a variety of methods for checking whether there are any errors in the object, but nothing really consistent:
- Checking
empty()
on the$errors
property - Checking if the count from
get_error_codes()
is greater than 0 - Checking if the value of
get_error_code()
is an empty string
My proposal would be to introduce a boolean has_errors()
method, or perhaps empty()
that simply checks whether the $errors
property is empty or not.
Attachments (3)
Change History (12)
#2
@
7 years ago
I've submitted a patch adding the has_errors()
method & tests. This is my first patch, so please let me know if anything needs changing.
#3
@
7 years ago
- Milestone changed from Awaiting Review to 5.0
Hi @robdxw, thanks for the patch! It looks good to me.
As a next step, we could update all the instances in WordPress core that currently check if a WP_Error
object contains any errors in one way or another (as outlined in the ticket description) to use the new method instead.
#5
@
7 years ago
- Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
- Owner set to robdxw
- Status changed from new to assigned
42742.diff just updates @robdxw's latest patch to fix the simplified @covers
annotations introduced in [42646].
Also assigns the ticket to @robdxw to mark the good-first-bug as "claimed".
#7
@
6 years ago
- Keywords needs-patch added; good-first-bug has-patch removed
- Milestone changed from 5.0 to 5.1
- Resolution fixed deleted
- Status changed from closed to reopened
@since
needs updating
#9
@
6 years ago
- Keywords has-dev-note added; needs-patch removed
This was mentioned in a 5.1 dev note: https://make.wordpress.org/core/2019/01/23/miscellaneous-developer-focused-changes-in-5-1/
WP_Error
full tests coverage