-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy patherrors.c
155 lines (141 loc) · 4.12 KB
/
errors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-------------------------------------------------------------------------
*
* The Multicorn Foreign Data Wrapper allows you to fetch foreign data in
* Python in your PostgreSQL server.
*
* This module contains error handling functions.
*
* This software is released under the postgresql licence
*
*-------------------------------------------------------------------------
*/
#include "multicorn.h"
#include "bytesobject.h"
#include "access/xact.h"
void reportException(PyObject *pErrType,
PyObject *pErrValue,
PyObject *pErrTraceback);
void reportMulticornException(PyObject *pErrValue);
PGDLLEXPORT void
errorCheck()
{
PyObject *pErrType,
*pErrValue,
*pErrTraceback;
PyErr_Fetch(&pErrType, &pErrValue, &pErrTraceback);
if (pErrType)
{
// if the error value has a property _is_multicorn_exception and a boolean value True, then we don't report the
// error as a generic exception with a stack trace -- instead we just take the message, code(severity), hint,
// and detail, and log it to Postgres. These exceptions are generated in utils.py to intercept ERROR/FATAL log
// messages. So, first detect whether that's the case, and call a new reporting function...
PyObject *is_multicorn_exception = PyObject_GetAttrString(pErrValue, "_is_multicorn_exception");
// clear AttributeError possibly raised by PyObject_GetAttrString
PyErr_Clear();
if (is_multicorn_exception != NULL && PyObject_IsTrue(is_multicorn_exception))
{
Py_DECREF(is_multicorn_exception);
Py_DECREF(pErrType);
Py_DECREF(pErrTraceback);
reportMulticornException(pErrValue);
}
else
{
reportException(pErrType, pErrValue, pErrTraceback);
}
}
}
void
reportException(PyObject *pErrType, PyObject *pErrValue, PyObject *pErrTraceback)
{
char *errName,
*errValue,
*errTraceback = "";
PyObject *traceback_list;
PyObject *pTemp;
PyObject *tracebackModule = PyImport_ImportModule("traceback");
PyObject *format_exception = PyObject_GetAttrString(tracebackModule, "format_exception");
PyObject *newline = PyString_FromString("\n");
int severity;
PyErr_NormalizeException(&pErrType, &pErrValue, &pErrTraceback);
pTemp = PyObject_GetAttrString(pErrType, "__name__");
errName = PyString_AsString(pTemp);
errValue = PyString_AsString(PyObject_Str(pErrValue));
if (pErrTraceback != NULL)
{
traceback_list = PyObject_CallFunction(format_exception, "(O,O,O)", pErrType, pErrValue, pErrTraceback);
errTraceback = PyString_AsString(PyObject_CallMethod(newline, "join", "(O)", traceback_list));
Py_DECREF(pErrTraceback);
Py_DECREF(traceback_list);
}
if (IsAbortedTransactionBlockState())
{
severity = WARNING;
}
else
{
severity = ERROR;
}
if (errstart(severity, TEXTDOMAIN))
{
if (errstart(severity, TEXTDOMAIN))
errmsg("Error in python: %s", errName);
errdetail("%s", errValue);
errdetail_log("%s", errTraceback);
}
Py_DECREF(pErrType);
Py_DECREF(pErrValue);
Py_DECREF(format_exception);
Py_DECREF(tracebackModule);
Py_DECREF(newline);
Py_DECREF(pTemp);
errfinish(__FILE__, __LINE__, PG_FUNCNAME_MACRO);
}
void reportMulticornException(PyObject* pErrValue)
{
int severity;
PyObject *message = PyObject_GetAttrString(pErrValue, "message");
PyObject *hint = PyObject_GetAttrString(pErrValue, "hint");
PyObject *detail = PyObject_GetAttrString(pErrValue, "detail");
PyObject *code = PyObject_GetAttrString(pErrValue, "code");
int level = PyLong_AsLong(code);
// Matches up with REPORT_CODES in utils.py
switch (level)
{
case 3:
severity = ERROR;
break;
default:
case 4:
severity = FATAL;
break;
}
PG_TRY();
{
if (errstart(severity, TEXTDOMAIN))
{
errmsg("%s", PyString_AsString(message));
if (hint != NULL && hint != Py_None)
{
char* hintstr = PyString_AsString(hint);
errhint("%s", hintstr);
}
if (detail != NULL && detail != Py_None)
{
char* detailstr = PyString_AsString(detail);
errdetail("%s", detailstr);
}
errfinish(__FILE__, __LINE__, PG_FUNCNAME_MACRO);
}
}
PG_CATCH();
{
Py_DECREF(message);
Py_DECREF(hint);
Py_DECREF(detail);
Py_DECREF(code);
Py_DECREF(pErrValue);
PG_RE_THROW();
}
PG_END_TRY();
}