diff --git a/src/ngx_postgres_module.c b/src/ngx_postgres_module.c index 365de007..f3f5a357 100644 --- a/src/ngx_postgres_module.c +++ b/src/ngx_postgres_module.c @@ -141,6 +141,10 @@ static ngx_http_variable_t ngx_postgres_module_variables[] = { ngx_postgres_variable_query, 0, NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 }, + { ngx_string("postgres_error"), NULL, + ngx_postgres_variable_error, 0, + NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 }, + { ngx_null_string, NULL, NULL, 0, 0, 0 } }; diff --git a/src/ngx_postgres_module.h b/src/ngx_postgres_module.h index 03f49fdb..b0f24b92 100644 --- a/src/ngx_postgres_module.h +++ b/src/ngx_postgres_module.h @@ -169,6 +169,7 @@ typedef struct { ngx_int_t var_rows; ngx_int_t var_affected; ngx_str_t var_query; + ngx_str_t var_error; ngx_array_t *variables; ngx_int_t status; } ngx_postgres_ctx_t; diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index d25c0541..efffa307 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -290,6 +290,9 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, ExecStatusType pgrc; PGresult *res; ngx_int_t rc; + ngx_postgres_ctx_t *pgctx; + u_char *errormsg; + u_char *errorvar; dd("entering"); @@ -331,10 +334,20 @@ ngx_postgres_upstream_get_result(ngx_http_request_t *r, ngx_connection_t *pgxc, pgrc = PQresultStatus(res); if ((pgrc != PGRES_COMMAND_OK) && (pgrc != PGRES_TUPLES_OK)) { dd("receiving result failed"); + + pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); + + errormsg = PQerrorMessage(pgdt->pgconn); + ngx_log_error(NGX_LOG_ERR, pgxc->log, 0, "postgres: failed to receive result: %s: %s", PQresStatus(pgrc), - PQerrorMessage(pgdt->pgconn)); + errormsg); + + errorvar = ngx_palloc(r->pool, strlen(errormsg)); + strcpy(errorvar, errormsg); + pgctx->var_error.len = strlen(errorvar); + pgctx->var_error.data = errorvar; PQclear(res); diff --git a/src/ngx_postgres_variable.c b/src/ngx_postgres_variable.c index e7f4f381..c8fb34c0 100644 --- a/src/ngx_postgres_variable.c +++ b/src/ngx_postgres_variable.c @@ -152,6 +152,32 @@ ngx_postgres_variable_query(ngx_http_request_t *r, return NGX_OK; } +ngx_int_t +ngx_postgres_variable_error(ngx_http_request_t *r, + ngx_http_variable_value_t *v, uintptr_t data) +{ + ngx_postgres_ctx_t *pgctx; + + dd("entering: \"$postgres_error\""); + + pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); + + if ((pgctx == NULL) || (pgctx->var_error.len == 0)) { + v->not_found = 1; + dd("returning NGX_OK (not_found)"); + return NGX_OK; + } + + v->valid = 1; + v->no_cacheable = 0; + v->not_found = 0; + v->len = pgctx->var_error.len; + v->data = pgctx->var_error.data; + + dd("returning NGX_OK"); + return NGX_OK; +} + ngx_int_t ngx_postgres_variable_get_custom(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data) diff --git a/src/ngx_postgres_variable.h b/src/ngx_postgres_variable.h index abdb1996..07f5c8a5 100644 --- a/src/ngx_postgres_variable.h +++ b/src/ngx_postgres_variable.h @@ -42,6 +42,8 @@ ngx_int_t ngx_postgres_variable_affected(ngx_http_request_t *, ngx_http_variable_value_t *, uintptr_t); ngx_int_t ngx_postgres_variable_query(ngx_http_request_t *, ngx_http_variable_value_t *, uintptr_t); +ngx_int_t ngx_postgres_variable_error(ngx_http_request_t *, + ngx_http_variable_value_t *, uintptr_t); ngx_int_t ngx_postgres_variable_get_custom(ngx_http_request_t *, ngx_http_variable_value_t *, uintptr_t); ngx_str_t ngx_postgres_variable_set_custom(ngx_http_request_t *r,