@@ -1976,10 +1976,16 @@ def load_kubernetes_configuration(filename):
19761976 return yaml .safe_load (stream )
19771977 except OSError as ex :
19781978 if getattr (ex , 'errno' , 0 ) == errno .ENOENT :
1979- raise CLIError ('{} does not exist' .format (filename ))
1979+ raise CLIError ('{} does not exist' .format (filename )) from ex
1980+ if getattr (ex , 'errno' , 0 ) in (errno .EACCES , errno .EPERM ):
1981+ raise FileOperationError (
1982+ 'Permission denied when trying to read {}. '
1983+ 'Please ensure you have read access to this file, or specify a different file path '
1984+ 'using the --file/-f argument.' .format (filename )
1985+ ) from ex
19801986 raise
19811987 except (yaml .parser .ParserError , UnicodeDecodeError ) as ex :
1982- raise CLIError ('Error parsing {} ({})' .format (filename , str (ex )))
1988+ raise CLIError ('Error parsing {} ({})' .format (filename , str (ex ))) from ex
19831989
19841990
19851991def merge_kubernetes_configurations (existing_file , addition_file , replace , context_name = None ):
@@ -2025,8 +2031,17 @@ def merge_kubernetes_configurations(existing_file, addition_file, replace, conte
20252031 existing_file_perms ,
20262032 )
20272033
2028- with open (existing_file , 'w+' ) as stream :
2029- yaml .safe_dump (existing , stream , default_flow_style = False )
2034+ try :
2035+ with open (existing_file , 'w+' ) as stream :
2036+ yaml .safe_dump (existing , stream , default_flow_style = False )
2037+ except OSError as ex :
2038+ if getattr (ex , 'errno' , 0 ) in (errno .EACCES , errno .EPERM , errno .EROFS ):
2039+ raise FileOperationError (
2040+ 'Permission denied when trying to write to {}. '
2041+ 'Please ensure you have write access to this file, or specify a different file path '
2042+ 'using the --file/-f argument.' .format (existing_file )
2043+ ) from ex
2044+ raise
20302045
20312046 current_context = addition .get ('current-context' , 'UNKNOWN' )
20322047 msg = 'Merged "{}" as current context in {}' .format (
0 commit comments