@@ -22,6 +22,29 @@ use crate::commands::playlist::PlaylistCommand;
2222use crate :: commands:: { CommandError , Formatter , OutputType , PlaylistFile } ;
2323const DEFAULT_ASSET_DURATION : u32 = 15 ;
2424
25+ /// Returns a user-friendly error message for authentication errors.
26+ fn get_authentication_error_message ( e : & AuthenticationError ) -> String {
27+ match e {
28+ AuthenticationError :: Io ( io_err) if io_err. kind ( ) == std:: io:: ErrorKind :: NotFound => {
29+ "Not logged in. Please run `screenly login` first to authenticate." . to_string ( )
30+ }
31+ _ => {
32+ format ! ( "Authentication error: {e}. Please run `screenly login` to authenticate." )
33+ }
34+ }
35+ }
36+
37+ /// Creates an Authentication instance or exits with a user-friendly error message.
38+ fn get_authentication ( ) -> Authentication {
39+ match Authentication :: new ( ) {
40+ Ok ( auth) => auth,
41+ Err ( e) => {
42+ error ! ( "{}" , get_authentication_error_message( & e) ) ;
43+ std:: process:: exit ( 1 ) ;
44+ }
45+ }
46+ }
47+
2548#[ derive( Error , Debug ) ]
2649enum ParseError {
2750 #[ error( "missing \" =\" symbol" ) ]
@@ -547,7 +570,7 @@ fn get_user_input() -> String {
547570}
548571
549572pub fn handle_cli_screen_command ( command : & ScreenCommands ) {
550- let authentication = Authentication :: new ( ) . expect ( "Failed to load authentication." ) ;
573+ let authentication = get_authentication ( ) ;
551574 let screen_command = commands:: screen:: ScreenCommand :: new ( authentication) ;
552575
553576 match command {
@@ -591,8 +614,7 @@ pub fn handle_cli_screen_command(command: &ScreenCommands) {
591614}
592615
593616pub fn handle_cli_playlist_command ( command : & PlaylistCommands ) {
594- let playlist_command =
595- PlaylistCommand :: new ( Authentication :: new ( ) . expect ( "Failed to load authentication." ) ) ;
617+ let playlist_command = PlaylistCommand :: new ( get_authentication ( ) ) ;
596618 match command {
597619 PlaylistCommands :: Create {
598620 json,
@@ -678,7 +700,7 @@ pub fn handle_cli_playlist_command(command: &PlaylistCommands) {
678700}
679701
680702pub fn handle_cli_asset_command ( command : & AssetCommands ) {
681- let authentication = Authentication :: new ( ) . expect ( "Failed to load authentication." ) ;
703+ let authentication = get_authentication ( ) ;
682704 let asset_command = commands:: asset:: AssetCommand :: new ( authentication) ;
683705
684706 match command {
@@ -819,7 +841,7 @@ pub fn handle_cli_asset_command(command: &AssetCommands) {
819841}
820842
821843pub fn handle_cli_edge_app_command ( command : & EdgeAppCommands ) {
822- let authentication = Authentication :: new ( ) . expect ( "Failed to load authentication." ) ;
844+ let authentication = get_authentication ( ) ;
823845 let edge_app_command = commands:: edge_app:: EdgeAppCommand :: new ( authentication) ;
824846
825847 match command {
@@ -1221,4 +1243,28 @@ mod tests {
12211243
12221244 assert_eq ! ( new_path, dir_path. join( "screenly.yml" ) ) ;
12231245 }
1246+
1247+ #[ test]
1248+ fn test_get_authentication_error_message_when_not_logged_in ( ) {
1249+ let io_err = std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , "file not found" ) ;
1250+ let auth_err = AuthenticationError :: Io ( io_err) ;
1251+
1252+ let message = get_authentication_error_message ( & auth_err) ;
1253+
1254+ assert_eq ! (
1255+ message,
1256+ "Not logged in. Please run `screenly login` first to authenticate."
1257+ ) ;
1258+ }
1259+
1260+ #[ test]
1261+ fn test_get_authentication_error_message_for_other_errors ( ) {
1262+ let io_err = std:: io:: Error :: new ( std:: io:: ErrorKind :: PermissionDenied , "permission denied" ) ;
1263+ let auth_err = AuthenticationError :: Io ( io_err) ;
1264+
1265+ let message = get_authentication_error_message ( & auth_err) ;
1266+
1267+ assert ! ( message. contains( "Authentication error" ) ) ;
1268+ assert ! ( message. contains( "Please run `screenly login` to authenticate" ) ) ;
1269+ }
12241270}
0 commit comments