Skip to content

Commit 22b8f8a

Browse files
committed
fix: [#223] HTTP error status code trying to insert duplicate category in MySQL
Using MySQL the endpoint to inser categories returns a different HTTP status code. It should return a 400 and It was returning a 500. The reason is we parse the error message and for MySQL the error message is not the same as SQLite: MySQL: ``` Error: Duplicate entry 'category name 118802' for key 'torrust_categories.name' ``` It has been changed but we should now rely on concrete error messages. Besides we should not relay on the database contrains, mahybe we should check in the handler that the category does not exist.
1 parent c6346a5 commit 22b8f8a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/databases/mysql.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ impl Database for Mysql {
250250
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))
251251
.map_err(|e| match e {
252252
sqlx::Error::Database(err) => {
253-
if err.message().contains("UNIQUE") {
253+
if err.message().contains("Duplicate entry") {
254+
// Example error message when you try to insert a duplicate category:
255+
// Error: Duplicate entry 'category name SAMPLE_NAME' for key 'torrust_categories.name'
254256
database::Error::CategoryAlreadyExists
255257
} else {
256258
database::Error::Error

0 commit comments

Comments
 (0)