Commit f02299b6 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[webapi] Add custom exception to generate common message

parent 6ec7fddc
......@@ -40,6 +40,14 @@ import simplejson as json
from urllib.parse import urlparse
class WebAPIError(RuntimeError):
def __init__(self, cmd, url, answer):
message = "{cmd} error {status}: {url}\nError:{text}".format(
cmd=cmd, status=answer.status_code, url=url, text=answer.text
)
super().__init__(message)
class WebAPI(object):
"""Central class for all remote service related calls"""
......@@ -82,7 +90,7 @@ class WebAPI(object):
# if answer.status_code < 200 or answer.status_code >= 300:
if answer.status_code not in [200, 204]:
raise RuntimeError("GET error %i : %s" % (answer.status_code, url))
raise WebAPIError("GET", url, answer)
return answer.json()
......@@ -91,7 +99,7 @@ class WebAPI(object):
answer = requests.post(url, json=data, headers=self._make_headers())
if answer.status_code not in [200, 201]:
raise RuntimeError("POST error %i : %s" % (answer.status_code, url))
raise WebAPIError("POST", url, answer)
try:
return answer.json()
......@@ -103,7 +111,7 @@ class WebAPI(object):
answer = requests.put(url, json=data, headers=self._make_headers())
if answer.status_code not in [200, 204]:
raise RuntimeError("PUT error %i : %s" % (answer.status_code, url))
raise WebAPIError("PUT", url, answer)
try:
return answer.json()
......@@ -116,4 +124,4 @@ class WebAPI(object):
# Should respond a 204 status and an empty body
if answer.status_code not in [200, 204]:
raise RuntimeError("DELETE error %i : %s" % (answer.status_code, url))
raise WebAPIError("DELETE", url, answer)
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