_exceptions.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (c) "Neo4j"
  2. # Neo4j Sweden AB [https://neo4j.com]
  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. # https://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. """Internal module for all internal exception classes."""
  16. from os import strerror
  17. class BoltError(Exception):
  18. """Base class for all Bolt protocol errors."""
  19. def __init__(self, message, address):
  20. super().__init__(message)
  21. self.address = address
  22. class BoltConnectionError(BoltError):
  23. """Raised when a connection fails."""
  24. def __init__(self, message, address):
  25. msg = (
  26. "Connection Failed. "
  27. "Please ensure that your database is listening on the correct "
  28. "host and port and that you have enabled encryption if required. "
  29. "Note that the default encryption setting has changed in Neo4j "
  30. f"4.0. See the docs for more information. {message}"
  31. )
  32. super().__init__(msg, address)
  33. def __str__(self):
  34. s = super().__str__()
  35. errno = self.errno
  36. if errno:
  37. s += f" (code {errno}: {strerror(errno)})"
  38. return s
  39. @property
  40. def errno(self):
  41. try:
  42. return self.__cause__.errno
  43. except AttributeError:
  44. return None
  45. class BoltSecurityError(BoltConnectionError):
  46. """Raised when a connection fails for security reasons."""
  47. def __str__(self):
  48. return f"[{self.__cause__.__class__.__name__}] {super().__str__()}"
  49. class BoltHandshakeError(BoltError):
  50. """Raised when a handshake completes unsuccessfully."""
  51. def __init__(self, message, address, request_data, response_data):
  52. super().__init__(message, address)
  53. self.request_data = request_data
  54. self.response_data = response_data
  55. class BoltProtocolError(BoltError):
  56. """Raised when an unexpected or unsupported protocol event occurs."""
  57. class SocketDeadlineExceededError(RuntimeError):
  58. """Raised from sockets with deadlines when a timeout occurs."""