Better Logging + Error handling

This commit is contained in:
Oleg Silkin 2019-07-20 09:10:14 -04:00
parent 7ddc18086d
commit ee26f3e913
2 changed files with 13 additions and 10 deletions

View file

@ -14,6 +14,7 @@ from src.database import obtain_connection
from src.database import delete_comment_by_id from src.database import delete_comment_by_id
from src.writes import create_comment_or_error from src.writes import create_comment_or_error
from src.misc import is_authentic_delete_signal from src.misc import is_authentic_delete_signal
from src.misc import make_error
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -101,7 +102,7 @@ async def process_json(app, body: dict) -> dict:
else: else:
response['error'] = make_error('INTERNAL', err) response['error'] = make_error('INTERNAL', err)
else: else:
response['error'] = ERRORS['METHOD_NOT_FOUND'] response['error'] = make_error('METHOD_NOT_FOUND')
return response return response
@ -117,15 +118,6 @@ async def api_endpoint(request: web.Request):
) )
else: else:
return web.json_response(await process_json(request.app, body)) return web.json_response(await process_json(request.app, body))
else:
logger.warning('Got invalid request from %s: %s', request.remote, body)
return web.json_response({'error': ERRORS['INVALID_REQUEST']})
except json.decoder.JSONDecodeError as jde:
logger.exception('Received malformed JSON from %s: %s', request.remote, jde.msg)
logger.debug('Request headers: %s', request.headers)
return web.json_response({
'error': ERRORS['PARSE_ERROR']
})
except Exception as e: except Exception as e:
logger.exception(f'Exception raised by request from {request.remote}: {e}') logger.exception(f'Exception raised by request from {request.remote}: {e}')
logger.debug(f'Request headers: {request.headers}') logger.debug(f'Request headers: {request.headers}')

View file

@ -31,6 +31,17 @@ ERRORS = {
} }
def make_error(error, exc: Exception = None) -> dict:
body = ERRORS[error] if error in ERRORS else ERRORS['INTERNAL']
try:
if exc:
body.update({
type(exc).__name__: str(exc)
})
finally:
return body
def channel_matches_pattern(channel_id: str, channel_name: str): def channel_matches_pattern(channel_id: str, channel_name: str):
assert channel_id and channel_name assert channel_id and channel_name
assert type(channel_id) is str and type(channel_name) is str assert type(channel_id) is str and type(channel_name) is str