Commit b9c0a950 authored by Franziska Koehn's avatar Franziska Koehn
Browse files

exceptions for statuscodes added

parent d10bf7af
......@@ -184,7 +184,6 @@ def retry_qry(fun):
try:
return fun(*args, **kwargs)
except Exception as e:
print(("wrapped", e))
if type(last_exception) is type(e):
raise e
last_exception = e
......@@ -251,12 +250,27 @@ def search_for(host, root_element, constraints, search_fields, user, passw):
tree = create_xml_tree(root_element, constraints, search_fields)
tree.write(tmp, xml_declaration=True)
tmp.flush()
r = requests.post(url, files={'file': open(tmp.name, 'rb')}, auth=HTTPBasicAuth(user, passw))
try:
r = requests.post(url, files={'file': open(tmp.name, 'rb')}, auth=HTTPBasicAuth(user, passw))
except requests.exceptions.InvalidSchema as e:
raise xsa.errors.ServerNotFoundError(e)
except requests.exceptions.InvalidURL as e:
raise xsa.errors.ServerNotFoundError(e)
except requests.RequestException as e:
raise xsa.errors.Error(e)
print("response=",r.status_code)
if r.status_code == requests.codes.ok:
json_file=(json.loads(r.text))['ResultSet']['Result']
return JsonTable(json_file)
elif r.status_code == requests.codes.unauthorized:
raise xsa.errors.UnauthorizedError("HTTP 401: unauthorized")
elif r.status_code == requests.codes.forbidden:
raise xsa.errors.Error("HTTP 403: forbidden (check query definition, e.g. roottype)")
elif r.status_code == requests.codes.not_found:
raise xsa.errors.ServerNotFoundError("HTTP 404: not found (check host-address)")
elif r.status_code == requests.codes.internal_server_error:
raise xsa.errors.Error("HTTP 500: Internal Server Error (check query definition, e.g. constraints)")
def create_xml_tree(root_type, query, fields):
import xml.etree.cElementTree as ET
......
Supports Markdown
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