WordPress.org

Make WordPress Core

Opened 14 months ago

#38777 new defect (bug)

Add method to merge one WP_Error into another

Reported by: rmccue Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: rest-api Cc:

Description

WP_Error can store multiple error messages and codes, but this requires building the error object manually. If you have two existing error instances and want to merge them into a single one, you have to do this manually:

foreach ( $other_errors as $code => $messages ) {
	foreach ( $messages as $message ) {
		$error->add( $code, $message );
	}
	if ( $error_data = $error->get_error_data( $code ) ) {
		$error->add_data( $error_data, $code );
	}
}

While this multi-error feature isn't widely used, it's very helpful in the REST API, where multi-errors are natively supported via an additional_errors key in the error response.

Change History (0)

Note: See TracTickets for help on using tickets.