@@ -79,26 +79,20 @@ pub fn update_metadata(metadata: Metadata) -> anyhow::Result<()> {
7979}
8080
8181/// Format a panic message with optional location information.
82- fn format_panic_message (
82+ fn format_message (
8383 category : & str ,
84- description : & str ,
84+ panic_message : & str ,
8585 location : Option < & panic:: Location > ,
8686) -> String {
87- let base = match location {
88- Some ( loc) => format ! (
89- "Process panicked with {} ({}:{}:{})" ,
90- category,
91- loc. file( ) ,
92- loc. line( ) ,
93- loc. column( )
94- ) ,
95- None => format ! ( "Process panicked with {}" , category) ,
87+ let base = if panic_message. is_empty ( ) {
88+ format ! ( "Process panicked with {}" , category)
89+ } else {
90+ format ! ( "Process panicked with {} \" {}\" " , category, panic_message)
9691 } ;
9792
98- if description. is_empty ( ) {
99- base
100- } else {
101- format ! ( "{}: {}" , base, description)
93+ match location {
94+ Some ( loc) => format ! ( "{} ({}:{}:{})" , base, loc. file( ) , loc. line( ) , loc. column( ) ) ,
95+ None => base,
10296 }
10397}
10498
@@ -124,12 +118,12 @@ pub fn register_panic_hook() -> anyhow::Result<()> {
124118 panic:: set_hook ( Box :: new ( |panic_info| {
125119 // Extract panic message from payload (supports &str and String)
126120 let message = if let Some ( & s) = panic_info. payload ( ) . downcast_ref :: < & str > ( ) {
127- format_panic_message ( "message" , s, panic_info. location ( ) )
121+ format_message ( "message" , s, panic_info. location ( ) )
128122 } else if let Some ( s) = panic_info. payload ( ) . downcast_ref :: < String > ( ) {
129- format_panic_message ( "message" , s. as_str ( ) , panic_info. location ( ) )
123+ format_message ( "message" , s. as_str ( ) , panic_info. location ( ) )
130124 } else {
131125 // For non-string types, use a generic message
132- format_panic_message ( "unknown type" , "" , panic_info. location ( ) )
126+ format_message ( "unknown type" , "" , panic_info. location ( ) )
133127 } ;
134128
135129 // Store the message, cleaning up any old message
@@ -394,4 +388,58 @@ mod tests {
394388 assert_eq ! ( stored_metadata. library_name, "test" ) ;
395389 }
396390 }
391+
392+ #[ test]
393+ fn test_format_message_with_message_and_location ( ) {
394+ let location = panic:: Location :: caller ( ) ;
395+ let result = format_message ( "message" , "test panic" , Some ( location) ) ;
396+
397+ assert ! ( result. starts_with( "Process panicked with message \" test panic\" (" ) ) ;
398+ assert ! ( result. contains( & format!( "{}:" , location. file( ) ) ) ) ;
399+ assert ! ( result. contains( & format!( ":{}" , location. line( ) ) ) ) ;
400+ assert ! ( result. ends_with( & format!( "{})" , location. column( ) ) ) ) ;
401+ }
402+
403+ #[ test]
404+ fn test_format_message_with_message_no_location ( ) {
405+ let result = format_message ( "message" , "test panic" , None ) ;
406+ assert_eq ! ( result, "Process panicked with message \" test panic\" " ) ;
407+ }
408+
409+ #[ test]
410+ fn test_format_message_empty_message_with_location ( ) {
411+ let location = panic:: Location :: caller ( ) ;
412+ let result = format_message ( "unknown type" , "" , Some ( location) ) ;
413+
414+ assert ! ( result. starts_with( "Process panicked with unknown type (" ) ) ;
415+ assert ! ( result. contains( & format!( "{}:" , location. file( ) ) ) ) ;
416+ assert ! ( result. ends_with( & format!( "{})" , location. column( ) ) ) ) ;
417+ }
418+
419+ #[ test]
420+ fn test_format_message_empty_message_no_location ( ) {
421+ let result = format_message ( "unknown type" , "" , None ) ;
422+ assert_eq ! ( result, "Process panicked with unknown type" ) ;
423+ }
424+
425+ #[ test]
426+ fn test_format_message_different_categories ( ) {
427+ let result1 = format_message ( "message" , "test" , None ) ;
428+ assert_eq ! ( result1, "Process panicked with message \" test\" " ) ;
429+
430+ let result2 = format_message ( "unknown type" , "" , None ) ;
431+ assert_eq ! ( result2, "Process panicked with unknown type" ) ;
432+
433+ let result3 = format_message ( "custom category" , "content" , None ) ;
434+ assert_eq ! ( result3, "Process panicked with custom category \" content\" " ) ;
435+ }
436+
437+ #[ test]
438+ fn test_format_message_with_special_characters ( ) {
439+ let result = format_message ( "message" , "test \" quoted\" 'text'" , None ) ;
440+ assert_eq ! (
441+ result,
442+ "Process panicked with message \" test \" quoted\" 'text'\" "
443+ ) ;
444+ }
397445}
0 commit comments