wic: refactored processing of wic exceptions

All wic exceptions are now inherited from new base exception
class WicError. It makes them easy to maintain and catch.

Processing of exceptions is done this way:
    Known wic exceptions cause wic to print error message to stdout.
    Unknown exceptions are not catched anymore and produce standard
    python traceback.

(From OE-Core rev: e5e2c18ce4344c14d9e52ece916333bd0a619281)

Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Ed Bartosh 2015-05-27 14:05:20 +03:00 committed by Richard Purdie
parent 9761becc7f
commit 346ef9245d
2 changed files with 13 additions and 32 deletions

View File

@ -16,32 +16,14 @@
# with this program; if not, write to the Free Software Foundation, Inc., 59 # with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
class CreatorError(Exception): class WicError(Exception):
"""An exception base class for all imgcreate errors.""" pass
keyword = '<creator>'
def __init__(self, msg): class CreatorError(WicError):
self.msg = msg pass
def __str__(self): class Usage(WicError):
if isinstance(self.msg, unicode): pass
self.msg = self.msg.encode('utf-8', 'ignore')
else:
self.msg = str(self.msg)
return self.keyword + self.msg
class Usage(CreatorError): class ImageError(WicError):
keyword = '<usage>' pass
def __str__(self):
if isinstance(self.msg, unicode):
self.msg = self.msg.encode('utf-8', 'ignore')
else:
self.msg = str(self.msg)
return self.keyword + self.msg + ', please use "--help" for more info'
class KsError(CreatorError):
keyword = '<kickstart>'
class ImageError(CreatorError):
keyword = '<mount>'

View File

@ -53,6 +53,7 @@ else:
bitbake_main = None bitbake_main = None
from wic.utils.oe.misc import find_bitbake_env_lines, set_bitbake_env_lines from wic.utils.oe.misc import find_bitbake_env_lines, set_bitbake_env_lines
from wic.utils.errors import WicError
from image import engine from image import engine
from image import help as hlp from image import help as hlp
@ -309,10 +310,8 @@ def main(argv):
if __name__ == "__main__": if __name__ == "__main__":
try: try:
ret = main(sys.argv[1:]) sys.exit(main(sys.argv[1:]))
except Exception: except WicError as err:
ret = 1 print >> sys.stderr, "ERROR:", err
import traceback sys.exit(1)
traceback.print_exc()
sys.exit(ret)