mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-09 06:08:49 -06:00
Refactoring error response handling
This commit is contained in:
17
src/error.rs
Normal file
17
src/error.rs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
use axum::body::Full;
|
||||||
|
use axum::http::{StatusCode};
|
||||||
|
use axum::response::{IntoResponse, Response};
|
||||||
|
|
||||||
|
pub enum TimeBannerError {
|
||||||
|
ParseError(String),
|
||||||
|
RenderError(String),
|
||||||
|
RasterizeError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Full<String>) {
|
||||||
|
match error {
|
||||||
|
TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Template Could Not Be Rendered :: {}", msg))),
|
||||||
|
TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", msg))),
|
||||||
|
TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Failed to rasterize :: {}", msg)))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ mod abbr;
|
|||||||
mod routes;
|
mod routes;
|
||||||
mod parse;
|
mod parse;
|
||||||
mod template;
|
mod template;
|
||||||
|
mod error;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
use std::io::Read;
|
|
||||||
use std::process::Output;
|
|
||||||
use std::time::SystemTime;
|
|
||||||
|
|
||||||
use axum::{http::StatusCode, response::IntoResponse};
|
use axum::{http::StatusCode, response::IntoResponse};
|
||||||
use axum::body::{Bytes, Full};
|
use axum::body::{Bytes, Full};
|
||||||
use axum::extract::{Path};
|
use axum::extract::{Path};
|
||||||
use axum::http::{header};
|
use axum::http::{header};
|
||||||
use axum::response::Response;
|
use axum::response::Response;
|
||||||
use chrono::{DateTime, FixedOffset, NaiveDateTime, Offset, Utc};
|
use chrono::{DateTime, NaiveDateTime, Offset, Utc};
|
||||||
|
use crate::error::{get_error_response, TimeBannerError};
|
||||||
|
|
||||||
|
|
||||||
use crate::parse::split_on_extension;
|
use crate::parse::split_on_extension;
|
||||||
@@ -21,12 +18,6 @@ fn parse_path(path: &str) -> (&str, &str) {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
enum TimeBannerError {
|
|
||||||
ParseError(String),
|
|
||||||
RenderError(String),
|
|
||||||
RasterizeError(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), TimeBannerError> {
|
fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), TimeBannerError> {
|
||||||
match extension {
|
match extension {
|
||||||
"svg" => Ok(("image/svg+xml", Bytes::from(data))),
|
"svg" => Ok(("image/svg+xml", Bytes::from(data))),
|
||||||
@@ -60,10 +51,7 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
// Parse epoch
|
// Parse epoch
|
||||||
let parsed_epoch = raw_time.parse::<i64>();
|
let parsed_epoch = raw_time.parse::<i64>();
|
||||||
if parsed_epoch.is_err() {
|
if parsed_epoch.is_err() {
|
||||||
return Response::builder()
|
return (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", parsed_epoch.unwrap_err()))).into_response();
|
||||||
.status(StatusCode::BAD_REQUEST)
|
|
||||||
.body(Full::from(format!("Failed to parse epoch :: {}", parsed_epoch.unwrap_err())))
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert epoch to DateTime
|
// Convert epoch to DateTime
|
||||||
@@ -93,27 +81,8 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
||||||
match rasterize_result {
|
match rasterize_result {
|
||||||
Ok((mime_type, bytes)) => {
|
Ok((mime_type, bytes)) => {
|
||||||
Response::builder()
|
(StatusCode::OK, [(header::CONTENT_TYPE, mime_type)], bytes).into_response()
|
||||||
.status(StatusCode::OK)
|
|
||||||
.header(header::CONTENT_TYPE, mime_type)
|
|
||||||
.body(Full::from(bytes))
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
match e {
|
|
||||||
TimeBannerError::RenderError(msg) => Response::builder()
|
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
||||||
.body(Full::from(format!("Template Could Not Be Rendered :: {}", msg)))
|
|
||||||
.unwrap(),
|
|
||||||
TimeBannerError::ParseError(msg) => Response::builder()
|
|
||||||
.status(StatusCode::BAD_REQUEST)
|
|
||||||
.body(Full::from(format!("Failed to parse epoch :: {}", msg)))
|
|
||||||
.unwrap(),
|
|
||||||
TimeBannerError::RasterizeError(msg) => Response::builder()
|
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
||||||
.body(Full::from(format!("Failed to rasterize :: {}", msg)))
|
|
||||||
.unwrap(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Err(e) => get_error_response(e).into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user