Skip to content

Commit

Permalink
Fixing handling of errors, now returning the proper error
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas794 committed Oct 9, 2024
1 parent 87c3d0e commit 66dc83a
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 202 deletions.
30 changes: 2 additions & 28 deletions src/consults/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,7 @@ impl Delete {
},
}
}
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}

Expand All @@ -82,20 +69,7 @@ impl Delete {

match resolve {
Ok(b) => Ok(b),
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}
}
Expand Down
30 changes: 2 additions & 28 deletions src/consults/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,7 @@ impl Insert {
}
Ok(())
}
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}

Expand Down Expand Up @@ -100,20 +87,7 @@ impl Insert {

match resolve {
Ok(line) => Ok(line),
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}
}
Expand Down
30 changes: 2 additions & 28 deletions src/consults/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,7 @@ impl Select {
}
Ok(())
}
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}

Expand All @@ -91,20 +78,7 @@ impl Select {

match csv_data {
Ok(data) => Ok(data),
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}
}
Expand Down
30 changes: 2 additions & 28 deletions src/consults/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,7 @@ impl Update {

Ok(())
}
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}

Expand All @@ -104,20 +91,7 @@ impl Update {

match resolve {
Ok(buf) => Ok(buf),
Err(e) => {
let formatted_error = format!("{}", e);
let dots = formatted_error.find(":").unwrap_or_default();
if formatted_error.contains("SYNTAX") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Syntax(formatted_error))
} else if formatted_error.contains("COLUMN") {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Table(formatted_error))
} else {
let formatted_error = formatted_error[dots..].trim().to_string();
Err(Tperrors::Generic(formatted_error))
}
}
Err(e) => Err(e),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/errors/tperrors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub enum Tperrors {
Table(String),
Syntax(String),
Generic(String),
Column(String),
}

impl Display for Tperrors {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match self {
Tperrors::Generic(e) => write!(f, "ERROR: {}", e),
Tperrors::Column(e) => write!(f, "INVALID_COLUMN: {}", e),
Tperrors::Table(e) => write!(f, "INVALID_TABLE: {}", e),
Tperrors::Syntax(e) => write!(f, "SYNTAX_ERROR: {}", e),
}
Expand Down
Loading

0 comments on commit 66dc83a

Please sign in to comment.