use crate::{errors::NetworkError, response::*}; use actix_web::{body::Body, error::ResponseError, HttpResponse}; use serde::Serialize; impl ResponseError for NetworkError { fn error_response(&self) -> HttpResponse { match self { NetworkError::InternalError(msg) => { let resp = FlowyResponse::from_msg(&msg, ServerCode::InternalError); HttpResponse::InternalServerError().json(resp) }, NetworkError::BadRequest(ref resp) => HttpResponse::BadRequest().json(resp), NetworkError::Unauthorized => { let resp = FlowyResponse::from_msg("Unauthorized", ServerCode::Unauthorized); HttpResponse::Unauthorized().json(resp) }, } } } impl std::convert::Into for FlowyResponse { fn into(self) -> HttpResponse { match serde_json::to_string(&self) { Ok(body) => HttpResponse::Ok().body(Body::from(body)), Err(e) => { let msg = format!("Serial error: {:?}", e); NetworkError::InternalError(msg).error_response() }, } } }