Python 7.0pl5, Python Manual manual Generating a Failure

Models: Python Manual 7.0pl5

1 124
Download 124 pages 59.17 Kb
Page 65
Image 65

Generating a Failure

The reason for requiring that methods be explicitly exported is that it would usually be quite dangerous to allow open access to all member functions of a class. This is because any class is likely to implement methods to assist in intermediate stages of processing a request. Providing automatic access to such member functions could compromise the operation of the class.

When a method is invoked as a result of a service request, the default behaviour will be that the value returned from the method will be what is returned to the caller as the response. If necessary, a method may explicitly indicate that a failure response should instead be returned. A method can also indicate that a delayed response will be sent. This latter case is useful when the service needs to do something first in order to generate a suitable response.

Generating a Failure

If a method encounters an error and raises an exception this will be caught by the service agent frame- work and a failure response will be generated. The value of the origin for this type of failure will be "netsvc" and the value of the error code will be "SERVER_APPLICATION_ERROR". If you want to generate a failure response which is specific to your application, you should catch any exceptions and indicate the type of failure response by calling the member function "abortResponse()".

class Database(netsvc.Service):

def __init__(self,name="database",**kw): netsvc.Service.__init__(self,name) self._database = MySQLdb.connect(**kw) self.exportMethod(self.execute)

def execute(self,query,args=None): try:

cursor = self._database.cursor() cursor.execute(query,args)

if cursor.description == None: return cursor.rowcount

return cursor.fetchall()

except MySQLdb.ProgrammingError,exception: details = netsvc.exceptionDetails() self.abortResponse(1,"Programming Error","db",details)

except MySQLdb.Error,(error,description): self.abortResponse(error,description,"mysql")

The four arguments to the member function "abortResponse()" are the error code, the description of the error, the origin and any additional details. It is recommended that an origin which clearly iden- tifies the source of the error, or namespace from which the error codes are derived always be used. If an origin is not used, it becomes impossible to programmatically deal with an error when different as- pects of a service generate overlapping error code sets.

Note that the "abortResponse()" member function will in turn raise its own special exception. When this exception is caught by the service agent framework, it will be translated into a failure re- sponse as described by the arguments used to call "abortResponse()". As a new exception is

65

Page 65
Image 65
Python 7.0pl5, Python Manual manual Generating a Failure