Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Handle error code 1048 on MySQL #81

Merged
merged 1 commit into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions src/connector/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod tests {
use url::Url;

lazy_static! {
static ref CONN_STR: String = env::var("TEST_MYSQL").unwrap();
static ref CONN_STR: String = env::var("TEST_MYSQL").expect("TEST_MYSQL env var");
}

#[test]
Expand Down Expand Up @@ -464,22 +464,48 @@ VALUES (1, 'Joe', 27, 20000.00 );
.await
.unwrap();

let res = conn
.query_raw("INSERT INTO test_null_constraint_violation () VALUES ()", &[])
.await;
// Error code 1364
{
let res = conn
.query_raw("INSERT INTO test_null_constraint_violation () VALUES ()", &[])
.await;

let err = res.unwrap_err();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1364"), err.original_code());
assert_eq!(
Some("Field \'id1\' doesn\'t have a default value"),
err.original_message()
);
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id1")]), constraint)
}
_ => panic!(err),
}
}

let err = res.unwrap_err();
// Error code 1048
{
conn.query_raw(
"INSERT INTO test_null_constraint_violation (id1, id2) VALUES (50, 55)",
&[],
)
.await
.unwrap();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1364"), err.original_code());
assert_eq!(
Some("Field \'id1\' doesn\'t have a default value"),
err.original_message()
);
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id1")]), constraint)
let err = conn
.query_raw("UPDATE test_null_constraint_violation SET id2 = NULL", &[])
.await
.unwrap_err();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1048"), err.original_code());
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id2")]), constraint);
}
_ => panic!("{:?}", err),
}
_ => panic!(err),
}
}
}
2 changes: 1 addition & 1 deletion src/connector/mysql/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl From<my::error::Error> for Error {

builder.build()
}
my::error::Error::Server(ServerError { ref message, code, .. }) if code == 1364 => {
my::error::Error::Server(ServerError { ref message, code, .. }) if code == 1364 || code == 1048 => {
let splitted: Vec<&str> = message.split_whitespace().collect();
let splitted: Vec<&str> = splitted.get(1).map(|s| s.split('\'').collect()).unwrap();

Expand Down