1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-22 18:33:18 +01:00

Update to actix-web 4.0.0-beta.7. (#24)

Add two new error types implementing ResponseError, as required, for our extractors.

Add public docs for new errors.

Co-authored-by: LukeMathWalker <rust@lpalmieri.com>
This commit is contained in:
Luca Palmieri 2021-06-20 19:03:48 +01:00 committed by GitHub
parent 077bb85088
commit 0034e3bda8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 10 deletions

View File

@ -24,8 +24,8 @@ opentelemetry_0_13 = ["opentelemetry", "tracing-opentelemetry"]
emit_event_on_error = []
[dependencies]
actix-web = "=4.0.0-beta.6"
actix-http = "=3.0.0-beta.6"
actix-web = "=4.0.0-beta.7"
actix-http = "=3.0.0-beta.7"
actix-service = "^2.0.0"
tracing = "0.1.19"
tracing-futures = "0.2.4"

View File

@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4.0.0-beta.6"
actix-web = "4.0.0-beta.7"
opentelemetry = { version = "0.13", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.12", features = ["tokio"] }
tracing-opentelemetry = { version = "0.12" }

View File

@ -7,7 +7,7 @@ edition = "2018"
license = "MIT/Apache-2.0"
[dependencies]
actix-web = "=4.0.0-beta.6"
actix-web = "=4.0.0-beta.7"
tracing = "0.1.19"
opentelemetry = { version = "0.13", features = ["rt-tokio-current-thread"] }
opentelemetry-jaeger = { version = "0.12", features = ["tokio"] }

View File

@ -1,5 +1,5 @@
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use actix_web::{FromRequest, HttpRequest, ResponseError};
use std::future::{ready, Ready};
use uuid::Uuid;
@ -54,11 +54,47 @@ impl std::fmt::Display for RequestId {
}
}
impl FromRequest for RequestId {
type Error = ();
type Error = RequestIdExtractionError;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.extensions().get::<RequestId>().copied().ok_or(()))
ready(
req.extensions()
.get::<RequestId>()
.copied()
.ok_or(RequestIdExtractionError { _priv: () }),
)
}
}
#[derive(Debug)]
/// Error returned by the [`RequestId`] extractor when it fails to retrieve
/// the current request id from request-local storage.
///
/// It only happens if you try to extract the current request id without having
/// registered [`TracingLogger`] as a middleware for your application.
///
/// [`TracingLogger`]: crate::TracingLogger
pub struct RequestIdExtractionError {
// It turns out that a unit struct has a public constructor!
// Therefore adding fields to it (either public or private) later on
// is an API breaking change.
// Therefore we are adding a dummy private field that the compiler is going
// to optimise away to make sure users cannot construct this error
// manually in their own code.
_priv: (),
}
impl ResponseError for RequestIdExtractionError {}
impl std::fmt::Display for RequestIdExtractionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Failed to retrieve request id from request-local storage."
)
}
}
impl std::error::Error for RequestIdExtractionError {}

View File

@ -1,5 +1,5 @@
use actix_web::dev::Payload;
use actix_web::{FromRequest, HttpRequest};
use actix_web::{FromRequest, HttpRequest, ResponseError};
use std::future::{ready, Ready};
use tracing::Span;
@ -47,11 +47,47 @@ impl std::convert::Into<Span> for RootSpan {
}
impl FromRequest for RootSpan {
type Error = ();
type Error = RootSpanExtractionError;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(req.extensions().get::<RootSpan>().cloned().ok_or(()))
ready(
req.extensions()
.get::<RootSpan>()
.cloned()
.ok_or(RootSpanExtractionError { _priv: () }),
)
}
}
#[derive(Debug)]
/// Error returned by the [`RootSpan`] extractor when it fails to retrieve
/// the root span from request-local storage.
///
/// It only happens if you try to extract the root span without having
/// registered [`TracingLogger`] as a middleware for your application.
///
/// [`TracingLogger`]: crate::TracingLogger
pub struct RootSpanExtractionError {
// It turns out that a unit struct has a public constructor!
// Therefore adding fields to it (either public or private) later on
// is an API breaking change.
// Therefore we are adding a dummy private field that the compiler is going
// to optimise away to make sure users cannot construct this error
// manually in their own code.
_priv: (),
}
impl ResponseError for RootSpanExtractionError {}
impl std::fmt::Display for RootSpanExtractionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Failed to retrieve the root span from request-local storage."
)
}
}
impl std::error::Error for RootSpanExtractionError {}