return json on 402 404 and 500 status

This commit is contained in:
creating2morrow 2023-05-08 04:02:38 -04:00
parent 01cbc0181d
commit a7135a53f7
3 changed files with 39 additions and 2 deletions

View file

@ -89,7 +89,7 @@ pub async fn start() {
find_tunnels().await;
{
tokio::spawn(async move {
let tick: std::sync::mpsc::Receiver<()> = schedule_recv::periodic_ms(60000);
let tick: std::sync::mpsc::Receiver<()> = schedule_recv::periodic_ms(600000);
loop {
tick.recv().unwrap();
check_connection().await;

View file

@ -879,3 +879,18 @@ impl Default for Jwp {
}
}
}
/// For handling 402, 404 and 500 error responses
#[derive(Serialize, Deserialize, Debug)]
#[serde(crate = "rocket::serde")]
pub struct ErrorResponse {
pub error: String,
}
impl Default for ErrorResponse {
fn default() -> Self {
ErrorResponse {
error: utils::empty_string(),
}
}
}

View file

@ -1,10 +1,31 @@
#[macro_use]
extern crate rocket;
use rocket::http::Status;
use rocket::serde::json::Json;
use rocket::response::status::Custom;
use nevmes::*;
use nevmes_core::*;
// The only changes in here should be mounting new controller methods
#[catch(402)]
fn payment_required() -> Custom<Json<reqres::ErrorResponse>> {
Custom(Status::PaymentRequired,
Json(reqres::ErrorResponse { error: String::from("Payment required") }))
}
#[catch(404)]
fn not_found() -> Custom<Json<reqres::ErrorResponse>> {
Custom(Status::NotFound,
Json(reqres::ErrorResponse { error: String::from("Resource does not exist") }))
}
#[catch(500)]
fn internal_error() -> Custom<Json<reqres::ErrorResponse>> {
Custom(Status::InternalServerError,
Json(reqres::ErrorResponse { error: String::from("Internal server error") }))
}
// The only changes below here should be mounting new controller methods
#[launch]
async fn rocket() -> _ {
let config = rocket::Config {
@ -16,6 +37,7 @@ async fn rocket() -> _ {
env_logger::init();
utils::start_up().await;
rocket::custom(&config)
.register("/", catchers![internal_error, not_found, payment_required])
.mount("/invoice", routes![controller::gen_invoice])
.mount("/message/rx", routes![controller::rx_message])
.mount("/prove", routes![controller::gen_jwp])