mirror of
https://github.com/actix/actix-website
synced 2025-01-22 16:15:56 +01:00
Adds logging example to errors chapter.
This commit is contained in:
parent
eb58a8692c
commit
6f95771c34
@ -128,6 +128,12 @@ By dividing errors into those which are user facing and those which are not, we
|
|||||||
can ensure that we don't accidentally expose users to errors thrown by
|
can ensure that we don't accidentally expose users to errors thrown by
|
||||||
application internals which they weren't meant to see.
|
application internals which they weren't meant to see.
|
||||||
|
|
||||||
|
# Error Logging
|
||||||
|
|
||||||
|
This is a basic example using `middleware::Logger`:
|
||||||
|
|
||||||
|
{{< include-example example="errors" file="logging.rs" section="logging" >}}
|
||||||
|
|
||||||
[actixerror]: https://docs.rs/actix-web/1.0.2/actix_web/error/struct.Error.html
|
[actixerror]: https://docs.rs/actix-web/1.0.2/actix_web/error/struct.Error.html
|
||||||
[errorhelpers]: https://docs.rs/actix-web/1.0.2/actix_web/trait.ResponseError.html
|
[errorhelpers]: https://docs.rs/actix-web/1.0.2/actix_web/trait.ResponseError.html
|
||||||
[failure]: https://github.com/rust-lang-nursery/failure
|
[failure]: https://github.com/rust-lang-nursery/failure
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "errors"
|
name = "my_errors"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
|
38
examples/errors/src/logging.rs
Normal file
38
examples/errors/src/logging.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// <logging>
|
||||||
|
use actix_web::{error, Result};
|
||||||
|
use failure::Fail;
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
|
#[derive(Fail, Debug)]
|
||||||
|
#[fail(display = "my error")]
|
||||||
|
pub struct MyError {
|
||||||
|
name: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use default implementation for `error_response()` method
|
||||||
|
impl error::ResponseError for MyError {}
|
||||||
|
|
||||||
|
pub fn index() -> Result<&'static str, MyError> {
|
||||||
|
let err = MyError { name: "test error" };
|
||||||
|
debug!("{}", err);
|
||||||
|
Err(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
use actix_web::{middleware::Logger, web, App, HttpServer};
|
||||||
|
|
||||||
|
std::env::set_var("RUST_LOG", "my_errors=debug,actix_web=info");
|
||||||
|
std::env::set_var("RUST_BACKTRACE", "1");
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.wrap(Logger::default())
|
||||||
|
.route("/", web::get().to(index))
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
// </logging>
|
@ -1,4 +1,5 @@
|
|||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
|
pub mod logging;
|
||||||
pub mod override_error;
|
pub mod override_error;
|
||||||
pub mod recommend_one;
|
pub mod recommend_one;
|
||||||
pub mod recommend_two;
|
pub mod recommend_two;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user