11use std:: collections:: HashMap ;
22use std:: io;
33use std:: process:: { Child , Command , Stdio } ;
4+ use thiserror:: Error ;
45
5- use log:: debug;
6+ use log:: { debug, error } ;
67
78use crate :: { paramparsing, Operation } ;
89
@@ -27,11 +28,19 @@ fn spawn_helper(helper: &str, operation: Operation) -> io::Result<Child> {
2728 Ok ( cmd. spawn ( ) ?)
2829}
2930
31+ #[ derive( Error , Debug ) ]
32+ pub enum CredHelperError {
33+ #[ error( "{0}" ) ]
34+ Io ( #[ from] std:: io:: Error ) ,
35+ #[ error( "credential helper exited with code {0}" ) ]
36+ Non0ExitCode ( i32 , Option < HashMap < String , String > > ) ,
37+ }
38+
3039pub fn run (
3140 helper : & str ,
3241 operation : Operation ,
3342 params : & HashMap < String , String > ,
34- ) -> io :: Result < HashMap < String , String > > {
43+ ) -> Result < HashMap < String , String > , CredHelperError > {
3544 let mut process = spawn_helper ( helper, operation) ?;
3645 let mut stdin = process. stdin . take ( ) . unwrap ( ) ;
3746 paramparsing:: write_to ( & params, & mut stdin) ?;
@@ -40,6 +49,14 @@ pub fn run(
4049 let mut stdout = process. stdout . take ( ) . unwrap ( ) ;
4150 let output_params = paramparsing:: parse_from ( & mut stdout) ?;
4251 drop ( stdout) ;
43- process. wait ( ) ?;
44- Ok ( output_params)
52+ let output = process. wait ( ) ?;
53+
54+ if !output. success ( ) {
55+ Err ( CredHelperError :: Non0ExitCode (
56+ output. code ( ) . unwrap_or_default ( ) ,
57+ Some ( output_params) ,
58+ ) )
59+ } else {
60+ Ok ( output_params)
61+ }
4562}
0 commit comments