1
- use std:: sync:: OnceLock ;
2
-
1
+ use anyhow:: Context ;
3
2
use lsp_types:: notification:: Notification ;
3
+ use std:: sync:: OnceLock ;
4
4
5
5
use crate :: server:: ClientSender ;
6
6
@@ -10,53 +10,31 @@ pub(crate) fn init_messenger(client_sender: ClientSender) {
10
10
MESSENGER
11
11
. set ( client_sender)
12
12
. expect ( "messenger should only be initialized once" ) ;
13
-
14
- // unregister any previously registered panic hook
15
- let _ = std:: panic:: take_hook ( ) ;
16
-
17
- // When we panic, try to notify the client.
18
- std:: panic:: set_hook ( Box :: new ( move |panic_info| {
19
- if let Some ( messenger) = MESSENGER . get ( ) {
20
- let _ = messenger. send ( lsp_server:: Message :: Notification (
21
- lsp_server:: Notification {
22
- method : lsp_types:: notification:: ShowMessage :: METHOD . into ( ) ,
23
- params : serde_json:: to_value ( lsp_types:: ShowMessageParams {
24
- typ : lsp_types:: MessageType :: ERROR ,
25
- message : String :: from (
26
- "The Ruff language server exited with a panic. See the logs for more details."
27
- ) ,
28
- } )
29
- . unwrap_or_default ( ) ,
30
- } ,
31
- ) ) ;
32
- }
33
-
34
- let backtrace = std:: backtrace:: Backtrace :: force_capture ( ) ;
35
- tracing:: error!( "{panic_info}\n {backtrace}" ) ;
36
- #[ allow( clippy:: print_stderr) ]
37
- {
38
- // we also need to print to stderr directly in case tracing hasn't
39
- // been initialized.
40
- eprintln ! ( "{panic_info}\n {backtrace}" ) ;
41
- }
42
- } ) ) ;
43
13
}
44
14
45
15
pub ( crate ) fn show_message ( message : String , message_type : lsp_types:: MessageType ) {
16
+ try_show_message ( message, message_type) . unwrap ( ) ;
17
+ }
18
+
19
+ pub ( super ) fn try_show_message (
20
+ message : String ,
21
+ message_type : lsp_types:: MessageType ,
22
+ ) -> crate :: Result < ( ) > {
46
23
MESSENGER
47
24
. get ( )
48
- . expect ( "messenger should be initialized" )
25
+ . ok_or_else ( || anyhow :: anyhow! ( "messenger not initialized" ) ) ?
49
26
. send ( lsp_server:: Message :: Notification (
50
27
lsp_server:: Notification {
51
28
method : lsp_types:: notification:: ShowMessage :: METHOD . into ( ) ,
52
29
params : serde_json:: to_value ( lsp_types:: ShowMessageParams {
53
30
typ : message_type,
54
31
message,
55
- } )
56
- . unwrap ( ) ,
32
+ } ) ?,
57
33
} ,
58
34
) )
59
- . expect ( "message should send" ) ;
35
+ . context ( "Failed to send message" ) ?;
36
+
37
+ Ok ( ( ) )
60
38
}
61
39
62
40
/// Sends an error to the client with a formatted message. The error is sent in a
0 commit comments