mirror of
https://github.com/Xevion/time-banner.git
synced 2025-12-06 01:16:36 -06:00
Handle error response compiler issues properly, use JSON for error response format
This commit is contained in:
20
src/error.rs
20
src/error.rs
@@ -1,6 +1,8 @@
|
|||||||
use axum::body::Full;
|
use axum::body::Full;
|
||||||
use axum::http::{StatusCode};
|
use axum::http::{StatusCode};
|
||||||
|
use axum::Json;
|
||||||
use axum::response::{IntoResponse, Response};
|
use axum::response::{IntoResponse, Response};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub enum TimeBannerError {
|
pub enum TimeBannerError {
|
||||||
ParseError(String),
|
ParseError(String),
|
||||||
@@ -8,10 +10,18 @@ pub enum TimeBannerError {
|
|||||||
RasterizeError(String),
|
RasterizeError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Full<String>) {
|
#[derive(Serialize, Deserialize)]
|
||||||
match error {
|
pub struct ErrorResponse {
|
||||||
TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Template Could Not Be Rendered :: {}", msg))),
|
code: u16,
|
||||||
TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", msg))),
|
message: String,
|
||||||
TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Failed to rasterize :: {}", msg)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Json<ErrorResponse>) {
|
||||||
|
let (code, message) = match error {
|
||||||
|
TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RenderError :: {}", msg)),
|
||||||
|
TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, format!("ParserError :: {}", msg)),
|
||||||
|
TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, format!("RasertizeError :: {}", msg))
|
||||||
|
};
|
||||||
|
|
||||||
|
(code, Json(ErrorResponse { code: code.as_u16(), message }))
|
||||||
}
|
}
|
||||||
@@ -51,11 +51,15 @@ 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 (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", parsed_epoch.unwrap_err()))).into_response();
|
return get_error_response(TimeBannerError::ParseError("Input could not be parsed into integer.".to_string())).into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert epoch to DateTime
|
// Convert epoch to DateTime
|
||||||
let naive_time = NaiveDateTime::from_timestamp_opt(parsed_epoch.unwrap(), 0);
|
let naive_time = NaiveDateTime::from_timestamp_opt(parsed_epoch.unwrap(), 0);
|
||||||
|
if naive_time.is_none() {
|
||||||
|
return get_error_response(TimeBannerError::ParseError("Input was not a valid DateTime".to_string())).into_response();
|
||||||
|
}
|
||||||
|
|
||||||
let utc_time = DateTime::<Utc>::from_utc(naive_time.unwrap(), Utc);
|
let utc_time = DateTime::<Utc>::from_utc(naive_time.unwrap(), Utc);
|
||||||
|
|
||||||
// Build context for rendering
|
// Build context for rendering
|
||||||
@@ -75,7 +79,7 @@ pub async fn implicit_handler(Path(path): Path<String>) -> impl IntoResponse {
|
|||||||
.body(Full::from(
|
.body(Full::from(
|
||||||
format!("Template Could Not Be Rendered :: {}", rendered_template.err().unwrap())
|
format!("Template Could Not Be Rendered :: {}", rendered_template.err().unwrap())
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap().into_response();
|
||||||
}
|
}
|
||||||
|
|
||||||
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension);
|
||||||
|
|||||||
Reference in New Issue
Block a user