Skip to content

Commit

Permalink
improvement: add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sentinel1909 committed Apr 27, 2024
1 parent 61f6331 commit fcf50b3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rivet-head-cli"
version = "1.3.0"
version = "1.4.0"
edition = "2021"
description = "The CLI for interacting with the Rivet Head API"
readme = "README.md"
Expand Down
50 changes: 40 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@

// dependencies
use clap::Parser;
use reqwest::Error;
use std::collections::HashMap;
use std::io::{self, Write};

// command line arguments
// a enum type to represent possible application errors
#[derive(Debug)]
enum AppError {
ReqwestError(reqwest::Error),
IOError(std::io::Error),
}

// implement the From trait to convert a reqwest::Error type to an AppError type
impl From<reqwest::Error> for AppError {
fn from(err: reqwest::Error) -> Self {
AppError::ReqwestError(err)
}
}

// implement the From trait to convert a std::io::Error type to an AppError type
impl From<std::io::Error> for AppError {
fn from(err: std::io::Error) -> Self {
AppError::IOError(err)
}
}

// implement the Display trait for the AppError type
impl std::fmt::Display for AppError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AppError::ReqwestError(err) => write!(f, "Request Error: {}", err),
AppError::IOError(err) => write!(f, "IO Error: {}", err),
}
}
}

// struct type to represent the command line arguments
#[derive(Parser, Debug)]
#[command(name = "Rivet Head CLI", author = "Jeff Mitchell", version = "1.2", about = "CLI app to work with the Rivet Head API", long_about = None)]
struct Args {
Expand All @@ -20,8 +50,9 @@ struct Args {
thoughts: Option<String>,
}

// main function
#[tokio::main]
async fn main() -> Result<(), Error> {
async fn main() -> Result<(), AppError> {
// parse the command line arguments
let args = Args::parse();

Expand All @@ -40,7 +71,7 @@ async fn main() -> Result<(), Error> {
let mut stdout = io::stdout();
writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout");
let body = res.text().await?;
writeln!(stdout, "Body:\n\n{:?}", body).expect("Unable to write to stdout");
writeln!(stdout, "Body:\n\n{:?}", body)?;
}

verb if verb == "post" => {
Expand All @@ -56,9 +87,9 @@ async fn main() -> Result<(), Error> {

// print the status code and body
let mut stdout = io::stdout();
writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout");
writeln!(stdout, "Status: {}", res.status())?;
let body = res.text().await?;
writeln!(stdout, "Body: {:?}", body).expect("Unable to write to stdout");
writeln!(stdout, "Body: {:?}", body)?;
}

verb if verb == "put" => {
Expand All @@ -74,16 +105,15 @@ async fn main() -> Result<(), Error> {

// print the status code and body
let mut stdout = io::stdout();
writeln!(stdout, "Status: {}", res.status()).expect("Unable to write to stdout");
writeln!(stdout, "Status: {}", res.status())?;
let body = res.text().await?;
writeln!(stdout, "Body: {:?}", body).expect("Unable to write to stdout");
writeln!(stdout, "Body: {:?}", body)?;
}

// handle incorrectly spelled verbs
_ => {
let mut stdout = io::stdout();
writeln!(stdout, "Invalid verb, enter get, post, put or delete")
.expect("Unable to write to stdout");
writeln!(stdout, "Invalid verb, enter get, post, put or delete")?;
}
}

Expand Down

0 comments on commit fcf50b3

Please # to comment.