Skip to content

Commit

Permalink
fix: [torrust#223] HTTP error status code trying to insert duplicate …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
josecelano committed Aug 9, 2023
1 parent c6346a5 commit 22b8f8a
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ impl Database for Mysql {
.map(|v| i64::try_from(v.last_insert_id()).expect("last ID is larger than i64"))
.map_err(|e| match e {
sqlx::Error::Database(err) => {
if err.message().contains("UNIQUE") {
if err.message().contains("Duplicate entry") {
// Example error message when you try to insert a duplicate category:
// Error: Duplicate entry 'category name SAMPLE_NAME' for key 'torrust_categories.name'
database::Error::CategoryAlreadyExists
} else {
database::Error::Error
Expand Down

0 comments on commit 22b8f8a

Please sign in to comment.