@@ -9,7 +9,6 @@ use aes::cipher::{KeyIvInit, StreamCipher};
99use base64:: engine:: Engine as _;
1010use base64:: engine:: general_purpose:: STANDARD as BASE64 ;
1111use bytes:: Bytes ;
12- use futures_util:: { FutureExt , TryFutureExt } ;
1312use hmac:: { Hmac , Mac } ;
1413use http_body_util:: { BodyExt , Full } ;
1514use hyper:: { Method , Request , Response , StatusCode , body:: Incoming } ;
@@ -24,7 +23,7 @@ use super::{DiscoveryError, DiscoveryEvent};
2423
2524use crate :: {
2625 core:: config:: DeviceType ,
27- core:: { Error , authentication:: Credentials , diffie_hellman:: DhLocalKeys } ,
26+ core:: { Error , authentication:: Credentials , diffie_hellman:: DhLocalKeys , error :: ErrorKind } ,
2827} ;
2928
3029type Aes128Ctr = ctr:: Ctr128BE < aes:: Aes128 > ;
@@ -234,10 +233,28 @@ impl RequestHandler {
234233 res
235234 }
236235
236+ fn error_response ( & self , err : & Error ) -> Response < Full < Bytes > > {
237+ let status = match err. kind {
238+ ErrorKind :: InvalidArgument | ErrorKind :: FailedPrecondition => StatusCode :: BAD_REQUEST ,
239+ _ => StatusCode :: SERVICE_UNAVAILABLE ,
240+ } ;
241+
242+ let body = json ! ( {
243+ "status" : 102 ,
244+ "spotifyError" : 0 ,
245+ "statusString" : status. canonical_reason( ) . unwrap_or( "ERROR" ) ,
246+ } )
247+ . to_string ( ) ;
248+
249+ let mut res = Response :: new ( Full :: new ( Bytes :: from ( body) ) ) ;
250+ * res. status_mut ( ) = status;
251+ res
252+ }
253+
237254 async fn handle (
238255 self : Arc < Self > ,
239256 request : Request < Incoming > ,
240- ) -> Result < hyper:: Result < Response < Full < Bytes > > > , Error > {
257+ ) -> hyper:: Result < Response < Full < Bytes > > > {
241258 let mut params = Params :: new ( ) ;
242259
243260 let ( parts, body) = request. into_parts ( ) ;
@@ -257,11 +274,17 @@ impl RequestHandler {
257274
258275 let action = params. get ( "action" ) . map ( Cow :: as_ref) ;
259276
260- Ok ( Ok ( match ( parts. method , action) {
277+ Ok ( match ( parts. method , action) {
261278 ( Method :: GET , Some ( "getInfo" ) ) => self . handle_get_info ( ) ,
262- ( Method :: POST , Some ( "addUser" ) ) => self . handle_add_user ( & params) ?,
279+ ( Method :: POST , Some ( "addUser" ) ) => match self . handle_add_user ( & params) {
280+ Ok ( response) => response,
281+ Err ( err) => {
282+ error ! ( "could not handle discovery request: {err}" ) ;
283+ self . error_response ( & err)
284+ }
285+ } ,
263286 _ => self . not_found ( ) ,
264- } ) )
287+ } )
265288 }
266289}
267290
@@ -325,12 +348,7 @@ impl DiscoveryServer {
325348 let discovery = discovery. clone( ) ;
326349
327350 let svc = hyper:: service:: service_fn( move |request| {
328- discovery
329- . clone( )
330- . handle( request)
331- . inspect_err( |e| error!( "could not handle discovery request: {e}" ) )
332- . and_then( |x| async move { Ok ( x) } )
333- . map( Result :: unwrap) // guaranteed by `and_then` above
351+ discovery. clone( ) . handle( request)
334352 } ) ;
335353
336354 let conn = server. serve_connection( io, svc) ;
0 commit comments