MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _handle_dbapi_exception

Method _handle_dbapi_exception

lib/sqlalchemy/engine/base.py:2201–2345  ·  view source on GitHub ↗
(
        self,
        e: BaseException,
        statement: Optional[str],
        parameters: Optional[_AnyExecuteParams],
        cursor: Optional[DBAPICursor],
        context: Optional[ExecutionContext],
        is_sub_exec: bool = False,
    )

Source from the content-addressed store, hash-verified

2199 _is_disconnect = False
2200
2201 def _handle_dbapi_exception(
2202 self,
2203 e: BaseException,
2204 statement: Optional[str],
2205 parameters: Optional[_AnyExecuteParams],
2206 cursor: Optional[DBAPICursor],
2207 context: Optional[ExecutionContext],
2208 is_sub_exec: bool = False,
2209 ) -> NoReturn:
2210 exc_info = sys.exc_info()
2211
2212 is_exit_exception = util.is_exit_exception(e)
2213
2214 if not self._is_disconnect:
2215 self._is_disconnect = (
2216 isinstance(e, self.dialect.loaded_dbapi.Error)
2217 and not self.closed
2218 and self.dialect.is_disconnect(
2219 e,
2220 self._dbapi_connection if not self.invalidated else None,
2221 cursor,
2222 )
2223 ) or (is_exit_exception and not self.closed)
2224
2225 invalidate_pool_on_disconnect = not is_exit_exception
2226
2227 ismulti: bool = (
2228 not is_sub_exec and context.executemany
2229 if context is not None
2230 else False
2231 )
2232 if self._reentrant_error:
2233 raise exc.DBAPIError.instance(
2234 statement,
2235 parameters,
2236 e,
2237 self.dialect.loaded_dbapi.Error,
2238 hide_parameters=self.engine.hide_parameters,
2239 dialect=self.dialect,
2240 ismulti=ismulti,
2241 ).with_traceback(exc_info[2]) from e
2242 self._reentrant_error = True
2243 try:
2244 # non-DBAPI error - if we already got a context,
2245 # or there's no string statement, don't wrap it
2246 should_wrap = isinstance(e, self.dialect.loaded_dbapi.Error) or (
2247 not isinstance(e, exc.StatementError)
2248 and statement is not None
2249 and context is None
2250 and not is_exit_exception
2251 )
2252
2253 if should_wrap:
2254 sqlalchemy_exception = exc.DBAPIError.instance(
2255 statement,
2256 parameters,
2257 cast(Exception, e),
2258 self.dialect.loaded_dbapi.Error,

Callers 15

connectionMethod · 0.95
get_isolation_levelMethod · 0.95
_begin_implMethod · 0.95
_rollback_implMethod · 0.95
_commit_implMethod · 0.95
_begin_twophase_implMethod · 0.95
_commit_twophase_implMethod · 0.95
_execute_defaultMethod · 0.95
_execute_contextMethod · 0.95
_exec_single_contextMethod · 0.95

Calls 11

_safe_close_cursorMethod · 0.95
in_transactionMethod · 0.95
_rollback_implMethod · 0.95
invalidateMethod · 0.95
instanceMethod · 0.80
_invalidateMethod · 0.80
castFunction · 0.50
is_disconnectMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected