1

After much googli searching to figure out whats going on, here it is:

I have a custom validation exception which takes a request and response

class ValidationException(Exception):
    message = "Caught Validation Exception"

    def __init__(self, request, response):
        self.details = {
            "request": request,
            "response": response
        }
        super(ValidationException, self).__init__(self.message, self.details)

I have an exception handler which will raise an instance of it on some condition:

class handler:
    if something:
        raise ValidationException(request, response)

The handler is called in the event we encounter an issue in a post

class Poster:
    def post(data):
        if self.last_response.status_code not in self.valid_post_codes:
            self.exception_handler.handleException(self.last_request, self.last_response)

The problem is, I'm raising the ValidationException, getting it in my traceback, but, it doesn't seem to get caught where I want it.

def testThis(self):
    try:
        self.poster.post(json.dumps({}))
    except ValidationException:
        print "got validation"
    except Exception:
        print "got exception"

Result: "got exception"

traceback

lib/service/pas/api/order.py line 24 in postOrder
  return self.post()
lib/service/base.py line 42 in post
  self.exception_handler.handleException(self.last_request, self.last_response)
lib/service/exception/handler.py line 14 in handleException
  raise ValidationException(request, response)
ValidationException:

For what its worth:

assertRaises(ValidationException, self.poster.post, json.dumps({}))

only catches Exception as well. Any ideas? :\ Any help is greatly appreciated! Thanks in advance

mikeyseay
  • 169
  • 1
  • 10

2 Answers2

1

Well well well... So..

My IDE prefixed my import with "lib" which imported Exceptions.ValidationException.

When I throw my.own.ValidationException elsewhere, it wasn't being caught as it wasn't of the same type. Just so turned out there happened to be another ValidationException I didn't know about...

Thats amazing, NOT!

mikeyseay
  • 169
  • 1
  • 10
0

I could not replicate it with the code given, but two suspicious places to consider:

  1. You seem to be mixing old-style and new-style classes. That can lead to subtle bugs. Try class Handler(object):, class Poster(object): and in all other instances make classes new-style classes that are explicit subclasses of object.

  2. You seem to have a complex exception invocation mechanism, with

    self.exception_handler.handleException(self.last_request, self.last_response)
    

    Why not just:

    raise ValidationException(self.last_request, self.last_repsonse)
    

    right there? At least as a debugging experiment, remove or short-circuit modules, functions, and code you're not sure about or isn't preven to work. Cut to the chase, see if you can fix the behavior. At least, it might help you narrow down where the problem is. If it's in the handler, you can then choose to either fix it or chuck it.

Jonathan Eunice
  • 21,653
  • 6
  • 75
  • 77