@@ -112,7 +112,8 @@ impl DapHandler {
112112 pub fn dispatch ( & self , req : Request ) -> DapOutput {
113113 let cmd = req. command . clone ( ) ;
114114
115- match cmd {
115+ let err_req = req. clone ( ) ;
116+ let result = match cmd {
116117 Command :: Initialize ( args) => self . handle_initialize ( req, args) ,
117118 Command :: Attach ( args) => self . handle_attach ( req, args) ,
118119 Command :: Disconnect ( args) => self . handle_disconnect ( req, args) ,
@@ -144,12 +145,21 @@ impl DapHandler {
144145 Command :: Pause ( args) => self . handle_pause ( req, args) ,
145146 _ => {
146147 log:: warn!( "DAP: Unknown request" ) ;
147- DapOutput :: response ( req . error ( "Ark DAP: Unknown request" ) )
148+ return DapOutput :: response ( err_req . error ( "Ark DAP: Unknown request" ) ) ;
148149 } ,
150+ } ;
151+
152+ match result {
153+ Ok ( output) => output,
154+ Err ( err) => DapOutput :: response ( err_req. error ( & format ! ( "{err}" ) ) ) ,
149155 }
150156 }
151157
152- fn handle_initialize ( & self , req : Request , _args : InitializeArguments ) -> DapOutput {
158+ fn handle_initialize (
159+ & self ,
160+ req : Request ,
161+ _args : InitializeArguments ,
162+ ) -> anyhow:: Result < DapOutput > {
153163 let rsp = req. success ( ResponseBody :: Initialize ( types:: Capabilities {
154164 supports_restart_request : Some ( true ) ,
155165 supports_exception_info_request : Some ( false ) ,
@@ -185,11 +195,11 @@ impl DapHandler {
185195 supports_log_points : Some ( true ) ,
186196 ..Default :: default ( )
187197 } ) ) ;
188- DapOutput {
198+ Ok ( DapOutput {
189199 response : rsp,
190200 dap_events : vec ! [ Event :: Initialized ] ,
191201 console_events : vec ! [ ] ,
192- }
202+ } )
193203 }
194204
195205 // Handle SetBreakpoints requests from the frontend.
@@ -207,11 +217,13 @@ impl DapHandler {
207217 // - When a user unchecks a breakpoint, it appears as a deletion (omitted
208218 // from the request). We preserve verified breakpoints as Disabled so we
209219 // can restore their state when re-enabled without requiring re-sourcing.
210- fn handle_set_breakpoints ( & self , req : Request , args : SetBreakpointsArguments ) -> DapOutput {
220+ fn handle_set_breakpoints (
221+ & self ,
222+ req : Request ,
223+ args : SetBreakpointsArguments ,
224+ ) -> anyhow:: Result < DapOutput > {
211225 let Some ( path) = args. source . path . as_ref ( ) else {
212- // We don't currently have virtual documents managed via source references
213- log:: warn!( "Missing a path to set breakpoints for." ) ;
214- return DapOutput :: response ( req. error ( "Missing a path to set breakpoints for" ) ) ;
226+ return Err ( anyhow:: anyhow!( "Missing a path to set breakpoints for" ) ) ;
215227 } ;
216228
217229 // We currently only support "path" URIs as Positron never sends URIs.
@@ -224,7 +236,7 @@ impl DapHandler {
224236 let rsp = req. success ( ResponseBody :: SetBreakpoints ( SetBreakpointsResponse {
225237 breakpoints : vec ! [ ] ,
226238 } ) ) ;
227- return DapOutput :: response ( rsp) ;
239+ return Ok ( DapOutput :: response ( rsp) ) ;
228240 } ,
229241 } ;
230242
@@ -253,7 +265,7 @@ impl DapHandler {
253265 let rsp = req. success ( ResponseBody :: SetBreakpoints ( SetBreakpointsResponse {
254266 breakpoints,
255267 } ) ) ;
256- return DapOutput :: response ( rsp) ;
268+ return Ok ( DapOutput :: response ( rsp) ) ;
257269 } ,
258270 } ;
259271
@@ -407,22 +419,30 @@ impl DapHandler {
407419 breakpoints : response_breakpoints,
408420 } ) ) ;
409421
410- DapOutput :: response ( rsp)
422+ Ok ( DapOutput :: response ( rsp) )
411423 }
412424
413- fn handle_attach ( & self , req : Request , _args : AttachRequestArguments ) -> DapOutput {
425+ fn handle_attach (
426+ & self ,
427+ req : Request ,
428+ _args : AttachRequestArguments ,
429+ ) -> anyhow:: Result < DapOutput > {
414430 let rsp = req. success ( ResponseBody :: Attach ) ;
415- DapOutput {
431+ Ok ( DapOutput {
416432 response : rsp,
417433 dap_events : vec ! [ Event :: Thread ( ThreadEventBody {
418434 reason: ThreadEventReason :: Started ,
419435 thread_id: THREAD_ID ,
420436 } ) ] ,
421437 console_events : vec ! [ ] ,
422- }
438+ } )
423439 }
424440
425- fn handle_disconnect ( & self , req : Request , _args : DisconnectArguments ) -> DapOutput {
441+ fn handle_disconnect (
442+ & self ,
443+ req : Request ,
444+ _args : DisconnectArguments ,
445+ ) -> anyhow:: Result < DapOutput > {
426446 // Only send `Q` if currently in a debugging session.
427447 let is_debugging = { self . state . lock ( ) . unwrap ( ) . is_debugging } ;
428448 let console_events = if is_debugging {
@@ -431,38 +451,38 @@ impl DapHandler {
431451 vec ! [ ]
432452 } ;
433453
434- DapOutput {
454+ Ok ( DapOutput {
435455 response : req. success ( ResponseBody :: Disconnect ) ,
436456 dap_events : vec ! [ ] ,
437457 console_events,
438- }
458+ } )
439459 }
440460
441- fn handle_restart < T > ( & self , req : Request , _args : T ) -> DapOutput {
442- DapOutput {
461+ fn handle_restart < T > ( & self , req : Request , _args : T ) -> anyhow :: Result < DapOutput > {
462+ Ok ( DapOutput {
443463 response : req. success ( ResponseBody :: Restart ) ,
444464 dap_events : vec ! [ ] ,
445465 console_events : vec ! [ DapConsoleEvent :: Restart ] ,
446- }
466+ } )
447467 }
448468
449469 // All servers must respond to `Threads` requests, possibly with
450470 // a dummy thread as is the case here
451- fn handle_threads ( & self , req : Request ) -> DapOutput {
471+ fn handle_threads ( & self , req : Request ) -> anyhow :: Result < DapOutput > {
452472 let rsp = req. success ( ResponseBody :: Threads ( ThreadsResponse {
453473 threads : vec ! [ Thread {
454474 id: THREAD_ID ,
455475 name: String :: from( "R console" ) ,
456476 } ] ,
457477 } ) ) ;
458- DapOutput :: response ( rsp)
478+ Ok ( DapOutput :: response ( rsp) )
459479 }
460480
461481 fn handle_set_exception_breakpoints (
462482 & self ,
463483 req : Request ,
464484 args : SetExceptionBreakpointsArguments ,
465- ) -> DapOutput {
485+ ) -> anyhow :: Result < DapOutput > {
466486 {
467487 let mut state = self . state . lock ( ) . unwrap ( ) ;
468488 state. exception_breakpoint_filters = args. filters ;
@@ -475,10 +495,14 @@ impl DapHandler {
475495 breakpoints : None ,
476496 } ,
477497 ) ) ;
478- DapOutput :: response ( rsp)
498+ Ok ( DapOutput :: response ( rsp) )
479499 }
480500
481- fn handle_stacktrace ( & self , req : Request , args : StackTraceArguments ) -> DapOutput {
501+ fn handle_stacktrace (
502+ & self ,
503+ req : Request ,
504+ args : StackTraceArguments ,
505+ ) -> anyhow:: Result < DapOutput > {
482506 let stack = {
483507 let state = self . state . lock ( ) . unwrap ( ) ;
484508 let fallback_sources = & state. fallback_sources ;
@@ -496,24 +520,23 @@ impl DapHandler {
496520
497521 let start_frame = args. start_frame . unwrap_or ( 0 ) ;
498522 let Ok ( start) = usize:: try_from ( start_frame) else {
499- let rsp = req. error ( & format ! ( "Invalid start_frame: {start_frame}" ) ) ;
500- return DapOutput :: response ( rsp) ;
523+ return Err ( anyhow:: anyhow!( "Invalid start_frame: {start_frame}" ) ) ;
501524 } ;
502525 let start = std:: cmp:: min ( start, n_usize) ;
503526
504527 let end = if let Some ( levels) = args. levels {
505528 let Ok ( levels) = usize:: try_from ( levels) else {
506- let rsp = req. error ( & format ! ( "Invalid levels: {levels}" ) ) ;
507- return DapOutput :: response ( rsp) ;
529+ return Err ( anyhow:: anyhow!( "Invalid levels: {levels}" ) ) ;
508530 } ;
509531 std:: cmp:: min ( start. saturating_add ( levels) , n_usize)
510532 } else {
511533 n_usize
512534 } ;
513535
514536 let Ok ( total_frames) = i64:: try_from ( n_usize) else {
515- let rsp = req. error ( & format ! ( "Stack frame count overflows i64: {n_usize}" ) ) ;
516- return DapOutput :: response ( rsp) ;
537+ return Err ( anyhow:: anyhow!(
538+ "Stack frame count overflows i64: {n_usize}"
539+ ) ) ;
517540 } ;
518541 let stack = stack[ start..end] . to_vec ( ) ;
519542
@@ -522,16 +545,14 @@ impl DapHandler {
522545 total_frames : Some ( total_frames) ,
523546 } ) ) ;
524547
525- DapOutput :: response ( rsp)
548+ Ok ( DapOutput :: response ( rsp) )
526549 }
527550
528- fn handle_source ( & self , req : Request , _args : SourceArguments ) -> DapOutput {
529- let message = "Unsupported `source` request: {req:?}" ;
530- log:: error!( "{message}" ) ;
531- DapOutput :: response ( req. error ( message) )
551+ fn handle_source ( & self , _req : Request , _args : SourceArguments ) -> anyhow:: Result < DapOutput > {
552+ Err ( anyhow:: anyhow!( "Unsupported `source` request" ) )
532553 }
533554
534- fn handle_scopes ( & self , req : Request , args : ScopesArguments ) -> DapOutput {
555+ fn handle_scopes ( & self , req : Request , args : ScopesArguments ) -> anyhow :: Result < DapOutput > {
535556 let state = self . state . lock ( ) . unwrap ( ) ;
536557 let frame_id_to_variables_reference = & state. frame_id_to_variables_reference ;
537558
@@ -561,15 +582,19 @@ impl DapHandler {
561582 let rsp = req. success ( ResponseBody :: Scopes ( ScopesResponse { scopes } ) ) ;
562583
563584 drop ( state) ;
564- DapOutput :: response ( rsp)
585+ Ok ( DapOutput :: response ( rsp) )
565586 }
566587
567- fn handle_variables ( & self , req : Request , args : VariablesArguments ) -> DapOutput {
588+ fn handle_variables (
589+ & self ,
590+ req : Request ,
591+ args : VariablesArguments ,
592+ ) -> anyhow:: Result < DapOutput > {
568593 let variables_reference = args. variables_reference ;
569594 let variables = self . collect_r_variables ( variables_reference) ;
570595 let variables = self . make_variables ( variables) ;
571596 let rsp = req. success ( ResponseBody :: Variables ( VariablesResponse { variables } ) ) ;
572- DapOutput :: response ( rsp)
597+ Ok ( DapOutput :: response ( rsp) )
573598 }
574599
575600 fn collect_r_variables ( & self , variables_reference : i64 ) -> Vec < RVariable > {
@@ -605,24 +630,24 @@ impl DapHandler {
605630 _args : A ,
606631 cmd : DebugRequest ,
607632 resp : ResponseBody ,
608- ) -> DapOutput {
609- DapOutput {
633+ ) -> anyhow :: Result < DapOutput > {
634+ Ok ( DapOutput {
610635 response : req. success ( resp) ,
611636 dap_events : vec ! [ ] ,
612637 console_events : vec ! [ DapConsoleEvent :: DebugCommand ( cmd) ] ,
613- }
638+ } )
614639 }
615640
616- fn handle_pause ( & self , req : Request , _args : PauseArguments ) -> DapOutput {
641+ fn handle_pause ( & self , req : Request , _args : PauseArguments ) -> anyhow :: Result < DapOutput > {
617642 self . state . lock ( ) . unwrap ( ) . is_interrupting_for_debugger = true ;
618643
619644 log:: info!( "DAP: Received request to pause R, sending interrupt" ) ;
620645
621- DapOutput {
646+ Ok ( DapOutput {
622647 response : req. success ( ResponseBody :: Pause ) ,
623648 dap_events : vec ! [ ] ,
624649 console_events : vec ! [ DapConsoleEvent :: Interrupt ] ,
625- }
650+ } )
626651 }
627652}
628653
0 commit comments