@@ -18,11 +18,12 @@ extern crate toml;
18
18
19
19
mod errors;
20
20
21
+ use error_chain:: ChainedError ;
21
22
use errors:: * ;
22
23
use futures:: { future, Future } ;
23
24
use intecture_api:: host:: local:: Local ;
24
25
use intecture_api:: host:: remote:: { JsonLineProto , LineMessage } ;
25
- use intecture_api:: remote:: { Executable , Request } ;
26
+ use intecture_api:: remote:: { Executable , Request , ResponseResult } ;
26
27
use std:: fs:: File ;
27
28
use std:: io:: { self , Read } ;
28
29
use std:: net:: SocketAddr ;
@@ -49,9 +50,9 @@ impl Service for Api {
49
50
Message :: WithoutBody ( req) => req,
50
51
} ;
51
52
52
- let request: Request = match serde_json:: from_value ( req) . chain_err ( || "Received invalid Request" ) {
53
+ let request: Request = match serde_json:: from_value ( req) . chain_err ( || "Could not deserialize Request" ) {
53
54
Ok ( r) => r,
54
- Err ( e) => return Box :: new ( future:: err ( e) )
55
+ Err ( e) => return Box :: new ( future:: ok ( error_to_msg ( e) ) ) ,
55
56
} ;
56
57
57
58
// XXX Danger zone! If we're running multiple threads, this `unwrap()`
@@ -62,14 +63,19 @@ impl Service for Api {
62
63
let handle = self . remote . handle ( ) . unwrap ( ) ;
63
64
Box :: new ( request. exec ( & self . host , & handle)
64
65
. chain_err ( || "Failed to execute Request" )
65
- . and_then ( |mut msg| {
66
- let body = msg. take_body ( ) ;
67
- match serde_json:: to_value ( msg. into_inner ( ) ) . chain_err ( || "Could not serialize result" ) {
68
- Ok ( v) => match body {
69
- Some ( b) => future:: ok ( Message :: WithBody ( v, b) ) ,
70
- None => future:: ok ( Message :: WithoutBody ( v) ) ,
66
+ . then ( |req| {
67
+ match req {
68
+ Ok ( mut msg) => {
69
+ let body = msg. take_body ( ) ;
70
+ match serde_json:: to_value ( msg. into_inner ( ) ) . chain_err ( || "Could not serialize Result" ) {
71
+ Ok ( v) => match body {
72
+ Some ( b) => future:: ok ( Message :: WithBody ( v, b) ) ,
73
+ None => future:: ok ( Message :: WithoutBody ( v) ) ,
74
+ } ,
75
+ Err ( e) => future:: ok ( error_to_msg ( e) ) ,
76
+ }
71
77
} ,
72
- Err ( e) => future:: err ( e ) ,
78
+ Err ( e) => future:: ok ( error_to_msg ( e ) ) ,
73
79
}
74
80
} ) )
75
81
}
@@ -143,3 +149,12 @@ quick_main!(|| -> Result<()> {
143
149
} ) ;
144
150
Ok ( ( ) )
145
151
} ) ;
152
+
153
+ fn error_to_msg ( e : Error ) -> LineMessage {
154
+ let response = ResponseResult :: Err ( format ! ( "{}" , e. display_chain( ) ) ) ;
155
+ // If we can't serialize this, we can't serialize anything, so
156
+ // panicking is appropriate.
157
+ let value = serde_json:: to_value ( response)
158
+ . expect ( "Cannot serialize ResponseResult::Err. This is bad..." ) ;
159
+ Message :: WithoutBody ( value)
160
+ }
0 commit comments