← Back to team overview

lazr-users team mailing list archive

Re: Exporting exceptions from lazr.restful

 

On Jul 26, 2009, at 9:33 PM, Barry Warsaw wrote:

I don't really understand how to export exceptions in lazr.restful. In Mailman3 I have an exception called BadDomainSpecificationError, and I've tried to declare it both of these two ways:

@error_status(400)
class BadDomainSpecificationError(MailmanError):
"""The specification of a virtual domain is invalid or duplicated."""


class BadDomainSpecificationError(MailmanError):
"""The specification of a virtual domain is invalid or duplicated."""
   webservice_error(400)


However, when the application raises BadDomainSpecificationError, I always get a 500 exception code. I'm using Python 2.6.

I don't really understand what the two different declarations are for.

These are just different spellings for the same thing. Since I'm on Python 2.6, I'm going to use the class decorator.

What else am I missing to properly export this exception error code?

With many thanks to Leonard and Francis, I've mostly gotten this working. The problem was that my IPublication.handleException() implementation needs to look up a view to handle the exception. My method now looks like this:

    def handleException(self, application, request, exc_info,
                        retry_allowed=True):
        """See `IPublication`."""
        # Any in-progress transaction must be aborted.
        config.db.abort()
# Reproduce the behavior of ZopePublication by looking up a view
        # for this exception.
        exception = exc_info[1]
# XXX BAW 2009-08-06 This should not be necessary. I need to register
        # a view so that 404 will be returned for a NotFound.
        if isinstance(exception, NotFound):
            request.response.reset()
            request.response.setStatus(404)
            request.response.setResult('')
            return
view = queryMultiAdapter((exception, request), name='index.html')
        if view is not None:
            exc_info = None
            request.response.reset()
            request.response.setResult(view())
        else:
            traceback.print_exception(*exc_info)

What's not quite right yet?

I need to register a view to turn NotFound into a 404, or perhaps lazr.restful should do this for us.

I'd like to get the 400 HTTP error have a more useful error message than "Bad Request". Somehow, the application level exception string should be included in the HTTP error (I think). I'm not sure how to make that happen though.

Still, it's good enough for now.
-Barry

Attachment: PGP.sig
Description: This is a digitally signed message part


References