Skip to content

add $postgres_error variable #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ngx_postgres_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};

Expand Down
1 change: 1 addition & 0 deletions src/ngx_postgres_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion src/ngx_postgres_processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);

Expand Down
26 changes: 26 additions & 0 deletions src/ngx_postgres_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_postgres_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down