Skip to content

Commit

Permalink
fix: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonHor committed Feb 22, 2025
1 parent a52d58b commit d6aaf3d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
5 changes: 2 additions & 3 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl DocumentMetadata {
.lock()
.map_err(|e| DocumentError::LockError(e.to_string()))?;

Ok(last_modified_guard.clone())
Ok(*last_modified_guard)
}

fn update_last_modified(&mut self) -> Result<(), DocumentError> {
Expand Down Expand Up @@ -122,9 +122,8 @@ impl DocumentTrait for Document {

history_guard.push(content.to_owned());
*content_guard = content.to_owned();
self.metadata.update_last_modified();

Ok(())
self.metadata.update_last_modified()
}

fn content(&self) -> Result<Arc<str>, DocumentError> {
Expand Down
17 changes: 9 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::document::{Document, DocumentTrait};
use actix_web::{get, post, web, App, HttpServer, Responder};
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::{Mutex};
use uuid::Uuid;

mod document;
Expand Down Expand Up @@ -34,11 +34,12 @@ impl DocumentDb {
#[get("/doc/{uuid}")]
async fn get_doc(server: web::Data<DocumentDb>, uuid: web::Path<String>) -> impl Responder {
match server.find_doc(&uuid).await {
Ok(doc) => format!(
"Document content: {}",
doc.content()
.expect("Error while obtaining document content.")
),
Ok(doc) => match doc.content() {
Ok(content) => {
format!("Document content: {}", content)
}
Err(e) => format!("Error: {}", e),
},
Err(e) => format!("Error: {}", e),
}
}
Expand All @@ -58,7 +59,7 @@ async fn create_doc(
let doc_id = doc.id();
match server.add_doc(doc).await {
Ok(_) => format!("Document created with ID: {}", doc_id),
Err(e) => e.to_owned()
Err(e) => format!("Error: {}", e),
}
}

Expand All @@ -76,7 +77,7 @@ async fn edit_doc(
match server.find_doc(&uuid).await {
Ok(mut doc) => match doc.set_content(body.content.as_ref()) {
Ok(_) => "Content changed".to_owned(),
Err(_) => "Couldn't change content, try again later".to_owned(),
Err(e) => format!("Error: {}", e),
},
Err(e) => format!("Error: {}", e),
}
Expand Down

0 comments on commit d6aaf3d

Please sign in to comment.