Two teams try and fill in any horizontal, vertical, or diagonal line on a bingo board by playing maps on osu! osu.bingo
osu
at microservice 69 lines 1.6 kB view raw
1use axum::{ 2 http::StatusCode, 3 response::{IntoResponse, Response}, 4}; 5use serde::Serialize; 6 7#[derive(Serialize)] 8pub struct ApiError { 9 code: u16, 10 message: String, 11} 12 13impl ApiError { 14 pub fn new(code: u16, msg: &str) -> Self { 15 ApiError { 16 code, 17 message: String::from(msg), 18 } 19 } 20} 21 22impl IntoResponse for ApiError { 23 fn into_response(self) -> Response { 24 let body = ( 25 StatusCode::from_u16(self.code).unwrap(), 26 serde_json::to_string(&self).unwrap(), 27 ) 28 .into_response() 29 .into_body(); 30 31 Response::builder() 32 .header("Content-Type", "application/json") 33 .status(self.code) 34 .body(body) 35 .unwrap() 36 } 37} 38 39impl From<sqlx::Error> for ApiError { 40 fn from(value: sqlx::Error) -> Self { 41 match value { 42 sqlx::Error::RowNotFound => ApiError::new(404, "Requested item yielded no results"), 43 x => ApiError::new(500, x.to_string().as_str()), 44 } 45 } 46} 47 48impl From<&str> for ApiError { 49 fn from(value: &str) -> Self { 50 ApiError::new(500, value) 51 } 52} 53 54impl From<(u16, &str)> for ApiError { 55 fn from(value: (u16, &str)) -> Self { 56 ApiError::new(value.0, value.1) 57 } 58} 59 60impl From<u16> for ApiError { 61 fn from(value: u16) -> Self { 62 let phrase = StatusCode::from_u16(value) 63 .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR) 64 .canonical_reason() 65 .unwrap_or("Unknown Reason"); 66 67 (value, phrase).into() 68 } 69}