Base exception for all the errors psycopg will raise. Exception that is the base class of all other error exceptions. You can use this to catch all errors with one single `!except` statement. This exception is guaranteed to be picklable.
| 253 | |
| 254 | |
| 255 | class Error(Exception): |
| 256 | """ |
| 257 | Base exception for all the errors psycopg will raise. |
| 258 | |
| 259 | Exception that is the base class of all other error exceptions. You can |
| 260 | use this to catch all errors with one single `!except` statement. |
| 261 | |
| 262 | This exception is guaranteed to be picklable. |
| 263 | """ |
| 264 | |
| 265 | __module__ = "psycopg" |
| 266 | |
| 267 | sqlstate: str | None = None |
| 268 | |
| 269 | def __init__( |
| 270 | self, |
| 271 | *args: Sequence[Any], |
| 272 | info: ErrorInfo = None, |
| 273 | encoding: str = "utf-8", |
| 274 | pgconn: PGconn | None = None, |
| 275 | ): |
| 276 | super().__init__(*args) |
| 277 | self._info = info |
| 278 | self._encoding = encoding |
| 279 | self._pgconn = pgconn |
| 280 | |
| 281 | # Handle sqlstate codes for which we don't have a class. |
| 282 | if not self.sqlstate and info: |
| 283 | self.sqlstate = self.diag.sqlstate |
| 284 | |
| 285 | @property |
| 286 | def pgconn(self) -> PGconn | None: |
| 287 | """The connection object, if the error was raised from a connection attempt. |
| 288 | |
| 289 | :rtype: psycopg.pq.PGconn | None |
| 290 | """ |
| 291 | return self._pgconn if self._pgconn else None |
| 292 | |
| 293 | @property |
| 294 | def pgresult(self) -> PGresult | None: |
| 295 | """The result object, if the exception was raised after a failed query. |
| 296 | |
| 297 | :rtype: psycopg.pq.PGresult | None |
| 298 | """ |
| 299 | return self._info if _is_pgresult(self._info) else None |
| 300 | |
| 301 | @property |
| 302 | def diag(self) -> Diagnostic: |
| 303 | """ |
| 304 | A `Diagnostic` object to inspect details of the errors from the database. |
| 305 | """ |
| 306 | return Diagnostic(self._info, encoding=self._encoding) |
| 307 | |
| 308 | def __reduce__(self) -> str | tuple[Any, ...]: |
| 309 | res = super().__reduce__() |
| 310 | if isinstance(res, tuple) and len(res) >= 3: |
| 311 | # To make the exception picklable |
| 312 | res[2]["_info"] = _info_to_dict(self._info) |