summaryrefslogtreecommitdiff
path: root/scripts/make_errors.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/make_errors.py')
-rwxr-xr-xscripts/make_errors.py103
1 files changed, 6 insertions, 97 deletions
diff --git a/scripts/make_errors.py b/scripts/make_errors.py
index 6a59cf1a..8e5f4f38 100755
--- a/scripts/make_errors.py
+++ b/scripts/make_errors.py
@@ -17,6 +17,7 @@ The script can be run at a new PostgreSQL release to refresh the module.
# License for more details.
from __future__ import print_function
+import os
import re
import sys
import urllib2
@@ -24,34 +25,19 @@ from collections import defaultdict
def main():
- if len(sys.argv) != 2:
- print("usage: %s /path/to/errors.py" % sys.argv[0], file=sys.stderr)
- return 2
+ filename = os.path.join(
+ os.path.dirname(__file__), "../psycopg/sqlstate_errors.h")
- filename = sys.argv[1]
-
- file_start = read_base_file(filename)
# If you add a version to the list fix the docs (in errors.rst)
classes, errors = fetch_errors(
['9.1', '9.2', '9.3', '9.4', '9.5', '9.6', '10', '11'])
f = open(filename, "w")
- for line in file_start:
- print(line, file=f)
+ print("/*\n * Autogenerated by 'scripts/make_errors.py'.\n */\n", file=f)
for line in generate_module_data(classes, errors):
print(line, file=f)
-def read_base_file(filename):
- rv = []
- for line in open(filename):
- rv.append(line.rstrip("\n"))
- if line.startswith("# autogenerated"):
- return rv
-
- raise ValueError("can't find the separator. Is this the right file?")
-
-
def parse_errors_txt(url):
classes = {}
errors = defaultdict(dict)
@@ -112,12 +98,7 @@ def fetch_errors(versions):
def generate_module_data(classes, errors):
- tmpl = """
-
-@for_sqlstate(%(errcode)r)
-class %(cls)s(%(base)s):
- pass\
-"""
+ tmpl = '{"%(errcode)s", "%(cls)s"},'
specific = {
'38002': 'ModifyingSqlDataNotPermittedExt',
'38003': 'ProhibitedSqlStatementAttemptedExt',
@@ -137,7 +118,7 @@ class %(cls)s(%(base)s):
# success and warning - never raised
continue
- yield "\n\n# %s" % clslabel
+ yield "\n/* %s */" % clslabel
for errcode, errlabel in sorted(errors[clscode].items()):
if errcode in specific:
@@ -150,81 +131,9 @@ class %(cls)s(%(base)s):
yield tmpl % {
'cls': clsname,
- 'base': get_base_class_name(errcode),
'errcode': errcode
}
-def get_base_class_name(errcode):
- """
- This is a python porting of exception_from_sqlstate code in pqpath.c
- """
- if errcode[0] == '0':
- if errcode[1] == 'A': # Class 0A - Feature Not Supported
- return 'NotSupportedError'
- elif errcode[0] == '2':
- if errcode[1] in '01':
- # Class 20 - Case Not Found
- # Class 21 - Cardinality Violation
- return 'ProgrammingError'
- elif errcode[1] == '2': # Class 22 - Data Exception
- return 'DataError'
- elif errcode[1] == '3': # Class 23 - Integrity Constraint Violation
- return 'IntegrityError'
- elif errcode[1] in '45':
- # Class 24 - Invalid Cursor State
- # Class 25 - Invalid Transaction State
- return 'InternalError'
- elif errcode[1] in '678':
- # Class 26 - Invalid SQL Statement Name
- # Class 27 - Triggered Data Change Violation
- # Class 28 - Invalid Authorization Specification
- return 'OperationalError'
- elif errcode[1] in 'BDF':
- # Class 2B - Dependent Privilege Descriptors Still Exist
- # Class 2D - Invalid Transaction Termination
- # Class 2F - SQL Routine Exception
- return 'InternalError'
- elif errcode[0] == '3':
- if errcode[1] == '4': # Class 34 - Invalid Cursor Name
- return 'OperationalError'
- if errcode[1] in '89B':
- # Class 38 - External Routine Exception
- # Class 39 - External Routine Invocation Exception
- # Class 3B - Savepoint Exception
- return 'InternalError'
- if errcode[1] in 'DF':
- # Class 3D - Invalid Catalog Name
- # Class 3F - Invalid Schema Name
- return 'ProgrammingError'
- elif errcode[0] == '4':
- if errcode[1] == '0': # Class 40 - Transaction Rollback
- return 'TransactionRollbackError'
- if errcode[1] in '24':
- # Class 42 - Syntax Error or Access Rule Violation
- # Class 44 - WITH CHECK OPTION Violation
- return 'ProgrammingError'
- elif errcode[0] == '5':
- if errcode == "57014":
- return 'QueryCanceledError'
- # Class 53 - Insufficient Resources
- # Class 54 - Program Limit Exceeded
- # Class 55 - Object Not In Prerequisite State
- # Class 57 - Operator Intervention
- # Class 58 - System Error (errors external to PostgreSQL itself)
- else:
- return 'OperationalError'
- elif errcode[0] == 'F': # Class F0 - Configuration File Error
- return 'InternalError'
- elif errcode[0] == 'H': # Class HV - Foreign Data Wrapper Error (SQL/MED)
- return 'OperationalError'
- elif errcode[0] == 'P': # Class P0 - PL/pgSQL Error
- return 'InternalError'
- elif errcode[0] == 'X': # Class XX - Internal Error
- return 'InternalError'
-
- return 'DatabaseError'
-
-
if __name__ == '__main__':
sys.exit(main())