WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 3 months ago

#42742 closed enhancement (fixed)

Add a has_errors() method to WP_Error

Reported by: DrewAPicture Owned by: robdxw
Milestone: 5.0 Priority: normal
Severity: normal Version:
Component: General Keywords: good-first-bug has-patch has-unit-tests
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)

42742-wp-error-tests.diff (26.8 KB) - added by DrewAPicture 6 months ago.
WP_Error full tests coverage
42742_wp_error_has_errors_method.diff (1.7 KB) - added by robdxw 4 months ago.
Add has_errors() method and tests
42742.diff (1.6 KB) - added by DrewAPicture 4 months ago.

Download all attachments as: .zip

Change History (9)

@DrewAPicture
6 months ago

WP_Error full tests coverage

#1 @DrewAPicture
6 months ago

In 42255:

General: Add complete test coverage for WP_Error.

See #42742.

@robdxw
4 months ago

Add has_errors() method and tests

#2 @robdxw
4 months 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 @SergeyBiryukov
4 months 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.

#4 @DrewAPicture
4 months ago

In 42646:

Tests: Simplify @covers annotations for WP_Error test coverage. Also fix a typo in a test method name.

See #42742.

@DrewAPicture
4 months ago

#5 @DrewAPicture
4 months 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".

#6 @SergeyBiryukov
3 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 42761:

General: Introduce WP_Error::has_errors() method and use it where appropriate.

Props robdxw, DrewAPicture, SergeyBiryukov.
Fixes #42742.

Note: See TracTickets for help on using tickets.