Skip to content

Commit

Permalink
structured errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcfrancisco committed Jul 23, 2024
1 parent e1d7042 commit 84f52c6
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ wkb = "0.7.1"
bytes = "1.6.0"
geojson = "0.24.1"
serde_json = "1.0.120"
derive_more = "0.99.18"
39 changes: 39 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use derive_more::From;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Debug, From)]
pub enum Error {
// -- utils
FailedInputValidation(String),
Mode(String),

// -- pg
TableExists(String),
CannotAppend(String),

// -- file_types
UnsupportedFileExtension(String),
UnsupportedShapeType(String),
ContainsMixedDataTypes(String),

// -- Externals
#[from]
Io(std::io::Error),
#[from]
Pg(postgres::Error),
#[from]
Shapefile(shapefile::Error),
}

// region: --- Error Boilerplate

impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!(fmt, "{self:?}")
}
}

impl std::error::Error for Error {}

// endregion: --- Error Boilerplate
4 changes: 2 additions & 2 deletions src/file_types/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{Result, Error};

use postgres::types::Type;
use std::path::Path;
Expand Down Expand Up @@ -67,7 +67,7 @@ pub fn determine_file_type(input_file: &str) -> Result<FileType> {
match file_extension_str {
"shp" => Ok(FileType::Shapefile),
"geojson" => Ok(FileType::GeoJson),
_ => Err("Unsupported file type ✘".into()),
_ => Err(Error::UnsupportedFileExtension("Unsupported file type ✘".into())),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/file_types/geo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{Result, Error};
use geo::Coord;
use shapefile::Shape;

Expand Down Expand Up @@ -43,7 +43,7 @@ pub fn to_geo(shape: &Shape) -> Result<geo::Geometry<f64>> {
Ok(geo::Geometry::from(poly))
}
}
_ => Err("Unsupported shape type ✘".into()),
_ => Err(Error::UnsupportedShapeType("Unsupported shape type ✘".into())),
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/file_types/geojson.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use crate::{Result, Error};

use crate::Result;
use std::collections::HashMap;
use geojson::GeoJson;
use postgres::types::Type;
use serde_json;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(key, Type::FLOAT8);
}
Expand All @@ -54,7 +54,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(key, Type::TEXT);
}
Expand All @@ -67,7 +67,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&key)
&& table_config[&key] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(key, Type::BOOL);
}
Expand Down
16 changes: 8 additions & 8 deletions src/file_types/shapefile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;
use crate::{Result, Error};

use crate::Result;
use std::collections::HashMap;
use postgres::types::Type;
use shapefile::dbase::FieldValue;

Expand All @@ -24,7 +24,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::FLOAT8);
}
Expand All @@ -37,7 +37,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::FLOAT8);
}
Expand All @@ -50,7 +50,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::FLOAT8);
}
Expand All @@ -63,7 +63,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::FLOAT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::INT8);
}
Expand All @@ -76,7 +76,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::TEXT);
}
Expand All @@ -89,7 +89,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
} else if table_config.contains_key(&column_name)
&& table_config[&column_name] != Type::INT8
{
return Err("Column contains mixed data types ✘".into());
return Err(Error::ContainsMixedDataTypes("Column contains mixed data types ✘".to_string()));
} else {
table_config.insert(column_name, Type::BOOL);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub type Result<T> = core::result::Result<T, Error>;
pub type Error = Box<dyn std::error::Error>;
mod error;

pub use self::error::{Error, Result};

mod utils;
mod pg;
Expand Down
6 changes: 3 additions & 3 deletions src/pg/crud.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{Result, Error};
use postgres::types::Type;
use postgres::Statement;

Expand Down Expand Up @@ -99,7 +99,7 @@ pub fn can_append(table_name: &str, schema_name: &Option<String>, uri: &str) ->
if exists {
return Ok(());
} else {
return Err("Cannot append to a table that does NOT exist ✘".into());
return Err(Error::CannotAppend("Cannot append to a table that does NOT exist ✘".into()));
}
}

Expand All @@ -123,7 +123,7 @@ pub fn check_table_exists(
let exists: bool = client.query_one(&query, &[])?.get(0);
// If exists, throw error
if exists {
return Err("Table already exists ✘".into());
return Err(Error::TableExists("Table already exists ✘".into()));
} else {
return Ok(());
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{Result, Error};
use crate::file_types::common::{FileType, determine_file_type};
use crate::utils::validate::validate_args;
use crate::file_types::shapefile;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn run() -> Result<()> {
}
_ => {
println!("Mode not supported ✘");
return Err("Mode not supported ✘".into());
return Err(Error::FailedInputValidation("Mode not supported ✘".into()));
}
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/utils/validate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Result;
use crate::{Result, Error};
use super::cli::Cli;
use std::path::Path;

Expand All @@ -7,23 +7,23 @@ pub fn validate_args(args: &Cli) -> Result<()> {

// Check input file exists
if !Path::new(&args.input).exists() {
return Err("Input file does not exist ✘".into());
return Err(Error::FailedInputValidation("Input file does not exist ✘".into()));
}

// Check URL is not empty
if args.uri.is_empty() {
return Err("URL is empty ✘".into());
return Err(Error::FailedInputValidation("URL is empty ✘".into()));
}

// Check table is not empty
if args.table.is_empty() {
return Err("Table is empty ✘".into());
return Err(Error::FailedInputValidation("Table is empty ✘".into()));
}

// Check if srid is 4326 or 3857
if let Some(srid) = args.srid {
if srid != 4326 && srid != 3857 {
return Err("SRID must be 4326 or 3857 ✘".into());
return Err(Error::FailedInputValidation("SRID must be 4326 or 3857 ✘".into()));
}
}

Expand Down

0 comments on commit 84f52c6

Please sign in to comment.