1
0
mirror of https://github.com/actix/actix-website synced 2024-12-18 18:03:12 +01:00
actix-website/examples/errors/src/override_error.rs
dependabot[bot] c2f760dbbe
chore(deps): bump derive_more from 0.99.18 to 1.0.0 in /examples (#432)
* chore(deps): bump derive_more from 0.99.18 to 1.0.0 in /examples

Bumps [derive_more](https://github.com/JelteF/derive_more) from 0.99.18 to 1.0.0.
- [Release notes](https://github.com/JelteF/derive_more/releases)
- [Changelog](https://github.com/JelteF/derive_more/blob/master/CHANGELOG.md)
- [Commits](https://github.com/JelteF/derive_more/compare/v0.99.18...v1.0.0)

---
updated-dependencies:
- dependency-name: derive_more
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: upgrade derive_more calls

* ci: fix just invocation

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
2024-08-19 01:13:03 +00:00

62 lines
1.4 KiB
Rust

// <override>
use actix_web::{
error, get,
http::{header::ContentType, StatusCode},
App, HttpResponse,
};
use derive_more::derive::{Display, Error};
#[derive(Debug, Display, Error)]
enum MyError {
#[display("internal error")]
InternalError,
#[display("bad request")]
BadClientData,
#[display("timeout")]
Timeout,
}
impl error::ResponseError for MyError {
fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code())
.insert_header(ContentType::html())
.body(self.to_string())
}
fn status_code(&self) -> StatusCode {
match *self {
MyError::InternalError => StatusCode::INTERNAL_SERVER_ERROR,
MyError::BadClientData => StatusCode::BAD_REQUEST,
MyError::Timeout => StatusCode::GATEWAY_TIMEOUT,
}
}
}
#[get("/")]
async fn index() -> Result<&'static str, MyError> {
Err(MyError::BadClientData)
}
// </override>
#[get("/e2")]
async fn error2() -> Result<&'static str, MyError> {
Err(MyError::InternalError)
}
#[get("/e3")]
async fn error3() -> Result<&'static str, MyError> {
Err(MyError::Timeout)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::HttpServer;
HttpServer::new(|| App::new().service(index).service(error2).service(error3))
.bind(("127.0.0.1", 8080))?
.run()
.await
}