Skip to content

Commit cc6d339

Browse files
committed
add $postgres_error variable
1 parent f9479fb commit cc6d339

5 files changed

+47
-1
lines changed

src/ngx_postgres_module.c

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ static ngx_http_variable_t ngx_postgres_module_variables[] = {
141141
ngx_postgres_variable_query, 0,
142142
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
143143

144+
{ ngx_string("postgres_error"), NULL,
145+
ngx_postgres_variable_error, 0,
146+
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
147+
144148
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
145149
};
146150

src/ngx_postgres_module.h

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ typedef struct {
169169
ngx_int_t var_rows;
170170
ngx_int_t var_affected;
171171
ngx_str_t var_query;
172+
ngx_str_t var_error;
172173
ngx_array_t *variables;
173174
ngx_int_t status;
174175
} ngx_postgres_ctx_t;

src/ngx_postgres_processor.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
290290
ExecStatusType pgrc;
291291
PGresult *res;
292292
ngx_int_t rc;
293+
ngx_postgres_ctx_t *pgctx;
294+
u_char *errormsg;
295+
u_char *errorvar;
293296

294297
dd("entering");
295298

@@ -331,10 +334,20 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc,
331334
pgrc = PQresultStatus(res);
332335
if ((pgrc != PGRES_COMMAND_OK) && (pgrc != PGRES_TUPLES_OK)) {
333336
dd("receiving result failed");
337+
338+
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
339+
340+
errormsg = PQerrorMessage(pgdt->pgconn);
341+
334342
ngx_log_error(NGX_LOG_ERR, pgxc->log, 0,
335343
"postgres: failed to receive result: %s: %s",
336344
PQresStatus(pgrc),
337-
PQerrorMessage(pgdt->pgconn));
345+
errormsg);
346+
347+
errorvar = ngx_palloc(r->pool, strlen(errormsg));
348+
strcpy(errorvar, errormsg);
349+
pgctx->var_error.len = strlen(errorvar);
350+
pgctx->var_error.data = errorvar;
338351

339352
PQclear(res);
340353

src/ngx_postgres_variable.c

+26
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ ngx_postgres_variable_query(ngx_http_request_t *r,
152152
return NGX_OK;
153153
}
154154

155+
ngx_int_t
156+
ngx_postgres_variable_error(ngx_http_request_t *r,
157+
ngx_http_variable_value_t *v, uintptr_t data)
158+
{
159+
ngx_postgres_ctx_t *pgctx;
160+
161+
dd("entering: \"$postgres_error\"");
162+
163+
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
164+
165+
if ((pgctx == NULL) || (pgctx->var_error.len == 0)) {
166+
v->not_found = 1;
167+
dd("returning NGX_OK (not_found)");
168+
return NGX_OK;
169+
}
170+
171+
v->valid = 1;
172+
v->no_cacheable = 0;
173+
v->not_found = 0;
174+
v->len = pgctx->var_error.len;
175+
v->data = pgctx->var_error.data;
176+
177+
dd("returning NGX_OK");
178+
return NGX_OK;
179+
}
180+
155181
ngx_int_t
156182
ngx_postgres_variable_get_custom(ngx_http_request_t *r,
157183
ngx_http_variable_value_t *v, uintptr_t data)

src/ngx_postgres_variable.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ngx_int_t ngx_postgres_variable_affected(ngx_http_request_t *,
4242
ngx_http_variable_value_t *, uintptr_t);
4343
ngx_int_t ngx_postgres_variable_query(ngx_http_request_t *,
4444
ngx_http_variable_value_t *, uintptr_t);
45+
ngx_int_t ngx_postgres_variable_error(ngx_http_request_t *,
46+
ngx_http_variable_value_t *, uintptr_t);
4547
ngx_int_t ngx_postgres_variable_get_custom(ngx_http_request_t *,
4648
ngx_http_variable_value_t *, uintptr_t);
4749
ngx_str_t ngx_postgres_variable_set_custom(ngx_http_request_t *r,

0 commit comments

Comments
 (0)