1
0
mirror of https://github.com/actix/examples synced 2025-06-26 17:17:42 +02:00

standardize binary names

This commit is contained in:
Rob Ede
2022-02-18 02:22:58 +00:00
parent cfb181ecf5
commit 6f1c9e2350
29 changed files with 10 additions and 10 deletions

View File

@ -0,0 +1,10 @@
[package]
name = "json_error"
version = "1.0.0"
edition = "2021"
[dependencies]
actix-web = "4.0.0-rc.1"
failure = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -0,0 +1,50 @@
// This example is meant to show how to automatically generate a json error response when something goes wrong.
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::io;
use actix_web::http::StatusCode;
use actix_web::{web, App, HttpServer, ResponseError};
use serde::Serialize;
use serde_json::{json, to_string_pretty};
#[derive(Debug, Serialize)]
struct Error {
msg: String,
status: u16,
}
impl Display for Error {
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 error_response(&self) -> web::HttpResponse {
let err_json = json!({ "error": self.msg });
web::HttpResponse::build(StatusCode::from_u16(self.status).unwrap())
.json(err_json)
}
}
async fn index() -> Result<web::HttpResponse, Error> {
Err(Error {
msg: "an example error message".to_string(),
status: 400,
})
}
#[actix_web::main]
async fn main() -> io::Result<()> {
let ip_address = "127.0.0.1:8000";
println!("Running server on {}", ip_address);
HttpServer::new(|| {
App::new().service(web::resource("/").route(web::get().to(index)))
})
.bind(ip_address)
.expect("Can not bind to port 8000")
.run()
.await
}