Make WordPress Core

Changeset 35671


Ignore:
Timestamp:
11/18/2015 06:28:55 PM (10 years ago)
Author:
johnbillion
Message:

Update WP_REST_Response::as_error() to handle the new format error responses introduced in [35653].

Props danielbachhuber
Fixes #34551

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-response.php

    r34928 r35671  
    244244
    245245        if ( is_array( $this->get_data() ) ) {
    246             foreach ( $this->get_data() as $err ) {
    247                 $error->add( $err['code'], $err['message'], $err['data'] );
     246            $data = $this->get_data();
     247            $error->add( $data['code'], $data['message'], $data['data'] );
     248            if ( ! empty( $data['additional_errors'] ) ) {
     249                foreach( $data['additional_errors'] as $err ) {
     250                    $error->add( $err['code'], $err['message'], $err['data'] );
     251                }
    248252            }
    249253        } else {
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r35659 r35671  
    283283    }
    284284
     285    public function test_error_to_response_to_error() {
     286        $code     = 'wp-api-test-error';
     287        $message  = 'Test error message for the API';
     288        $code2    = 'wp-api-test-error-2';
     289        $message2 = 'Another test message';
     290        $error   = new WP_Error( $code, $message, array( 'status' => 400 ) );
     291        $error->add( $code2, $message2, array( 'status' => 403 ) );
     292
     293        $response = $this->server->error_to_response( $error );
     294        $this->assertInstanceOf( 'WP_REST_Response', $response );
     295
     296        $this->assertEquals( 400, $response->get_status() );
     297
     298        $error = $response->as_error();
     299        $this->assertInstanceOf( 'WP_Error', $error );
     300        $this->assertEquals( $code, $error->get_error_code() );
     301        $this->assertEquals( $message, $error->get_error_message() );
     302        $this->assertEquals( $message2, $error->errors[ $code2 ][0] );
     303        $this->assertEquals( array( 'status' => 403 ), $error->error_data[ $code2 ] );
     304    }
     305
    285306    public function test_rest_error() {
    286307        $data = array(
Note: See TracChangeset for help on using the changeset viewer.