@@ -36,6 +36,11 @@ use oauth2::{Authenticator, DefaultAuthenticatorDelegate};
3636use serde::json;
3737use clap::ArgMatches;
3838
39+ enum DoitError {
40+ IoError(String, io::Error),
41+ ApiError(api::Error),
42+ }
43+
3944struct Engine<'n, 'a> {
4045 opt: ArgMatches<'n, 'a>,
4146 hub: ${ hub_type_name} <hyper::Client , Authenticator <DefaultAuthenticatorDelegate, JsonTokenStorage, hyper::Client >>,
@@ -46,15 +51,15 @@ impl<'n, 'a> Engine<'n, 'a> {
4651% for resource in sorted (c.rta_map.keys()):
4752 % for method in sorted (c.rta_map[resource]):
4853 fn ${ call_method_ident(resource, method)} (&self, opt: &ArgMatches<'n, 'a>, dry_run: bool, err: &mut InvalidOptionsError)
49- -> Option< api::Error > {
54+ -> Result<(), DoitError > {
5055 ${ self ._method_call_impl(c, resource, method) | indent_all_but_first_by(2 )}
5156 }
5257
5358 % endfor # each method
5459% endfor
55- fn _doit(&self, dry_run: bool) -> (Option< api::Error >, Option<InvalidOptionsError >) {
60+ fn _doit(&self, dry_run: bool) -> Result< Result < (), DoitError >, Option<InvalidOptionsError >> {
5661 let mut err = InvalidOptionsError::new();
57- let mut call_result: Option< api::Error > = None ;
62+ let mut call_result: Result<(), DoitError > = Ok(()) ;
5863 let mut err_opt: Option<InvalidOptionsError > = None;
5964## RESOURCE LOOP: check for set primary subcommand
6065 match ${ SOPT + ' .subcommand()' } {
@@ -83,8 +88,10 @@ impl<'n, 'a> Engine<'n, 'a> {
8388 if err.issues.len() > 0 {
8489 err_opt = Some(err);
8590 }
91+ Err(err_opt)
92+ } else {
93+ Ok(call_result)
8694 }
87- (call_result, err_opt)
8895 }
8996
9097 // Please note that this call will fail if any part of the opt can't be handled
@@ -117,15 +124,17 @@ impl<'n, 'a> Engine<'n, 'a> {
117124 };
118125
119126 match engine._doit(true) {
120- (_, Some(err)) => Err(err),
121- _ => Ok(engine),
127+ Err(Some(err)) => Err(err),
128+ Err(None) => Ok(engine),
129+ Ok(_) => unreachable!(),
122130 }
123131 }
124132
125- // Execute the call with all the bells and whistles, informing the caller only if there was an error.
126- // The absense of one indicates success.
127- fn doit(&self) -> Option<api::Error > {
128- self._doit(false).0
133+ fn doit(&self) -> Result<(), DoitError> {
134+ match self._doit(false) {
135+ Ok(res) => res,
136+ Err(_) => unreachable!(),
137+ }
129138 }
130139}
131140</%def >
@@ -261,7 +270,7 @@ let mime_type = input_mime_from_opts(${opt_value(MIME_ARG, default=DEFAULT_MIME)
261270let protocol = CallType::Standard;
262271% endif # support upload
263272if dry_run {
264- None
273+ Ok(())
265274} else {
266275 assert!(err.issues.len() == 0);
267276 % if method_default_scope(mc.m):
@@ -270,9 +279,11 @@ if dry_run {
270279 }
271280 % endif
272281 ## Make the call, handle uploads, handle downloads (also media downloads|json decoding)
273- ## TODO: unify error handling
274282 % if handle_output:
275- let mut ostream = writer_from_opts(opt.value_of("${ (OUT_ARG )} "));
283+ let mut ostream = match writer_from_opts(opt.value_of("${ (OUT_ARG )} ")) {
284+ Ok(mut f) => f,
285+ Err(io_err) => return Err(DoitError::IoError(${ opt_value(OUT_ARG , default = ' -' )} .to_string(), io_err)),
286+ };
276287 % endif # handle output
277288 match match protocol {
278289 % if mc.media_params:
@@ -285,7 +296,7 @@ if dry_run {
285296 _ => unreachable!()
286297 % endif
287298 } {
288- Err(api_err) => Some( api_err),
299+ Err(api_err) => Err(DoitError::ApiError( api_err) ),
289300 % if mc.response_schema:
290301 Ok((mut response, output_schema)) => {
291302 % else :
@@ -310,7 +321,7 @@ if dry_run {
310321 % if track_download_flag:
311322 }
312323 % endif
313- None
324+ Ok(())
314325 }
315326 }
316327}\
0 commit comments