_errors.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) Microsoft Corporation.
  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. # These are types that we use in the API. They are public and are a part of the
  15. # stable API.
  16. from typing import Optional
  17. def is_target_closed_error(error: Exception) -> bool:
  18. return isinstance(error, TargetClosedError)
  19. class Error(Exception):
  20. def __init__(self, message: str) -> None:
  21. self._message = message
  22. self._name: Optional[str] = None
  23. self._stack: Optional[str] = None
  24. super().__init__(message)
  25. @property
  26. def message(self) -> str:
  27. return self._message
  28. @property
  29. def name(self) -> Optional[str]:
  30. return self._name
  31. @property
  32. def stack(self) -> Optional[str]:
  33. return self._stack
  34. class TimeoutError(Error):
  35. pass
  36. class TargetClosedError(Error):
  37. def __init__(self, message: str = None) -> None:
  38. super().__init__(message or "Target page, context or browser has been closed")
  39. def rewrite_error(error: Exception, message: str) -> Exception:
  40. rewritten_exc = type(error)(message)
  41. if isinstance(rewritten_exc, Error) and isinstance(error, Error):
  42. rewritten_exc._name = error.name
  43. rewritten_exc._stack = error.stack
  44. return rewritten_exc