Skip to content

Commit

Permalink
handling errors with status + body
Browse files Browse the repository at this point in the history
  • Loading branch information
costero-e committed Jun 10, 2024
1 parent baf0075 commit 999dda7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
45 changes: 30 additions & 15 deletions beacon/request/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ async def wrapper(request: Request):
except Exception as err:
qparams=''
if str(err) == 'Not Found':
response = build_beacon_error_response(404, qparams, str(err))
error = build_beacon_error_response(404, qparams, str('error'))
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Request':
response = build_beacon_error_response(400, qparams, str(err)+':'+str(err.text))
error = build_beacon_error_response(400, qparams, str('error'))
raise web.HTTPBadRequest(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Gateway':
response = build_beacon_error_response(502, qparams, str(err))
error = build_beacon_error_response(502, qparams, str('error'))
raise web.HTTPBadGateway(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Method Not Allowed':
response = build_beacon_error_response(405, qparams, str(err))
error = build_beacon_error_response(405, qparams, str('error'))
raise web.HTTPMethodNotAllowed(text=json.dumps(error), content_type='application/json')
else:
response = build_beacon_error_response(500, qparams, str(err))
error = build_beacon_error_response(500, qparams, str('error'))
raise web.HTTPInternalServerError(text=json.dumps(error), content_type='application/json')
return await json_stream(request, response)

return wrapper
Expand Down Expand Up @@ -209,15 +214,20 @@ async def wrapper(request: Request):
except Exception as err:
qparams = ''
if str(err) == 'Not Found':
response = build_beacon_error_response(404, qparams, str(err))
error = build_beacon_error_response(404, qparams, str('error'))
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Request':
response = build_beacon_error_response(400, qparams, str(err)+':'+str(err.text))
error = build_beacon_error_response(400, qparams, str('error'))
raise web.HTTPBadRequest(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Gateway':
response = build_beacon_error_response(502, qparams, str(err))
error = build_beacon_error_response(502, qparams, str('error'))
raise web.HTTPBadGateway(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Method Not Allowed':
response = build_beacon_error_response(405, qparams, str(err))
error = build_beacon_error_response(405, qparams, str('error'))
raise web.HTTPMethodNotAllowed(text=json.dumps(error), content_type='application/json')
else:
response = build_beacon_error_response(500, qparams, str(err))
error = build_beacon_error_response(500, qparams, str('error'))
raise web.HTTPInternalServerError(text=json.dumps(error), content_type='application/json')

return await json_stream(request, response)

Expand Down Expand Up @@ -317,15 +327,20 @@ async def wrapper(request: Request):
except Exception as err:
qparams = ''
if str(err) == 'Not Found':
response = build_beacon_error_response(404, qparams, str(err))
error = build_beacon_error_response(404, qparams, str('error'))
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Request':
build_beacon_error_response(400, qparams, str(err)+':'+str(err.text))
error = build_beacon_error_response(400, qparams, str('error'))
raise web.HTTPBadRequest(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Gateway':
response = build_beacon_error_response(502, qparams, str(err))
error = build_beacon_error_response(502, qparams, str('error'))
raise web.HTTPBadGateway(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Method Not Allowed':
response = build_beacon_error_response(405, qparams, str(err))
error = build_beacon_error_response(405, qparams, str('error'))
raise web.HTTPMethodNotAllowed(text=json.dumps(error), content_type='application/json')
else:
response = build_beacon_error_response(500, qparams, str(err))
error = build_beacon_error_response(500, qparams, str('error'))
raise web.HTTPInternalServerError(text=json.dumps(error), content_type='application/json')

return await json_stream(request, response)

Expand Down
10 changes: 8 additions & 2 deletions beacon/response/error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import subprocess
import logging
import json
import aiohttp
from aiohttp import web
from aiohttp.web_request import Request
from beacon.db.datasets import get_datasets
from beacon.request import RequestParams
Expand All @@ -14,8 +16,12 @@
async def handler(request: Request):
LOG.error('Running an error request')

LOG.error(request.url)

# Fetch datasets info
qparams = ''

error = build_beacon_error_response(404, qparams, str('error'))


response_converted= build_beacon_error_response(404, qparams, 'Not Found')
return await json_stream(request, response_converted)
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
16 changes: 11 additions & 5 deletions beacon/response/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from beacon.utils.auth import resolve_token
from beacon.utils.stream import json_stream
from bson import json_util
from aiohttp import web

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,13 +71,18 @@ async def handler(request: Request):
except Exception as err:
qparams = ''
if str(err) == 'Not Found':
response_converted = build_beacon_error_response(404, qparams, str(err))
error = build_beacon_error_response(404, qparams, str('error'))
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Request':
response_converted = build_beacon_error_response(400, qparams, str(err)+':'+str(err.text))
error = build_beacon_error_response(400, qparams, str('error'))
raise web.HTTPBadRequest(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Gateway':
response_converted = build_beacon_error_response(502, qparams, str(err))
error = build_beacon_error_response(502, qparams, str('error'))
raise web.HTTPBadGateway(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Method Not Allowed':
response_converted = build_beacon_error_response(405, qparams, str(err))
error = build_beacon_error_response(405, qparams, str('error'))
raise web.HTTPMethodNotAllowed(text=json.dumps(error), content_type='application/json')
else:
response_converted = build_beacon_error_response(500, qparams, str(err))
error = build_beacon_error_response(500, qparams, str('error'))
raise web.HTTPInternalServerError(text=json.dumps(error), content_type='application/json')
return await json_stream(request, response_converted)
17 changes: 14 additions & 3 deletions beacon/response/service_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from aiohttp.web_request import Request
from beacon.response.build_response import build_beacon_service_info_response, build_beacon_error_response
from beacon.utils.stream import json_stream
from aiohttp import web
import json

LOG = logging.getLogger(__name__)

Expand All @@ -12,9 +14,18 @@ async def handler(request: Request):
except Exception as err:
qparams = ''
if str(err) == 'Not Found':
response_converted = build_beacon_error_response(404, qparams, str(err))
error = build_beacon_error_response(404, qparams, str('error'))
raise web.HTTPNotFound(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Request':
response_converted = build_beacon_error_response(400, qparams, str(err))
error = build_beacon_error_response(400, qparams, str('error'))
raise web.HTTPBadRequest(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Bad Gateway':
error = build_beacon_error_response(502, qparams, str('error'))
raise web.HTTPBadGateway(text=json.dumps(error), content_type='application/json')
elif str(err) == 'Method Not Allowed':
error = build_beacon_error_response(405, qparams, str('error'))
raise web.HTTPMethodNotAllowed(text=json.dumps(error), content_type='application/json')
else:
response_converted = build_beacon_error_response(500, qparams, str(err))
error = build_beacon_error_response(500, qparams, str('error'))
raise web.HTTPInternalServerError(text=json.dumps(error), content_type='application/json')
return await json_stream(request, response_converted)

0 comments on commit 999dda7

Please sign in to comment.