1+ use api:: client:: get_api_host;
12use http:: StatusCode ;
23
3- pub fn log_error ( error : & anyhow:: Error , base_message : Option < & str > ) -> i32 {
4+ pub ( crate ) const UNAUTHORIZED_CONTEXT : & str = concat ! (
5+ "Your Trunk organization URL slug or token may be incorrect - find it in the Trunk app" ,
6+ ) ;
7+
8+ fn add_settings_url_to_context (
9+ base_message : String ,
10+ domain : Option < String > ,
11+ org_url_slug : & String ,
12+ ) -> String {
13+ match domain {
14+ Some ( present_domain) => {
15+ let settings_url = format ! (
16+ "{}/{}/settings" ,
17+ present_domain. replace( "api" , "app" ) ,
18+ org_url_slug
19+ ) ;
20+ format ! (
21+ "{}\n Hint: Your settings page can be found at: {}" ,
22+ base_message, settings_url
23+ )
24+ }
25+ None => base_message,
26+ }
27+ }
28+
29+ const HELP_TEXT : & str = "\n \n For more help, contact us at https://slack.trunk.io/" ;
30+
31+ pub struct Context {
32+ pub base_message : Option < String > ,
33+ pub org_url_slug : String ,
34+ }
35+
36+ pub fn log_error ( error : & anyhow:: Error , context : Context ) -> i32 {
437 let root_cause = error. root_cause ( ) ;
38+ let Context {
39+ base_message,
40+ org_url_slug,
41+ } = context;
542 if let Some ( io_error) = root_cause. downcast_ref :: < std:: io:: Error > ( ) {
643 if io_error. kind ( ) == std:: io:: ErrorKind :: ConnectionRefused {
744 tracing:: warn!(
845 "{}" ,
9- message( base_message, "could not connect to trunk's server" )
46+ message( base_message, "Unable to connect to trunk's server" )
1047 ) ;
1148 return exitcode:: OK ;
1249 }
1350 }
1451
52+ let api_host = get_api_host ( ) ;
1553 if let Some ( reqwest_error) = root_cause. downcast_ref :: < reqwest:: Error > ( ) {
1654 if let Some ( status) = reqwest_error. status ( ) {
1755 if status == StatusCode :: UNAUTHORIZED
1856 || status == StatusCode :: FORBIDDEN
1957 || status == StatusCode :: NOT_FOUND
2058 {
21- tracing:: warn!( "{}" , message( base_message, "unauthorized to access trunk" ) , ) ;
59+ tracing:: warn!(
60+ "{}" ,
61+ add_settings_url_to_context(
62+ message( base_message, UNAUTHORIZED_CONTEXT ) ,
63+ Some ( api_host) ,
64+ & org_url_slug
65+ )
66+ ) ;
2267 return exitcode:: SOFTWARE ;
2368 }
2469 }
2570 }
2671
27- tracing:: error!( "{}" , message( base_message, "error" ) ) ;
72+ if let Some ( base_message) = base_message {
73+ tracing:: error!( "{}" , message( Some ( base_message) , HELP_TEXT ) ) ;
74+ } else {
75+ tracing:: error!( "{}" , error) ;
76+ }
2877 tracing:: error!( hidden_in_console = true , "Caused by error: {:#?}" , error) ;
2978 exitcode:: SOFTWARE
3079}
@@ -45,9 +94,30 @@ pub fn error_reason(error: &anyhow::Error) -> String {
4594 "unknown" . into ( )
4695}
4796
48- fn message ( base_message : Option < & str > , hint : & str ) -> String {
97+ fn message ( base_message : Option < String > , hint : & str ) -> String {
4998 match base_message {
50- Some ( base_message) => format ! ( "{} because {}" , base_message, hint) ,
99+ Some ( base_message) => format ! ( "{}\n \t {}" , base_message, hint) ,
51100 None => String :: from ( hint) ,
52101 }
53102}
103+
104+ #[ test]
105+ fn adds_settings_if_domain_present ( ) {
106+ let host = "https://api.fake-trunk.io" ;
107+ let final_context = add_settings_url_to_context (
108+ "base_context" . into ( ) ,
109+ Some ( host. into ( ) ) ,
110+ & String :: from ( "fake-org-slug" ) ,
111+ ) ;
112+ assert_eq ! (
113+ final_context,
114+ "base_context\n Hint: Your settings page can be found at: https://app.fake-trunk.io/fake-org-slug/settings" ,
115+ )
116+ }
117+
118+ #[ test]
119+ fn does_not_add_settings_if_domain_absent ( ) {
120+ let final_context =
121+ add_settings_url_to_context ( "base_context" . into ( ) , None , & String :: from ( "fake-org-slug" ) ) ;
122+ assert_eq ! ( final_context, "base_context" , )
123+ }
0 commit comments