mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-17 00:00:16 +02:00
Support returning any http_body::Body from IntoResponse (#86)
Adds associated `Body` and `BodyError` types to `IntoResponse`. This is required for returning responses with bodies other than `hyper::Body` from handlers. That wasn't previously possible. This is a breaking change so should be shipped in 0.2.
This commit is contained in:
@@ -16,10 +16,12 @@ use axum::{
|
||||
response::IntoResponse,
|
||||
AddExtensionLayer,
|
||||
};
|
||||
use http::StatusCode;
|
||||
use bytes::Bytes;
|
||||
use http::{Response, StatusCode};
|
||||
use http_body::Full;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use std::{convert::Infallible, net::SocketAddr, sync::Arc};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tokio::main]
|
||||
@@ -89,7 +91,10 @@ impl From<UserRepoError> for AppError {
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> http::Response<Body> {
|
||||
type Body = Full<Bytes>;
|
||||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
let (status, error_json) = match self {
|
||||
AppError::UserRepo(UserRepoError::NotFound) => {
|
||||
(StatusCode::NOT_FOUND, json!("User not found"))
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
|
||||
use askama::Template;
|
||||
use axum::{prelude::*, response::IntoResponse};
|
||||
use bytes::Bytes;
|
||||
use http::{Response, StatusCode};
|
||||
use std::net::SocketAddr;
|
||||
use http_body::Full;
|
||||
use std::{convert::Infallible, net::SocketAddr};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -46,12 +48,15 @@ impl<T> IntoResponse for HtmlTemplate<T>
|
||||
where
|
||||
T: Template,
|
||||
{
|
||||
fn into_response(self) -> http::Response<Body> {
|
||||
type Body = Full<Bytes>;
|
||||
type BodyError = Infallible;
|
||||
|
||||
fn into_response(self) -> Response<Self::Body> {
|
||||
match self.0.render() {
|
||||
Ok(html) => response::Html(html).into_response(),
|
||||
Err(err) => Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::from(format!(
|
||||
.body(Full::from(format!(
|
||||
"Failed to render template. Error: {}",
|
||||
err
|
||||
)))
|
||||
|
||||
@@ -10,8 +10,10 @@ use axum::{
|
||||
extract::{FromRequest, RequestParts},
|
||||
prelude::*,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use http::Response;
|
||||
use http::StatusCode;
|
||||
use http_body::Full;
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
@@ -51,7 +53,7 @@ impl<B> FromRequest<B> for Version
|
||||
where
|
||||
B: Send,
|
||||
{
|
||||
type Rejection = Response<Body>;
|
||||
type Rejection = Response<Full<Bytes>>;
|
||||
|
||||
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
let params = extract::Path::<HashMap<String, String>>::from_request(req)
|
||||
|
||||
Reference in New Issue
Block a user