4949from neo4j .conf import Config , PoolConfig
5050from neo4j .io ._bolt3 import Outbox , BufferedSocket , Inbox , Response , InitResponse , CommitResponse
5151from neo4j .errors import BoltRoutingError , Neo4jAvailabilityError
52- from neo4j .exceptions import ProtocolError , SecurityError , \
53- ServiceUnavailable , AuthError , IncompleteCommitError , \
54- ConnectionExpired , DatabaseUnavailableError , NotALeaderError , \
55- ForbiddenOnReadOnlyDatabaseError , ClientError
52+ from neo4j .exceptions import (
53+ ProtocolError ,
54+ SecurityError ,
55+ ServiceUnavailable ,
56+ AuthError ,
57+ IncompleteCommitError ,
58+ ConnectionExpired ,
59+ DatabaseUnavailableError ,
60+ NotALeaderError ,
61+ ForbiddenOnReadOnlyDatabaseError ,
62+ ClientError ,
63+ SessionExpired ,
64+ TransactionError ,
65+ )
5666from neo4j .meta import get_user_agent
5767from neo4j .packstream import Packer , Unpacker
5868from neo4j .routing import RoutingTable
5969
60-
6170# Set up logger
6271log = getLogger ("neo4j" )
6372
@@ -89,10 +98,6 @@ class Bolt:
8998 #: The pool of which this connection is a member
9099 pool = None
91100
92- #: Error class used for raising connection errors
93- # TODO: separate errors for connector API
94- Error = ServiceUnavailable
95-
96101 @classmethod
97102 def ping (cls , address , * , timeout = None , ** config ):
98103 """ Attempt to establish a Bolt connection, returning the
@@ -305,13 +310,13 @@ def send_all(self):
305310 """ Send all queued messages to the server.
306311 """
307312 if self .closed ():
308- raise self . Error ("Failed to write to closed connection "
309- "{!r} ({!r})" . format ( self .unresolved_address ,
310- self . server . address ))
313+ raise ServiceUnavailable ("Failed to write to closed connection {!r} ({!r})" . format (
314+ self .unresolved_address , self . server . address ))
315+
311316 if self .defunct ():
312- raise self . Error ("Failed to write to defunct connection "
313- "{!r} ({!r})" . format ( self .unresolved_address ,
314- self . server . address ))
317+ raise ServiceUnavailable ("Failed to write to defunct connection {!r} ({!r})" . format (
318+ self .unresolved_address , self . server . address ))
319+
315320 try :
316321 self ._send_all ()
317322 except (IOError , OSError ) as error :
@@ -331,13 +336,13 @@ def fetch_message(self):
331336 messages fetched
332337 """
333338 if self ._closed :
334- raise self . Error ("Failed to read from closed connection "
335- "{!r} ({!r})" . format ( self .unresolved_address ,
336- self . server . address ))
339+ raise ServiceUnavailable ("Failed to read from closed connection {!r} ({!r})" . format (
340+ self .unresolved_address , self . server . address ))
341+
337342 if self ._defunct :
338- raise self . Error ("Failed to read from defunct connection "
339- "{!r} ({!r})" . format ( self .unresolved_address ,
340- self . server . address ))
343+ raise ServiceUnavailable ("Failed to read from defunct connection {!r} ({!r})" . format (
344+ self .unresolved_address , self . server . address ))
345+
341346 if not self .responses :
342347 return 0 , 0
343348
@@ -388,9 +393,11 @@ def fetch_message(self):
388393 return len (details ), 1
389394
390395 def _set_defunct (self , error = None ):
391- message = ("Failed to read from defunct connection "
392- "{!r} ({!r})" .format (self .unresolved_address ,
393- self .server .address ))
396+ direct_driver = isinstance (self .pool , BoltPool )
397+
398+ message = ("Failed to read from defunct connection {!r} ({!r})" .format (
399+ self .unresolved_address , self .server .address ))
400+
394401 log .error (message )
395402 # We were attempting to receive data but the connection
396403 # has unexpectedly terminated. So, we need to close the
@@ -406,7 +413,11 @@ def _set_defunct(self, error=None):
406413 for response in self .responses :
407414 if isinstance (response , CommitResponse ):
408415 raise IncompleteCommitError (message )
409- raise self .Error (message )
416+
417+ if direct_driver :
418+ raise ServiceUnavailable (message )
419+ else :
420+ raise SessionExpired (message )
410421
411422 def timedout (self ):
412423 return 0 <= self ._max_connection_lifetime <= perf_counter () - self ._creation_timestamp
@@ -846,11 +857,9 @@ def acquire(self, access_mode=None, timeout=None):
846857 try :
847858 address = self ._select_address (access_mode )
848859 except Neo4jAvailabilityError as err :
849- raise ConnectionExpired ("Failed to obtain connection "
850- "towards '%s' server." % access_mode ) from err
860+ raise SessionExpired ("Failed to obtain connection towards '%s' server." % access_mode ) from err
851861 try :
852862 connection = self ._acquire (address , timeout = timeout ) # should always be a resolved address
853- connection .Error = ConnectionExpired
854863 except ServiceUnavailable :
855864 self .deactivate (address )
856865 else :
0 commit comments