[GRASS-dev] [GRASS GIS] #2326: Command functions in grass.script.core miss a correct error reporting

GRASS GIS trac at osgeo.org
Sun Jun 15 07:23:10 PDT 2014


#2326: Command functions in grass.script.core miss a correct error reporting
--------------------------------+-------------------------------------------
 Reporter:  wenzeslaus          |       Owner:  grass-dev@…              
     Type:  enhancement         |      Status:  new                      
 Priority:  major               |   Milestone:  7.1.0                    
Component:  Python              |     Version:  svn-trunk                
 Keywords:  script, exceptions  |    Platform:  All                      
      Cpu:  Unspecified         |  
--------------------------------+-------------------------------------------

Comment(by wenzeslaus):

 Replying to [comment:6 wenzeslaus]:
 >
 > Another consideration is raising exception versus calling `fatal()`
 function which can be set to raise exception but this exception would have
 to contain everything in a message while a custom exception (I used one
 derived from
 [https://docs.python.org/2/library/subprocess.html#subprocess.CalledProcessError
 subprocess.CalledProcessError]) can store return code, command (or
 command/module name) and stderr separately.
 >

 To take advantage of custom exception with additional info and keeping
 `fatal()` with exit as a default behavior we can use the following
 function in the `*_command` functions.

 {{{
 #!python
 def called_command_error(cmd, returncode, errors)
     global raise_on_error
     if raise_on_error:
         raise CalledCommandError(returncode, cmd, erros=errors)
     # the same as CalledCommandError.__str__ is doing
     msg = _("Command '{cmd}' returned non-zero exit status {rc}"
             " and the following errors.\n"
             "%{errors}").format(cmd=cmd, rc=returncode,
                                 errors=errors)
     # fatal function will test raise_on_error again
     # the alternative is to mirror code from fatal function completely
     # which would be: error(msg); sys.exit(1)
     fatal(msg)
 }}}

-- 
Ticket URL: <http://trac.osgeo.org/grass/ticket/2326#comment:7>
GRASS GIS <http://grass.osgeo.org>



More information about the grass-dev mailing list