I am implementing a XML-RPC API for my Zope app.
The API will be used by a CRM.
Both systems have company entities, which are mapped by an ID.
There is a field mapping, and there are also restrictions on field lengths, e.g. street name must not be too long, other wise my Zope app cannot print address labels.
Assume, the CRM sends a request to the create_company
endpoint, but e.g. the street name is too long.
As a first attempt on the side of my Zope app, I implemented a try-catch block, with the input validation in the try block, and a return Fault(faultCode="1", faultString="street name is too long")
in the except block.
Works fine, the client gets a reasonable error message, but.. the transaction is NOT aborted on the side of my Zope app.
This means, if there is something with side effects in my try block, it will be persisted even when the request is borked.
try:
validate()
# maybe side effects
except ValidationError:
return Fault(faultCode="1", faultString="street name is too long")
else:
# update company
return company.getId()
I then tried to re-raise an exception in the except block, which resulted in a generic 500 server fault
for the client with no useful information.
try:
validate()
# maybe side effects
except ValidationError:
raise ValueError # or raise Fault
else:
# update company
return company.getId()
My last attempt was to issue an transaction.abort()
in the exception block, which does not seem to have any effect. If there were side effects in the try block, they get persisted.
try:
validate()
# maybe side effects
except ValidationError:
transaction.abort()
return Fault(faultCode="1", faultString="street name is too long")
else:
# update company
return company.getId()
Anybody has a hint what to do?
REST is no option for now (due to pure Zope, time constraints... ).
Is the only chance to take 100% care that in the try-block no side effects occur?
Or what is the proper way to tell the client there is a problem with the data transmitted?