actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
https://actix.rs
Apache License 2.0
21.6k stars 1.67k forks source link

middleware::errhandlers::ErrorHandlers #936

Closed beykery closed 5 years ago

beykery commented 5 years ago

i use

middleware::errhandlers::ErrorHandlers::new().handler(http::StatusCode::INTERNAL_SERVER_ERROR, errhandle::render_500)

for processing the error 500 in my application as the example in errhandlers.rs

how can i modify the response body ? the example just modify a response header .

fafhrd91 commented 5 years ago

https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpResponse.html#method.map_body

https://docs.rs/actix-web/1.0.2/actix_web/struct.HttpResponse.html#method.take_body

example https://github.com/actix/actix-web/blob/master/src/test.rs#L314

beykery commented 5 years ago

thank , as the test.rs : res.take_body().take_body() .fold(BytesMut::new(), move |mut body, chunk| { body.extend_from_slice(&chunk); Ok::<_, Error>(body) }); but it result in : error[E0599]: no method named fold found for type actix_http::body::ResponseBody<B> in the current scope

fafhrd91 commented 5 years ago

use futures::Stream;

beykery commented 5 years ago

sorry it dont work as bellow :

` use actix_web::{get, middleware, web, App, HttpRequest, HttpResponse, HttpServer, dev, http, Result, Error}; use actix_web::middleware::errhandlers::ErrorHandlerResponse; use serde::{Serialize, Deserialize}; use actix_web::dev::ServiceResponse; use actix_http::http::StatusCode; use actix_http::Response; use actix_web::web::BytesMut; use futures::Stream;

pub fn render_500(mut res: dev::ServiceResponse) -> Result<ErrorHandlerResponse> { res.take_body() .fold(BytesMut::new(), move |mut body, chunk| { body.extend_fromslice(&chunk); Ok::<, Error>(body) }); Ok(ErrorHandlerResponse::Response(res)) } `

error[E0599]: no method named fold found for type actix_http::body::ResponseBody<B> in the current scope

fafhrd91 commented 5 years ago
beykery commented 5 years ago

i can't get it .

pedrogao commented 4 years ago

I can't get it too.