Comment 1 for bug 113140

Revision history for this message
Alexander Belchenko (bialix) wrote : Re: [Bug 113140] Pluggable IntelHexError(Exception) message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Enoch пишет:
> Public bug reported:
>
> May I suggest the following small change:
>
> - _fmt = 'IntelHex base error' #: format string
> + _fmt = '%(message)s' # user pluggable message
>
> I use it to report hex file constraint error as an ordinary
> IntelHexError exception case. For example,
>
> ih = IntelHex(filepath)
> bot = ih.minaddr()
> top = ih.maxaddr() + 1
> if (bot & 1) or (top & 1) or (top > APP_TOP) or top < 1024:
> raise IntelHexError(message='FOREIGN HEX FILE!')
>

Actually your proposal is slightly incorrect. Ideally you should
subclass base exception class and provide your own exception, e.g.

class MyError(IntelHexError):
    _fmt = 'FOREIGN HEX FILE!'

And then use this exception in your code:

if (bot & 1) or (top & 1) or (top > APP_TOP) or top < 1024:
    raise MyError

As workaround you right now can use base IntelHexError as you want
but to customize error message you need to pass new '_fmt' argument:

In [2]: raise intelhex.IntelHexError(_fmt='Custom error')
- ---------------------------------------------------------------------------
<class 'intelhex.IntelHexError'> Traceback (most recent call last)

C:\work\MyCode\intelhex\trunk\<ipython console> in <module>()

<class 'intelhex.IntelHexError'>: Custom error

Of course it possible to handle 'message' argument specially.
Actually it should be quite easy to implement, but I have more
important bugs to fix now.

>
> P/S My suggestion to capture IOError too and report it as IntelhexError
> still stands :-)

It's incorrect. I think transforming IOError to custom IntelHexError is
bad style. User is able to catch IOError himself. Per example:

try:
    ih = IntelHex('foo.hex')
except (IOError, IntelHexError):
    print 'Unable to load hex file'

[µ]

  affects /products/intelhex
  importance wishlist

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGP3uPzYr338mxwCURArmxAJ49T4cOeltETgzqoHTJ7y2Te7bo6QCdHTY0
urwSJzkc0HDc2u9lljsBhsg=
=Eacl
-----END PGP SIGNATURE-----