WordPress.org

Make WordPress Core

Changeset 41737


Ignore:
Timestamp:
10/04/2017 04:23:33 PM (4 years ago)
Author:
kadamwhite
Message:

REST API: Return 409 status when attempting to create an existing term.

Fixes an issue where submitting a well-formed request to create a term inappropriately returns a 500 error status if that term already exists.
HTTP 5xx error codes should be reserved for unexpected server errors, so "409 Conflict" is a more appropriate response.

Props alibasheer, guzzilar, shooper.
Fixes #41370.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r41731 r41737  
    406406                $existing_term = get_term( $term_id, $this->taxonomy );
    407407                $term->add_data( $existing_term->term_id, 'term_exists' );
     408                $term->add_data( array( 'status' => 409 ) );
    408409            }
    409410
  • trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php

    r39896 r41737  
    603603    }
    604604
     605    /**
     606     * @ticket 41370
     607     */
     608    public function test_create_item_term_already_exists() {
     609        wp_set_current_user( self::$administrator );
     610        $request = new WP_REST_Request( 'POST', '/wp/v2/categories' );
     611        $request->set_param( 'name', 'test' );
     612        $response = $this->server->dispatch( $request );
     613        $this->assertEquals( 201, $response->get_status() );
     614        $response = $this->server->dispatch( $request );
     615        $this->assertEquals( 409, $response->get_status() );
     616        $data = $response->get_data();
     617        $this->assertEquals( 'term_exists', $data['code'] );
     618    }
     619
    605620    public function test_create_item_invalid_taxonomy() {
    606621        wp_set_current_user( self::$administrator );
Note: See TracChangeset for help on using the changeset viewer.