forked from pllua/pllua-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pllua_errors.c
302 lines (260 loc) · 8.44 KB
/
pllua_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "pllua_errors.h"
#include "plluacommon.h"
extern LVMInfo lvm_info[2];
static const char error_type_name[] = "pllua_error";
int luaB_assert (lua_State *L) {
luaL_checkany(L, 1);
if (!lua_toboolean(L, 1))
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
return lua_gettop(L);
}
int luaB_error (lua_State *L) {
#if LUA_VERSION_NUM >= 503
int level = luaL_optinteger(L, 2, 1);
#else
int level = luaL_optint(L, 2, 1);
#endif
lua_settop(L, 1);
if (lua_isnoneornil(L, 1)){
if (lua_isnil(L, 1)){
lua_pop(L, 1);
}
if (level >0){
luaL_where(L, level);
lua_pushstring(L, "no exception data");
lua_concat(L, 2);
}else{
lua_pushstring(L, "no exception data");
}
}else
// if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
// luaL_where(L, level);
// lua_pushvalue(L, 1);
// lua_concat(L, 2);
// } else
if (lua_istable(L,1)){
set_error_mt(L);
}
return lua_error(L);
}
int luaL_error_skip_where (lua_State *L, const char *fmt, ...) {
va_list argp;
va_start(argp, fmt);
/*luaL_where(L, 1);*/
lua_pushvfstring(L, fmt, argp);
va_end(argp);
/*lua_concat(L, 2);*/
return lua_error(L);
}
static int error_tostring(lua_State *L)
{
lua_pushstring(L, "message");
lua_rawget(L, -2);
return 1;
}
static luaL_Reg regs[] =
{
{"__tostring", error_tostring},
{ NULL, NULL }
};
void register_error_mt(lua_State *L)
{
lua_newtable(L);
lua_pushlightuserdata(L, (void *) error_type_name);
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
luaP_register(L, regs);
lua_pop(L, 1);
}
void set_error_mt(lua_State *L)
{
luaP_getfield(L, error_type_name);
lua_setmetatable(L, -2);
}
void push_spi_error(lua_State *L, MemoryContext oldcontext)
{
ErrorData *edata;
/* Save error info */
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
lua_newtable(L);
if (edata->message){
lua_pushstring(L, edata->message); //"no exception data"
lua_setfield(L, -2, "message");
} else {
lua_pushstring(L, "no exception data");
lua_setfield(L, -2, "message");
}
if (edata->detail){
lua_pushstring(L, edata->detail);
lua_setfield(L, -2, "detail");
}
if (edata->context){
lua_pushstring(L, edata->context);
lua_setfield(L, -2, "context");
}
if (edata->hint){
lua_pushstring(L, edata->hint);
lua_setfield(L, -2, "hint");
}
if (edata->sqlerrcode){
lua_pushinteger(L, edata->sqlerrcode);
lua_setfield(L, -2, "sqlerrcode");
}
set_error_mt(L);
FreeErrorData(edata);
}
static void pllua_parse_error(lua_State *L, ErrorData *edata){
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (lua_type(L, -2) == LUA_TSTRING){
const char *key = lua_tostring(L, -2);
if (lua_type(L, -1) == LUA_TSTRING){
if (strcmp(key, "message") == 0){
edata->message = pstrdup( lua_tostring(L, -1) );
} else if (strcmp(key, "detail") == 0){
edata->detail = pstrdup( lua_tostring(L, -1) );
} else if (strcmp(key, "hint") == 0){
edata->hint = pstrdup( lua_tostring(L, -1) );
} else if (strcmp(key, "context") == 0){
edata->context = pstrdup( lua_tostring(L, -1) );
}
}else if (lua_type(L, -1) == LUA_TNUMBER){
if (strcmp(key, "sqlerrcode") == 0){
edata->sqlerrcode = (int)( lua_tonumber(L, -1) );
}
}
}
lua_pop(L, 1);
}
}
void luatable_topgerror(lua_State *L)
{
luatable_report(L, ERROR);
}
void luatable_report(lua_State *L, int elevel)
{
ErrorData edata;
char *query = NULL;
int position = 0;
edata.message = NULL;
edata.sqlerrcode = 0;
edata.detail = NULL;
edata.hint = NULL;
edata.context = NULL;
pllua_parse_error(L, &edata);
lua_pop(L, lua_gettop(L));
elevel = Min(elevel, ERROR);
ereport(elevel,
(errcode(edata.sqlerrcode ? edata.sqlerrcode : ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg_internal("%s", edata.message ? edata.message : "no exception data"),
(edata.detail) ? errdetail_internal("%s", edata.detail) : 0,
(edata.context) ? errcontext("%s", edata.context) : 0,
(edata.hint) ? errhint("%s", edata.hint) : 0,
(query) ? internalerrquery(query) : 0,
(position) ? internalerrposition(position) : 0));
}
/* this is a copy from lua source to call debug.traceback without access it from metatables */
#define LEVELS1 12 /* size of the first part of the stack */
#define LEVELS2 10 /* size of the second part of the stack */
static lua_State *getthread (lua_State *L, int *arg) {
if (lua_isthread(L, 1)) {
*arg = 1;
return lua_tothread(L, 1);
}
else {
*arg = 0;
return L;
}
}
static int db_errorfb (lua_State *L) {
int level;
int firstpart = 1; /* still before eventual `...' */
int arg;
lua_State *L1 = getthread(L, &arg);
lua_Debug ar;
luaL_Buffer b;
if (lua_isnumber(L, arg+2)) {
level = (int)lua_tointeger(L, arg+2);
lua_pop(L, 1);
}
else
level = (L == L1) ? 1 : 0; /* level 0 may be this own function */
if (lua_gettop(L) == arg)
lua_pushliteral(L, "");
else if (!lua_isstring(L, arg+1)) return 1; /* message is not a string */
else lua_pushliteral(L, "\n");
luaL_buffinit(L, &b);
luaL_addstring(&b, "stack traceback(");
luaL_addstring(&b, lvm_info[pllua_getmaster_index(L)].name);
luaL_addstring(&b, "):");
luaL_pushresult(&b);
//lua_pushliteral(L, "stack traceback:");
while (lua_getstack(L1, level++, &ar)) {
if (level > LEVELS1 && firstpart) {
/* no more than `LEVELS2' more levels? */
if (!lua_getstack(L1, level+LEVELS2, &ar))
level--; /* keep going */
else {
lua_pushliteral(L, "\n\t..."); /* too many levels */
while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
level++;
}
firstpart = 0;
continue;
}
lua_pushliteral(L, "\n\t");
lua_getinfo(L1, "Snl", &ar);
lua_pushfstring(L, "%s:", ar.short_src);
if (ar.currentline > 0)
lua_pushfstring(L, "%d:", ar.currentline);
if (*ar.namewhat != '\0') /* is there a name? */
lua_pushfstring(L, " in function " LUA_QS, ar.name);
else {
if (*ar.what == 'm') /* main? */
lua_pushfstring(L, " in main chunk");
else if (*ar.what == 'C' || *ar.what == 't')
lua_pushliteral(L, " ?"); /* C function or tail call */
else
lua_pushfstring(L, " in function <%s:%d>",
ar.short_src, ar.linedefined);
}
lua_concat(L, lua_gettop(L) - arg);
}
lua_concat(L, lua_gettop(L) - arg);
return 1;
}
int traceback (lua_State *L) {
int stateIndex = pllua_getmaster_index(L);
if (lvm_info[stateIndex].hasTraceback)
return 1;
if (lua_isstring(L, 1)){ /* 'message' not a string? */
lua_newtable(L);
lua_pushcfunction(L, db_errorfb);
lua_pushstring(L,""); /* empty concat string for context */
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
lvm_info[stateIndex].hasTraceback = true;
lua_setfield(L, -2, "context");
lua_swap(L); /*text <-> table*/
lua_setfield(L, -2, "message");
set_error_mt(L);
return 1;
}else if (lua_istable(L,1)){
lua_pushstring(L,"context");
lua_rawget(L, -2);
if (!lua_isstring(L, -1)){
lua_pop(L,1);
lua_pushstring(L,""); /* empty concat string for context */
}
lua_pushcfunction(L, db_errorfb);
lua_swap(L);
lua_pushinteger(L, 2); /* skip this function and traceback */
lua_call(L, 2, 1); /* call debug.traceback */
lvm_info[stateIndex].hasTraceback = true;
lua_setfield(L, -2, "context");
return 1;
}
return 1; /* keep it intact */
}