[enh] add error codes

This commit is contained in:
Laurent Peuch 2019-01-19 08:08:36 +01:00
parent ae4da946d6
commit f124bd02b7

View file

@ -19,18 +19,27 @@ async def check_http(request):
logger.info(f"Unvalid json in request, body is : {request.body}") logger.info(f"Unvalid json in request, body is : {request.body}")
return json_response({ return json_response({
"status": "error", "status": "error",
"content": "InvalidUsage, body isn't proper json" "code": "error_bad_json",
"content": "InvalidUsage, body isn't proper json",
}) })
if not data or "domain" not in data: if not data or "domain" not in data:
logger.info(f"Unvalid request didn't specified a domain (body is : {request.body}") logger.info(f"Unvalid request didn't specified a domain (body is : {request.body}")
return json_response({"status": "error", "content": "request must specify a domain"}) return json_response({
"status": "error",
"code": "error_no_domain",
"content": "request must specify a domain",
})
domain = data["domain"] domain = data["domain"]
if not validators.domain(domain): if not validators.domain(domain):
logger.info(f"Invalid request, is not in the right format (domain is : {domain})") logger.info(f"Invalid request, is not in the right format (domain is : {domain})")
return json_response({"status": "error", "content": "domain is not in the right format (do not include http:// or https://)"}) return json_response({
"status": "error",
"code": "error_domain_bad_format",
"content": "domain is not in the right format (do not include http:// or https://)",
})
# TODO DNS check # TODO DNS check
@ -43,12 +52,20 @@ async def check_http(request):
logger.info(f"Success when checking http access for {domain} asked by {ip}") logger.info(f"Success when checking http access for {domain} asked by {ip}")
# TODO various kind of errors # TODO various kind of errors
except aiohttp.client_exceptions.ClientConnectorError: except aiohttp.client_exceptions.ClientConnectorError:
return json_response({"status": "error", "content": "connection error, could not connect to the requested domain, it's very likely unreachable"}) return json_response({
"status": "error",
"code": "error_http_check_connection_error",
"content": "connection error, could not connect to the requested domain, it's very likely unreachable",
})
except Exception: except Exception:
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return json_response({"status": "error", "content": "an error happen while trying to get your domain, it's very likely unreachable"}) return json_response({
"status": "error",
"code": "error_http_check_unknown_error",
"content": "an error happen while trying to get your domain, it's very likely unreachable",
})
# [x] - get ip # [x] - get ip
# [x] - get request json # [x] - get request json
@ -60,6 +77,8 @@ async def check_http(request):
# [x] - ADD TIMEOUT # [x] - ADD TIMEOUT
# [x] - try/catch, if everything is ok → response ok # [x] - try/catch, if everything is ok → response ok
# [x] - otherwise reponse with exception # [x] - otherwise reponse with exception
# [x] - create error codes
# [ ] - rate limit
return json_response({"status": "ok"}) return json_response({"status": "ok"})