1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 09:59:21 +02:00

refactor error handling

This commit is contained in:
Nikolay Kim
2017-10-22 09:13:29 -07:00
parent d555fcabfc
commit f85925a652
11 changed files with 73 additions and 86 deletions

View File

@ -15,13 +15,11 @@ impl Actor for MyRoute {
impl Route for MyRoute {
type State = ();
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self> {
fn request(req: &mut HttpRequest, payload: Payload,
ctx: &mut HttpContext<Self>) -> RouteResult<Self> {
println!("{:?}", req);
let multipart = match req.multipart(payload) {
Ok(mp) => mp,
Err(e) => return e.into(),
};
let multipart = req.multipart(payload)?;
// get Multipart stream
WrapStream::<MyRoute>::actstream(multipart)

View File

@ -48,24 +48,19 @@ impl Actor for WsChatSession {
impl Route for WsChatSession {
type State = WsChatSessionState;
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
fn request(req: &mut HttpRequest,
payload: Payload, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
{
// websocket handshakre, it may fail if request is not websocket request
match ws::handshake(&req) {
Ok(resp) => {
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
Reply::async(
WsChatSession {
id: 0,
hb: Instant::now(),
room: "Main".to_owned(),
name: None})
}
Err(err) => {
Reply::reply(err)
}
}
let resp = ws::handshake(&req)?;
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
Reply::async(
WsChatSession {
id: 0,
hb: Instant::now(),
room: "Main".to_owned(),
name: None})
}
}

View File

@ -17,18 +17,12 @@ impl Route for MyWebSocket {
type State = ();
fn request(req: &mut HttpRequest,
payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
payload: Payload, ctx: &mut HttpContext<Self>) -> RouteResult<Self>
{
match ws::handshake(&req) {
Ok(resp) => {
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
Reply::async(MyWebSocket)
}
Err(err) => {
Reply::reply(err)
}
}
let resp = ws::handshake(&req)?;
ctx.start(resp);
ctx.add_stream(ws::WsStream::new(payload));
Reply::async(MyWebSocket)
}
}