handlers_utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. import tornado
  18. def expected_http_error(error, expected_code, expected_message=None):
  19. """Check that the error matches the expected output error."""
  20. e = error.value
  21. if isinstance(e, tornado.web.HTTPError):
  22. if expected_code != e.status_code:
  23. return False
  24. if expected_message is not None and expected_message != str(e):
  25. return False
  26. return True
  27. elif any(
  28. [
  29. isinstance(e, tornado.httpclient.HTTPClientError),
  30. isinstance(e, tornado.httpclient.HTTPError),
  31. ]
  32. ):
  33. if expected_code != e.code:
  34. return False
  35. if expected_message:
  36. message = json.loads(e.response.body.decode())["message"]
  37. if expected_message != message:
  38. return False
  39. return True