1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

update chat-broker example and fmt

This commit is contained in:
Nikolay Kim
2019-07-11 15:02:25 +06:00
parent 4aa663e794
commit 93275d7986
11 changed files with 51 additions and 61 deletions

View File

@ -13,41 +13,41 @@ use std::io;
#[derive(Debug, Serialize)]
struct Error {
msg: String,
status: u16,
msg: String,
status: u16,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", to_string_pretty(self).unwrap())
}
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", to_string_pretty(self).unwrap())
}
}
impl ResponseError for Error {
// builds the actual response to send back when an error occurs
fn render_response(&self) -> HttpResponse {
let err_json = json!({ "error": self.msg });
HttpResponse::build(StatusCode::from_u16(self.status).unwrap()).json(err_json)
}
// builds the actual response to send back when an error occurs
fn render_response(&self) -> HttpResponse {
let err_json = json!({ "error": self.msg });
HttpResponse::build(StatusCode::from_u16(self.status).unwrap()).json(err_json)
}
}
fn index(_: HttpRequest) -> impl Future<Item = HttpResponse, Error = Error> {
err(Error {
msg: "an example error message".to_string(),
status: 400,
})
err(Error {
msg: "an example error message".to_string(),
status: 400,
})
}
fn main() -> io::Result<()> {
let sys = System::new("json_error_example");
let ip_address = "127.0.0.1:8000";
let sys = System::new("json_error_example");
let ip_address = "127.0.0.1:8000";
HttpServer::new(|| App::new().service(resource("/").route(get().to_async(index))))
.bind(ip_address)
.expect("Can not bind to port 8000")
.start();
HttpServer::new(|| App::new().service(resource("/").route(get().to_async(index))))
.bind(ip_address)
.expect("Can not bind to port 8000")
.start();
println!("Running server on {}", ip_address);
println!("Running server on {}", ip_address);
sys.run()
sys.run()
}