1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 01:39:33 +00:00

update migration guide

This commit is contained in:
Rob Ede 2022-03-26 13:26:08 +00:00
parent 09cffc093c
commit e942d3e3b1
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
3 changed files with 27 additions and 5 deletions

View file

@ -31,6 +31,7 @@ Headings marked with :warning: are **breaking behavioral changes**. They will pr
- [Returning `HttpResponse` synchronously](#returning-httpresponse-synchronously)
- [`#[actix_web::main]` and `#[tokio::main]`](#actix_webmain-and-tokiomain)
- [`web::block`](#webblock)
-
## MSRV
@ -483,3 +484,24 @@ The `web::block` helper has changed return type from roughly `async fn(fn() -> R
- let n: u32 = web::block(|| Ok(123)).await?;
+ let n: u32 = web::block(|| Ok(123)).await??;
```
## `HttpResponse` as a `ResponseError`
The implementation of `ResponseError` for `HttpResponse` has been removed.
It was common in v3 to use `HttpResponse` as an error type in fallible handlers. The problem is that `HttpResponse` contains no knowledge or reference to the source error. Being able to guarantee that an "error" response actually contains an error reference makes middleware and other parts of Actix Web more effective.
The error response builders in the `error` module were available in v3 but are now the best method for simple error responses without requiring you to implement the trait on your own custom error types. These builders can receive simple strings and third party errors that can not implement the `ResponseError` trait.
A few common patterns are affected by this change:
```diff
- Err(HttpResponse::InternalServerError().finish())
+ Err(error::ErrorInternalServerError("reason"))
- Err(HttpResponse::InternalServerError().body(third_party_error.to_string()))
+ Err(error::ErrorInternalServerError(err))
- .map_err(|err| HttpResponse::InternalServerError().finish())?
+ .map_err(error::ErrorInternalServerError)?
```

View file

@ -5,10 +5,7 @@ use actix_utils::future::{err, ok, Ready};
use futures_core::future::LocalBoxFuture;
use serde::Serialize;
use crate::{
dev::Payload, error::ErrorInternalServerError, extract::FromRequest, request::HttpRequest,
Error,
};
use crate::{dev::Payload, error, Error, FromRequest, HttpRequest};
/// Data factory.
pub(crate) trait DataFactory {
@ -160,7 +157,7 @@ impl<T: ?Sized + 'static> FromRequest for Data<T> {
req.match_name().unwrap_or_else(|| req.path())
);
err(ErrorInternalServerError(
err(error::ErrorInternalServerError(
"Requested application data is not configured correctly. \
View/enable debug logs for more details.",
))

View file

@ -11,3 +11,6 @@
## Error Propagation
## When To (Not) Use Middleware
## Author's References
- `EitherBody` + when is middleware appropriate: https://discord.com/channels/771444961383153695/952016890723729428