generated from alecodes/base-template
feat: add thiserror for error handling
This commit is contained in:
parent
07861bcca9
commit
e65afb8553
4 changed files with 27 additions and 7 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use clap::{Parser, Subcommand};
|
||||
use futures::TryStreamExt;
|
||||
use simple_crud::error::Result;
|
||||
use simple_crud::sql::ColumnType;
|
||||
use sqlx::postgres::PgPool;
|
||||
use sqlx::Column;
|
||||
|
|
@ -21,7 +22,7 @@ struct Cli {
|
|||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Executes a query agains the database, it pass it directly to the underliying driver
|
||||
/// Executes a query against the database, it pass it directly to the underlying driver
|
||||
Query {
|
||||
/// Query to execute
|
||||
sql: String,
|
||||
|
|
@ -29,7 +30,7 @@ enum Commands {
|
|||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let url = cli.db_url.unwrap();
|
||||
|
|
@ -37,18 +38,20 @@ async fn main() {
|
|||
// You can check for the existence of subcommands, and if found use their
|
||||
// matches just as you would the top level cmd
|
||||
match &cli.command {
|
||||
Commands::Query { sql } => handle_query(url, sql).await.unwrap(),
|
||||
Commands::Query { sql } => handle_query(url, sql).await?,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_query(url: String, query: &String) -> Result<(), sqlx::Error> {
|
||||
async fn handle_query(url: String, query: &String) -> Result<()> {
|
||||
let pool = PgPool::connect(url.as_str()).await?;
|
||||
|
||||
let mut rows = sqlx::query(query.as_str()).fetch(&pool);
|
||||
|
||||
while let Some(row) = rows.try_next().await? {
|
||||
for idx in 0..row.len() {
|
||||
let column = ColumnType::new(&row, idx).unwrap();
|
||||
let column = ColumnType::new(&row, idx)?;
|
||||
// let value = String::from(row.get(col.ordinal()));
|
||||
println!("Column {:?}", column);
|
||||
}
|
||||
|
|
|
|||
17
src/error.rs
17
src/error.rs
|
|
@ -1 +1,16 @@
|
|||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("Unhandled error: {0}")]
|
||||
Generic(String),
|
||||
|
||||
#[error("Unhandled error: {0}")]
|
||||
Static(&'static str),
|
||||
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
|
||||
#[error("Error performing database operation.")]
|
||||
DatabaseOperation(#[from] sqlx::Error),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue