You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

error_handler.py 2.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Handle custom error."""
  16. from urllib.parse import quote
  17. from werkzeug.exceptions import NotFound
  18. from werkzeug.exceptions import MethodNotAllowed
  19. from flask import request, jsonify
  20. from mindinsight.datavisual.common.exceptions import RequestMethodNotAllowed
  21. from mindinsight.datavisual.common.exceptions import RestfulApiNotExist
  22. from mindinsight.datavisual.common.log import restful_logger as logger
  23. from mindinsight.utils.exceptions import UnknownError
  24. from mindinsight.utils.exceptions import FileSystemPermissionError
  25. def handle_http_exception_error(ex):
  26. """Handle http exception error."""
  27. logger.warning('%r %r, detail: %r', request.method, quote(request.path), str(ex))
  28. if isinstance(ex, NotFound):
  29. error = RestfulApiNotExist()
  30. elif isinstance(ex, MethodNotAllowed):
  31. error = RequestMethodNotAllowed()
  32. else:
  33. logger.exception(ex)
  34. error = UnknownError('System error or http error.')
  35. res_body = {"error_code": error.error_code, "error_msg": error.message}
  36. return jsonify(res_body), error.http_code
  37. def handle_mindinsight_error(ex):
  38. """Handle mindinsight error."""
  39. if int(ex.http_code) < 500:
  40. logger.warning('%r %r detail: %r', request.method, quote(request.path), ex.message)
  41. else:
  42. logger.error('%r %r detail: %r', request.method, quote(request.path), ex.message)
  43. logger.exception(ex)
  44. res_body = dict(error_code=ex.error_code, error_msg=ex.message)
  45. return jsonify(res_body), ex.http_code
  46. def handle_unknown_error(ex):
  47. """Handle unknown error."""
  48. logger.error('%r %r detail: %r', request.method, quote(request.path), str(ex))
  49. logger.exception(ex)
  50. if isinstance(ex, PermissionError):
  51. error = FileSystemPermissionError('File System Permission Error')
  52. else:
  53. error = UnknownError('System error.')
  54. res_body = dict(error_code=error.error_code, error_msg=error.message)
  55. return jsonify(res_body), error.http_code