#41370 closed defect (bug) (fixed)
REST API: Attempting to create an existing term yields HTTP 500
Reported by: | jnylen0 | Owned by: | kadamwhite |
---|---|---|---|
Milestone: | 4.9 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | good-first-bug has-unit-tests has-patch commit |
Focuses: | rest-api | Cc: |
Description
Reported at https://github.com/WordPress/gutenberg/pull/1490. I haven't had time to investigate yet, but this should be a fairly quick win.
HTTP 5xx error codes should be reserved for unexpected server errors like database failures, out of memory, etc. This one should be 400 Bad Request
or maybe 409 Conflict
instead.
Attachments (2)
Change History (21)
This ticket was mentioned in Slack in #core by flixos90. View the logs.
7 years ago
#3
@
7 years ago
Agree on this. I was testing request to /wp-json/wp/v2/categories and found that it returns status 500 error when attempting to create an existing term.
Tested on
- WP: 4.9-alpha-40870-src
- REST URL: /wp-json/wp/v2/categories
- Request Body:
- name: "test"
Try post this twice and you will get
- Response Status: 500
- Response Body:
{ "code": "term_exists", "message": "A term with the name provided already exists with this parent.", "data": 3 }
#4
@
7 years ago
@alibasheer I was checking at wp-includes/taxonomy.php: line 1990
on function wp_insert_term().
Would it be better if we change all of return new WP_Error();
in that function to return bad_request 400
instead of 500
?
Seems all of WP_Error in this function shouldn't be 500
neither.
#5
@
7 years ago
Hi @alibasheer
Sorry, I still can't find the way to patch code in (also new here too 😁).
But from your patch, if you want to make it to be 400
status code, you can simply add status
array in the first param of $term->add_data();
So from
<?php $term->add_data( $existing_term->term_id, 'term_exists' );
Could be
<?php $term->add_data( array( 'status' => 400, 'existing_term_id' => $existing_term->term_id ), 'term_exists' );
You will get an output
{ "code": "term_exists", "message": "A term with the name provided already exists with this parent.", "data": { "status": 400, "existing_term_id": 1 } }
with status 400 Bad Request
.
Sorry that I can't help patch it by myself.
#6
@
7 years ago
- Keywords has-unit-tests has-patch added; needs-unit-tests needs-testing removed
Looked at @alibasheer's patch and wrote an updated one that returns an HTTP 409 if the term already exists.
Unit test also included. The only thing I wasn't quite sure of is that I put the unit tests in the rest-categories-controller.php. I did this as it already had the test to create a category, so this was an easy addition. Not sure if it should have been somewhere more generic though. Open to suggestions.
This ticket was mentioned in Slack in #core by shooper. View the logs.
7 years ago
This ticket was mentioned in Slack in #core-restapi by shooper. View the logs.
7 years ago
This ticket was mentioned in Slack in #core-restapi by shooper. View the logs.
7 years ago
#10
@
7 years ago
- Milestone changed from Future Release to 4.9
- Owner set to kadamwhite
- Status changed from new to accepted
#13
@
7 years ago
Any reason as to why the existing term ID is no longer returned? Previously when going to create a term if it exists on the site you are trying to post to, it will just return the error along with the term ID so you can pass that along when creating a post so it assigns the correct taxonomy ID. This stopped returning.
Hello @jnylen0,
I added patch 41370.patch, this is my first patch, please let me know your feedback if I am doing it right. Here are my thoughts:
409 Conflict
as it is trying to create existing term, but I checked what you are returning in other cases like when creating existing post, or existing user and found you are sending400 Bad Request
. I ended up setting this to400 Bad Request
data
directly, but now I am returning an array of 2 elements in thedata
, theterm_id
andstatus