~james-w/ubuntu/lucid/psycopg2/precise-backport

« back to all changes in this revision

Viewing changes to doc/src/errorcodes.rst

  • Committer: Mikhail Turov
  • Date: 2010-07-28 20:30:01 UTC
  • mfrom: (5.1.9 sid)
  • Revision ID: groldster@gmail.com-20100728203001-u1dv46tnd3s02ejg
Tags: 2.2.1-1ubuntu1
releasing version 2.2.1-1ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
`psycopg2.errorcodes` -- Error codes defined by PostgreSQL
 
2
===============================================================
 
3
 
 
4
.. sectionauthor:: Daniele Varrazzo <daniele.varrazzo@gmail.com>
 
5
 
 
6
.. index::
 
7
    single: Error; Codes
 
8
 
 
9
.. module:: psycopg2.errorcodes
 
10
 
 
11
.. testsetup:: *
 
12
 
 
13
    from psycopg2 import errorcodes
 
14
 
 
15
.. versionadded:: 2.0.6
 
16
 
 
17
This module contains symbolic names for all PostgreSQL error codes and error
 
18
classes codes.  Subclasses of `~psycopg2.Error` make the PostgreSQL error
 
19
code available in the `~psycopg2.Error.pgcode` attribute.
 
20
 
 
21
From PostgreSQL documentation:
 
22
 
 
23
    All messages emitted by the PostgreSQL server are assigned five-character
 
24
    error codes that follow the SQL standard's conventions for :sql:`SQLSTATE`
 
25
    codes.  Applications that need to know which error condition has occurred
 
26
    should usually test the error code, rather than looking at the textual
 
27
    error message.  The error codes are less likely to change across
 
28
    PostgreSQL releases, and also are not subject to change due to
 
29
    localization of error messages. Note that some, but not all, of the error
 
30
    codes produced by PostgreSQL are defined by the SQL standard; some
 
31
    additional error codes for conditions not defined by the standard have
 
32
    been invented or borrowed from other databases.
 
33
 
 
34
    According to the standard, the first two characters of an error code
 
35
    denote a class of errors, while the last three characters indicate a
 
36
    specific condition within that class. Thus, an application that does not
 
37
    recognize the specific error code can still be able to infer what to do
 
38
    from the error class.
 
39
 
 
40
.. seealso:: `PostgreSQL Error Codes table`__
 
41
 
 
42
    .. __: http://www.postgresql.org/docs/8.4/static/errcodes-appendix.html#ERRCODES-TABLE
 
43
 
 
44
 
 
45
An example of the available constants defined in the module:
 
46
 
 
47
    >>> errorcodes.CLASS_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION
 
48
    '42'
 
49
    >>> errorcodes.UNDEFINED_TABLE
 
50
    '42P01'
 
51
 
 
52
Constants representing all the error values documented by PostgreSQL versions
 
53
between 8.1 and 8.4 are included in the module.
 
54
 
 
55
 
 
56
.. autofunction:: lookup(code)
 
57
 
 
58
    .. doctest::
 
59
 
 
60
        >>> try:
 
61
        ...     cur.execute("SELECT ouch FROM aargh;")
 
62
        ... except Exception, e:
 
63
        ...     pass
 
64
        ...
 
65
        >>> errorcodes.lookup(e.pgcode[:2])
 
66
        'CLASS_SYNTAX_ERROR_OR_ACCESS_RULE_VIOLATION'
 
67
        >>> errorcodes.lookup(e.pgcode)
 
68
        'UNDEFINED_TABLE'
 
69
 
 
70
    .. versionadded:: 2.0.14
 
71
 
 
72
 
 
73
.. testcode::
 
74
    :hide:
 
75
 
 
76
    conn.rollback()