external_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import re
  2. import sys
  3. from flask import current_app, got_request_exception
  4. from flask_restful import Api, http_status_message
  5. from werkzeug.datastructures import Headers
  6. from werkzeug.exceptions import HTTPException
  7. from core.errors.error import AppInvokeQuotaExceededError
  8. class ExternalApi(Api):
  9. def handle_error(self, e):
  10. """Error handler for the API transforms a raised exception into a Flask
  11. response, with the appropriate HTTP status code and body.
  12. :param e: the raised Exception object
  13. :type e: Exception
  14. """
  15. got_request_exception.send(current_app, exception=e)
  16. headers = Headers()
  17. if isinstance(e, HTTPException):
  18. if e.response is not None:
  19. resp = e.get_response()
  20. return resp
  21. status_code = e.code
  22. default_data = {
  23. 'code': re.sub(r'(?<!^)(?=[A-Z])', '_', type(e).__name__).lower(),
  24. 'message': getattr(e, 'description', http_status_message(status_code)),
  25. 'status': status_code
  26. }
  27. if default_data['message'] and default_data['message'] == 'Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)':
  28. default_data['message'] = 'Invalid JSON payload received or JSON payload is empty.'
  29. headers = e.get_response().headers
  30. elif isinstance(e, ValueError):
  31. status_code = 400
  32. default_data = {
  33. 'code': 'invalid_param',
  34. 'message': str(e),
  35. 'status': status_code
  36. }
  37. elif isinstance(e, AppInvokeQuotaExceededError):
  38. status_code = 429
  39. default_data = {
  40. 'code': 'too_many_requests',
  41. 'message': str(e),
  42. 'status': status_code
  43. }
  44. else:
  45. status_code = 500
  46. default_data = {
  47. 'message': http_status_message(status_code),
  48. }
  49. # Werkzeug exceptions generate a content-length header which is added
  50. # to the response in addition to the actual content-length header
  51. # https://github.com/flask-restful/flask-restful/issues/534
  52. remove_headers = ('Content-Length',)
  53. for header in remove_headers:
  54. headers.pop(header, None)
  55. data = getattr(e, 'data', default_data)
  56. error_cls_name = type(e).__name__
  57. if error_cls_name in self.errors:
  58. custom_data = self.errors.get(error_cls_name, {})
  59. custom_data = custom_data.copy()
  60. status_code = custom_data.get('status', 500)
  61. if 'message' in custom_data:
  62. custom_data['message'] = custom_data['message'].format(
  63. message=str(e.description if hasattr(e, 'description') else e)
  64. )
  65. data.update(custom_data)
  66. # record the exception in the logs when we have a server error of status code: 500
  67. if status_code and status_code >= 500:
  68. exc_info = sys.exc_info()
  69. if exc_info[1] is None:
  70. exc_info = None
  71. current_app.log_exception(exc_info)
  72. if status_code == 406 and self.default_mediatype is None:
  73. # if we are handling NotAcceptable (406), make sure that
  74. # make_response uses a representation we support as the
  75. # default mediatype (so that make_response doesn't throw
  76. # another NotAcceptable error).
  77. supported_mediatypes = list(self.representations.keys()) # only supported application/json
  78. fallback_mediatype = supported_mediatypes[0] if supported_mediatypes else "text/plain"
  79. data = {
  80. 'code': 'not_acceptable',
  81. 'message': data.get('message')
  82. }
  83. resp = self.make_response(
  84. data,
  85. status_code,
  86. headers,
  87. fallback_mediatype = fallback_mediatype
  88. )
  89. elif status_code == 400:
  90. if isinstance(data.get('message'), dict):
  91. param_key, param_value = list(data.get('message').items())[0]
  92. data = {
  93. 'code': 'invalid_param',
  94. 'message': param_value,
  95. 'params': param_key
  96. }
  97. else:
  98. if 'code' not in data:
  99. data['code'] = 'unknown'
  100. resp = self.make_response(data, status_code, headers)
  101. else:
  102. if 'code' not in data:
  103. data['code'] = 'unknown'
  104. resp = self.make_response(data, status_code, headers)
  105. if status_code == 401:
  106. resp = self.unauthorized(resp)
  107. return resp