Commit 1fc08717 authored by Marco Mariani's avatar Marco Mariani

Parse and display error messages from server (GET and POST).

parent 421c8876
...@@ -36,12 +36,14 @@ __all__ = ["slap", "ComputerPartition", "Computer", "SoftwareRelease", ...@@ -36,12 +36,14 @@ __all__ = ["slap", "ComputerPartition", "Computer", "SoftwareRelease",
import httplib import httplib
import logging import logging
import re
import socket import socket
import ssl import ssl
import traceback import traceback
import urllib import urllib
import urlparse import urlparse
from xml.sax import saxutils
import zope.interface import zope.interface
from interface import slap as interface from interface import slap as interface
from xml_marshaller import xml_marshaller from xml_marshaller import xml_marshaller
...@@ -339,6 +341,19 @@ class Computer(SlapDocument): ...@@ -339,6 +341,19 @@ class Computer(SlapDocument):
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def parsed_error_message(status, body, path):
m = re.search('(Error Value:\n.*)', body, re.MULTILINE)
if m:
match = ' '.join(line.strip() for line in m.group(0).split('\n'))
return '%s (status %s while calling %s)' % (
saxutils.unescape(match),
status,
path
)
else:
return 'Server responded with wrong code %s with %s' % (status, path)
class ComputerPartition(SlapRequester): class ComputerPartition(SlapRequester):
zope.interface.implements(interface.IComputerPartition) zope.interface.implements(interface.IComputerPartition)
...@@ -594,8 +609,9 @@ class ConnectionHelper: ...@@ -594,8 +609,9 @@ class ConnectionHelper:
elif self.response.status == httplib.FORBIDDEN: elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized(path) raise Unauthorized(path)
elif self.response.status != httplib.OK: elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \ message = parsed_error_message(self.response.status,
(self.response.status, path) self.response.read(),
path)
raise ServerError(message) raise ServerError(message)
finally: finally:
socket.setdefaulttimeout(default_timeout) socket.setdefaulttimeout(default_timeout)
...@@ -625,8 +641,9 @@ class ConnectionHelper: ...@@ -625,8 +641,9 @@ class ConnectionHelper:
elif self.response.status == httplib.FORBIDDEN: elif self.response.status == httplib.FORBIDDEN:
raise Unauthorized("%s - %s" % (path, parameter_dict)) raise Unauthorized("%s - %s" % (path, parameter_dict))
elif self.response.status != httplib.OK: elif self.response.status != httplib.OK:
message = 'Server responded with wrong code %s with %s' % \ message = parsed_error_message(self.response.status,
(self.response.status, path) self.response.read(),
path)
raise ServerError(message) raise ServerError(message)
finally: finally:
socket.setdefaulttimeout(default_timeout) socket.setdefaulttimeout(default_timeout)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment