-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathException.c
More file actions
257 lines (215 loc) · 7.35 KB
/
Exception.c
File metadata and controls
257 lines (215 loc) · 7.35 KB
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
/*
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the The BSD 3-Clause License
* which accompanies this distribution, and is available at
* http://opensource.org/licenses/BSD-3-Clause
*
* Contributors:
* Tada AB
* Chapman Flack
*
* @author Thomas Hallgren
*/
#include <postgres.h>
#include <executor/spi.h>
#include "pljava/Backend.h"
#include "pljava/Invocation.h"
#include "pljava/Exception.h"
#include "pljava/type/String.h"
#include "pljava/type/ErrorData.h"
jclass Class_class;
jmethodID Class_getName;
jmethodID Class_getCanonicalName;
jclass ServerException_class;
jmethodID ServerException_getErrorData;
jmethodID ServerException_obtain;
jclass Throwable_class;
jmethodID Throwable_getMessage;
jmethodID Throwable_printStackTrace;
static jclass UnhandledPGException_class;
static jmethodID UnhandledPGException_obtain;
jclass IllegalArgumentException_class;
jmethodID IllegalArgumentException_init;
jclass SQLException_class;
jmethodID SQLException_init;
jmethodID SQLException_getSQLState;
jclass UnsupportedOperationException_class;
jmethodID UnsupportedOperationException_init;
jclass NoSuchFieldError_class;
jclass NoSuchMethodError_class;
bool Exception_isPGUnhandled(jthrowable ex)
{
return JNI_isInstanceOf(ex, UnhandledPGException_class);
}
void
Exception_featureNotSupported(const char* requestedFeature, const char* introVersion)
{
jstring jmsg;
jobject ex;
StringInfoData buf;
initStringInfo(&buf);
PG_TRY();
{
appendStringInfoString(&buf, "Feature: ");
appendStringInfoString(&buf, requestedFeature);
appendStringInfoString(&buf, " lacks support in PostgreSQL version ");
appendStringInfo(&buf, "%d.%d",
PG_VERSION_NUM / 10000,
#if PG_VERSION_NUM >= 100000
(PG_VERSION_NUM) % 10000
#else
(PG_VERSION_NUM / 100) % 100
#endif
);
appendStringInfoString(&buf, ". It was introduced in version ");
appendStringInfoString(&buf, introVersion);
ereport(DEBUG3, (errmsg("%s", buf.data)));
jmsg = String_createJavaStringFromNTS(buf.data);
ex = JNI_newObject(UnsupportedOperationException_class, UnsupportedOperationException_init, jmsg);
JNI_deleteLocalRef(jmsg);
JNI_throw(ex);
}
PG_CATCH();
{
ereport(WARNING, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Exception while generating exception: %s", buf.data)));
}
PG_END_TRY();
pfree(buf.data);
}
void Exception_throw(int errCode, const char* errMessage, ...)
{
char buf[1024];
va_list args;
jstring message;
jstring sqlState;
jobject ex;
int idx;
va_start(args, errMessage);
vsnprintf(buf, sizeof(buf), errMessage, args);
ereport(DEBUG3, (errcode(errCode), errmsg("%s", buf)));
PG_TRY();
{
message = String_createJavaStringFromNTS(buf);
/* unpack MAKE_SQLSTATE code
*/
for (idx = 0; idx < 5; ++idx)
{
buf[idx] = (char)PGUNSIXBIT(errCode);
errCode >>= 6;
}
buf[idx] = 0;
sqlState = String_createJavaStringFromNTS(buf);
ex = JNI_newObject(SQLException_class, SQLException_init, message, sqlState);
JNI_deleteLocalRef(message);
JNI_deleteLocalRef(sqlState);
JNI_throw(ex);
}
PG_CATCH();
{
ereport(WARNING, (errcode(errCode), errmsg("Exception while generating exception: %s", buf)));
}
PG_END_TRY();
va_end(args);
}
void Exception_throwIllegalArgument(const char* errMessage, ...)
{
char buf[1024];
va_list args;
jstring message;
jobject ex;
va_start(args, errMessage);
vsnprintf(buf, sizeof(buf), errMessage, args);
ereport(DEBUG3, (errmsg("%s", buf)));
PG_TRY();
{
message = String_createJavaStringFromNTS(buf);
ex = JNI_newObject(IllegalArgumentException_class, IllegalArgumentException_init, message);
JNI_deleteLocalRef(message);
JNI_throw(ex);
}
PG_CATCH();
{
ereport(WARNING, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("Exception while generating exception: %s", buf)));
}
PG_END_TRY();
va_end(args);
}
void Exception_throwSPI(const char* function, int errCode)
{
Exception_throw(ERRCODE_INTERNAL_ERROR,
"SPI function SPI_%s failed with error %s", function,
SPI_result_code_string(errCode));
}
void Exception_throw_unhandled()
{
jobject ex;
PG_TRY();
{
ex = JNI_callStaticObjectMethodLocked(
UnhandledPGException_class, UnhandledPGException_obtain);
JNI_throw(ex);
}
PG_CATCH();
{
elog(WARNING, "Exception while generating exception");
}
PG_END_TRY();
}
void Exception_throw_ERROR(const char* funcName)
{
jobject ex;
PG_TRY();
{
jobject ed = pljava_ErrorData_getCurrentError();
FlushErrorState();
ex = JNI_callStaticObjectMethodLocked(
ServerException_class, ServerException_obtain, ed);
currentInvocation->errorOccurred = true;
elog(DEBUG2, "Exception in function %s", funcName);
JNI_deleteLocalRef(ed);
JNI_throw(ex);
}
PG_CATCH();
{
elog(WARNING, "Exception while generating exception");
}
PG_END_TRY();
}
extern void Exception_initialize(void);
void Exception_initialize(void)
{
Class_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/Class"));
Throwable_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/Throwable"));
Throwable_getMessage = PgObject_getJavaMethod(Throwable_class, "getMessage", "()Ljava/lang/String;");
Throwable_printStackTrace = PgObject_getJavaMethod(Throwable_class, "printStackTrace", "()V");
IllegalArgumentException_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/IllegalArgumentException"));
IllegalArgumentException_init = PgObject_getJavaMethod(IllegalArgumentException_class, "<init>", "(Ljava/lang/String;)V");
SQLException_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/sql/SQLException"));
SQLException_init = PgObject_getJavaMethod(SQLException_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");
SQLException_getSQLState = PgObject_getJavaMethod(SQLException_class, "getSQLState", "()Ljava/lang/String;");
UnsupportedOperationException_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/UnsupportedOperationException"));
UnsupportedOperationException_init = PgObject_getJavaMethod(UnsupportedOperationException_class, "<init>", "(Ljava/lang/String;)V");
NoSuchFieldError_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/NoSuchFieldError"));
NoSuchMethodError_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("java/lang/NoSuchMethodError"));
Class_getName = PgObject_getJavaMethod(Class_class, "getName", "()Ljava/lang/String;");
Class_getCanonicalName = PgObject_getJavaMethod(Class_class,
"getCanonicalName", "()Ljava/lang/String;");
}
extern void Exception_initialize2(void);
void Exception_initialize2(void)
{
ServerException_class = (jclass)JNI_newGlobalRef(PgObject_getJavaClass("org/postgresql/pljava/internal/ServerException"));
ServerException_obtain = PgObject_getStaticJavaMethod(
ServerException_class, "obtain",
"(Lorg/postgresql/pljava/internal/ErrorData;)"
"Lorg/postgresql/pljava/internal/ServerException;");
ServerException_getErrorData = PgObject_getJavaMethod(ServerException_class, "getErrorData", "()Lorg/postgresql/pljava/internal/ErrorData;");
UnhandledPGException_class = (jclass)JNI_newGlobalRef(
PgObject_getJavaClass(
"org/postgresql/pljava/internal/UnhandledPGException"));
UnhandledPGException_obtain = PgObject_getStaticJavaMethod(
UnhandledPGException_class, "obtain",
"()Lorg/postgresql/pljava/internal/UnhandledPGException;");
}