|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use http::{Response, StatusCode}; |
| 4 | + |
| 5 | +pub use problem_details::ProblemDetails; |
| 6 | +use serde::Serialize; |
| 7 | +use vercel_runtime::Body; |
| 8 | + |
| 9 | +#[derive(serde::Serialize)] |
| 10 | +pub struct ErrorsExt<T> { |
| 11 | + errors: Vec<T>, |
| 12 | +} |
| 13 | + |
| 14 | +impl<T> ErrorsExt<T> { |
| 15 | + pub fn new(errors: Vec<T>) -> Self { |
| 16 | + Self { errors } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(serde::Serialize)] |
| 21 | +pub struct MissingParameterDetails { |
| 22 | + detail: String, |
| 23 | + parameter: String, |
| 24 | +} |
| 25 | + |
| 26 | +impl MissingParameterDetails { |
| 27 | + pub fn new(parameter_name: impl Into<String>) -> Self { |
| 28 | + let parameter_string = parameter_name.into(); |
| 29 | + |
| 30 | + MissingParameterDetails { |
| 31 | + detail: format!("The query parameter {} is required", parameter_string), |
| 32 | + parameter: parameter_string, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +pub trait ErrorConverter { |
| 38 | + fn from_octocrab_err(err: octocrab::Error) -> Self; |
| 39 | + fn from_calc_err(err: crate::langs_calculator::LangCalcError) -> Self; |
| 40 | +} |
| 41 | + |
| 42 | +impl ErrorConverter for ProblemDetails { |
| 43 | + fn from_octocrab_err(err: octocrab::Error) -> Self { |
| 44 | + match err { |
| 45 | + octocrab::Error::GitHub { |
| 46 | + source, |
| 47 | + backtrace: _, |
| 48 | + } => problem_details::ProblemDetails::new() |
| 49 | + .with_title("GitHub API Error") |
| 50 | + .with_status(source.status_code) |
| 51 | + .with_detail(source.message), |
| 52 | + |
| 53 | + _ => create_internal_server_err_details(), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + fn from_calc_err(err: crate::langs_calculator::LangCalcError) -> Self { |
| 58 | + match err { |
| 59 | + crate::langs_calculator::LangCalcError::OctocrabError(err) => { |
| 60 | + Self::from_octocrab_err(err) |
| 61 | + } |
| 62 | + |
| 63 | + _ => create_internal_server_err_details(), |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub fn missing_query_param( |
| 69 | + param_name: impl Into<String>, |
| 70 | +) -> Result<Response<Body>, vercel_runtime::Error> { |
| 71 | + let errors = vec![MissingParameterDetails::new(param_name)]; |
| 72 | + |
| 73 | + let details = ProblemDetails::new() |
| 74 | + .with_type( |
| 75 | + http::Uri::from_str( |
| 76 | + "https://problems-registry.smartbear.com/missing-request-parameter", |
| 77 | + ) |
| 78 | + .unwrap(), |
| 79 | + ) |
| 80 | + .with_title("Missing request parameter") |
| 81 | + .with_detail("The request is missing an expected query parameter.") |
| 82 | + .with_status(StatusCode::BAD_REQUEST) |
| 83 | + .with_extensions(ErrorsExt::new(errors)); |
| 84 | + |
| 85 | + ext_problem_details(&details) |
| 86 | +} |
| 87 | + |
| 88 | +pub fn create_internal_server_err_details() -> ProblemDetails { |
| 89 | + problem_details::ProblemDetails::from_status_code(StatusCode::INTERNAL_SERVER_ERROR) |
| 90 | + .with_detail("An Internal Server Error has occurred") |
| 91 | +} |
| 92 | + |
| 93 | +pub fn problem_details(problem: &ProblemDetails) -> Result<Response<Body>, vercel_runtime::Error> { |
| 94 | + Ok(Response::builder() |
| 95 | + .status(&problem.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)) |
| 96 | + .header("Content-Type", "application/json") |
| 97 | + .body(serde_json::to_string(&problem).unwrap().into())?) |
| 98 | +} |
| 99 | + |
| 100 | +pub fn ext_problem_details<Ext>( |
| 101 | + problem: &ProblemDetails<Ext>, |
| 102 | +) -> Result<Response<Body>, vercel_runtime::Error> |
| 103 | +where |
| 104 | + Ext: Serialize, |
| 105 | +{ |
| 106 | + Ok(Response::builder() |
| 107 | + .status(&problem.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)) |
| 108 | + .header("Content-Type", "application/json") |
| 109 | + .body(serde_json::to_string(&problem).unwrap().into())?) |
| 110 | +} |
0 commit comments