Cannot Catch Zato Exception


#1

Hi,

Newbie question. I test the following code with Python built-in HTTP server. It is able to talk to HTTP server.

However, when I terminate the HTTP server, the exception code is not run. Instead, I always get the standard Zato exception response (and error entry in the zato server log.)

from zato.common import ZatoException

def handle (self):
    
    try:
        tgt = self.outgoing.plain_http.get("con.tutorial.http") 
        
    except ZatoException, e:
        self.logger.info('tutorial.http-server-down:  Connection exception caught')

 else:           
        self.logger.info('tutorial.http-server-down:  Success')  

Regards,
Wesley


#2

Hello Wesley,

please post the actual code of your service - this looks like an extract that is not valid Python code plus the snippet you posted won’t trigger ZatoException under any circumstances so it’s quite difficult to say what would happen in your real service.

Thanks.


#3

Hi David,

The actual code. I trigger it from webadmin’s invoker.

Outgoing REST connection:  con.tutorial.http
-  Host:        http://10.0.2.2:9090
-  URL Path:    /

(Zato 2.0.7, installed from quickstart, is run within Virtualbox VM. This connection calls
Python built-in HTTP Server being running on the host Windows laptop.)

from future import absolute_import, division, print_function, unicode_literals
from zato.server.service import Service
from zato.common import ConnectionException, HTTPException, ZatoException

class TryHttpSysDown (Service):
name = “tutorial.http-server-down”

def handle (self):
    
    try:
        tgt = self.outgoing.plain_http.get("con.tutorial.http") 
           
    except ZatoException, e:
        self.logger.info('tutorial.http-server-down:  Connection exception caught')
         
    else:    

        conResult = tgt.conn.get(self.cid)   
            
        text = conResult.text  
        status = conResult.status_code
        reason = conResult.reason
            
        self.logger.info('tutorial.http-server-down:  text: {}, reason: {}, status: {}'.format(text, status, reason))  
        self.response.payload = text

#4

Thanks, I understand it better now.

The reason is that in this line …

tgt = self.outgoing.plain_http.get("con.tutorial.http") 

… you are only getting a handle to a connection object, you are not invoking anything yet. You are just getting an object from cache that can be later used for issuing HTTP calls.

This line:

conResult = tgt.conn.get(self.cid)   

This is the one that actually issues a GET request and this is what can return an exception, but not the previous one.

I think it would be clearer if you were, for instance, using POST instead of GET but here you have two .get calls and you were expecting for the first to already issue a remote call but this is not the case.

Let me know if it helps.


#5

Hi David,

This is the code that works. Catching “ZatoException” somehow does not work, but with Python base “Exception”, it works. Good enough for me. Thanks.

def handle (self):

    try:
        tgt = self.outgoing.plain_http.get("con.tutorial.http") 
        conResult = tgt.conn.get(self.cid)   

        text = conResult.text  
        status = conResult.status_code
        reason = conResult.reason
            
        self.logger.info('tutorial.http-server-down:  text: {}, reason: {}, status: {}'.format(text, status, reason))  
            
        self.response.payload = text
        
    except Exception, e:
        self.logger.info('tutorial.http-server-down:  Connection exception caught')