Skip to content

Commit

Permalink
🚑 Do not return errors to clients
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiBardon committed May 25, 2024
1 parent fb2c9c7 commit 089665a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
19 changes: 1 addition & 18 deletions src/helpers/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ use core::fmt;
use std::collections::HashSet;
use std::env;
use std::fs::{self, File};
use std::io::{self, Cursor, Write};
use std::io::{self, Write};
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};

use lazy_static::lazy_static;
use rocket::http::ContentType;
use rocket::request::Request;
use rocket::response::{self, Responder, Response};
use tracing::{debug, info, trace};

use crate::config::*;
Expand Down Expand Up @@ -429,17 +426,3 @@ impl fmt::Display for Error {
}
}
}

#[rocket::async_trait]
impl<'r> Responder<'r, 'static> for Error {
fn respond_to(
self,
_: &'r Request<'_>,
) -> response::Result<'static> {
let res = self.to_string();
Response::build()
.header(ContentType::Plain)
.sized_body(res.len(), Cursor::new(res))
.ok()
}
}
3 changes: 1 addition & 2 deletions src/helpers/src/readers/object_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ fn find(
}
}

#[derive(Debug, Responder)]
#[derive(Debug)]
pub enum Error {
#[response(status = 500)]
WebsiteGenerationError(generate::Error),
}

Expand Down
33 changes: 25 additions & 8 deletions src/orangutan-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use rocket::http::{Cookie, CookieJar, SameSite, Status};
use rocket::outcome::Outcome;
use rocket::request::FromRequest;
use rocket::response::status::BadRequest;
use rocket::response::Redirect;
use rocket::{catch, catchers, get, post, request, routes, Request, Responder, State};
use rocket::response::{self, Redirect, Responder};
use rocket::{catch, catchers, get, post, request, routes, Request, State};
use time::Duration;
use tracing::{debug, error, trace, Level};
use tracing_subscriber::FmtSubscriber;
Expand Down Expand Up @@ -184,7 +184,7 @@ async fn handle_request(
origin: &Origin<'_>,
token: Option<Token>,
object_reader: &State<ObjectReader>,
) -> Result<Option<ReadObjectResponse>, object_reader::Error> {
) -> Result<Option<ReadObjectResponse>, Error> {
let biscuit = token.map(|t| t.biscuit);

// FIXME: Handle error
Expand Down Expand Up @@ -214,9 +214,9 @@ async fn handle_request(
let stored_objects: Vec<String> =
object_reader
.list_objects(&path, &website_id)
.map_err(|err| {
error!("Error when listing objects matching '{}': {}", &path, err);
err
.map_err(|err| Error::CannotListObjects {
path: path.to_owned(),
err,
})?;
let Some(object_key) = matching_files(&path, &stored_objects)
.first()
Expand Down Expand Up @@ -545,16 +545,30 @@ fn add_padding(base64_string: &str) -> String {
}
}

#[derive(Debug, Responder)]
#[response(status = 500)]
#[derive(Debug)]
enum Error {
WebsiteGenerationError(generate::Error),
CannotPullOutdatedRepository(generate::Error),
CannotListObjects {
path: String,
err: object_reader::Error,
},
CannotTrashOutdatedWebsites(generate::Error),
CannotRecoverTrash(generate::Error),
CannotEmptyTrash(generate::Error),
}

#[rocket::async_trait]
impl<'r> Responder<'r, 'static> for Error {
fn respond_to(
self,
_: &'r Request<'_>,
) -> response::Result<'static> {
error!("{self}");
Err(Status::InternalServerError)
}
}

impl fmt::Display for Error {
fn fmt(
&self,
Expand All @@ -565,6 +579,9 @@ impl fmt::Display for Error {
Error::CannotPullOutdatedRepository(err) => {
write!(f, "Cannot pull outdated repository: {err}")
},
Error::CannotListObjects { path, err } => {
write!(f, "Error when listing objects matching '{path}': {err}")
},
Error::CannotTrashOutdatedWebsites(err) => {
write!(f, "Cannot trash outdated websites: {err}")
},
Expand Down

0 comments on commit 089665a

Please sign in to comment.