From e06f5c8f52487cd5d1ac20874a62439daf25fd95 Mon Sep 17 00:00:00 2001
From: David Huss <dh@atoav.com>
Date: Fri, 28 Oct 2022 17:28:56 +0200
Subject: [PATCH] Fix error with eventhandler

---
 Cargo.lock              |  2 +-
 Cargo.toml              |  2 +-
 src/bin/eventhandler.rs | 12 +++++++++---
 src/errors.rs           |  4 ++++
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 4c92e46..9759e44 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1376,7 +1376,7 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0"
 
 [[package]]
 name = "sms-gateway"
-version = "0.3.2"
+version = "0.4.0"
 dependencies = [
  "actix-rt",
  "actix-web",
diff --git a/Cargo.toml b/Cargo.toml
index b75359a..d57d381 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -2,7 +2,7 @@
 name = "sms-gateway"
 description = "A http frontend for SMS Server Tools 3"
 authors = ["David Huss <david.huss@hfbk-hamburg.de>"]
-version = "0.4.0"
+version = "0.4.1"
 edition = "2021"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
diff --git a/src/bin/eventhandler.rs b/src/bin/eventhandler.rs
index eeac18f..05267f7 100644
--- a/src/bin/eventhandler.rs
+++ b/src/bin/eventhandler.rs
@@ -31,10 +31,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 
     match args.len() {
         2 | 3 => {
-            let kind = args[0].to_ascii_lowercase();
-            let path = PathBuf::from(&args[1]);
+            let kind = args[1].to_ascii_lowercase();
+            let path = PathBuf::from(&args[2]);
 
-            let content = std::fs::read_to_string(&path)?;
+            let content = match std::fs::read_to_string(&path) {
+                Ok(s) => s,
+                Err(e) => {
+                    error!("Could not read {} SMS from path \"{}\": {}", kind, path.to_string_lossy(), e);
+                    exit(1);
+                }
+            };
 
             match &kind[..] {
                 "received" => {
diff --git a/src/errors.rs b/src/errors.rs
index f7feb48..57845a3 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -15,6 +15,8 @@ pub enum SmsError {
   InvalidCharacters,
   #[error("Could not write the SMS to the outgoing directory: '{0}'")]
   WriteError(String),
+  #[error("Could not read SMS at path \"{0}\": {1}")]
+  ReadError(String, String),
   #[error("Received Event of unknown kind: '{0}'")]
   UnknownEventKind(String),
   #[error("Failed to deserialize SMS from string: '{0}'")]
@@ -33,6 +35,7 @@ impl SmsError {
             SmsError::InvaldNumber(_s) => "Bad Request".to_string(),
             SmsError::InvalidCharacters => "Bad Request".to_string(),
             SmsError::WriteError(_s) => "Internal Server Error".to_string(),
+            SmsError::ReadError(_s, _x) => "Internal Server Error".to_string(),
             SmsError::UnknownEventKind(_s) => "Internal Server Error".to_string(),
             SmsError::DeserializationError(_s) => "Internal Server Error".to_string(),
             SmsError::NotInWhiteList(_) => "Forbidden".to_string(),
@@ -60,6 +63,7 @@ impl ResponseError for SmsError {
             SmsError::InvaldNumber(_) => StatusCode::BAD_REQUEST,
             SmsError::InvalidCharacters => StatusCode::BAD_REQUEST,
             SmsError::WriteError(_) => StatusCode::INTERNAL_SERVER_ERROR,
+            SmsError::ReadError(..) => StatusCode::INTERNAL_SERVER_ERROR,
             SmsError::UnknownEventKind(_) => StatusCode::INTERNAL_SERVER_ERROR,
             SmsError::DeserializationError(_) => StatusCode::INTERNAL_SERVER_ERROR,
             SmsError::NotInWhiteList(_) => StatusCode::FORBIDDEN,
-- 
GitLab