-
Notifications
You must be signed in to change notification settings - Fork 126
Capturing SQL Exceptions #8
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
Comments
It's possible to use nginx's "error_page" to override the default 500/411 exceptions. For more complicated cases, please check out the ngx_lua module: http://github.com/chaoslawful/lua-nginx-module Basically you can issue a subrequest from within your Lua code to an internal nginx location configured by ngx_postgres, and then process the subrequest responses (like sql exceptions) the way you like in Lua ;) -agentzh |
If you want to pass error message returned from PostgreSQL server, then no, it's not possible right now. I'll add this to the |
Just following up if you have any suggested patch paths I could take in the the source tree. My use case is failures for columns that are unique and detecting the error message on the consumer end (eg., username, email fields). |
I think Piotr Sikora is working on $postgres_error_code and $postgres_error_message variables for this. Basically you can output these nginx variables via ngx_headers_more's more_set_headers, and capture these headers in Lua by inspecting the return values of ngx.location.capture or ngx.location.capture_multi in the context of ngx_lua :) |
What @agentzh meant is that I have this on my imminent (current or next week) I want to add |
The $postgres_error and $postgres_error_oneline would definitely be useful and powerful. I do like your idea about having errors printed in the response body as I suspect a lot of users would love that feature including myself. I've been using your module for www.geobacon.com and it's been rock solid. |
I wonder if it's feasible to encode such error code and error messages into the RDS output? |
@agentzh: error code & message are already present in the RDS header. |
@PiotrSikora Nice :) |
@docyes when PiotrSikora implemented the $postgres_error varialbe, you can append the error message to your main response body by using echo_after_body: http://wiki.nginx.org/HttpEchoModule#echo_after_body |
@agentzh: I thought about using |
@docyes alternatively you can use ngx_lua + Lua + lua-rds-parser to generate a nice error page all by yourself now: see http://wiki.nginx.org/HttpLuaModule and https://github.com/agentzh/lua-rds-parser |
On Tue, Sep 13, 2011 at 5:28 PM, Piotr Sikora
That's a very good question :) We can do something like this as long
That is, appending empty string (in $footer) is a no-op for echo_after_body. -agentzh |
Actually, it wasn't very good question :P
|
Thanks for looking into this you two, this is really valuable. From my perspective I am really liking the idea from @agetzh Having by default the appropriate http error status code along with RDS output of the error message would be sweet. |
@docyes: you must have missed part of the dialog. Error code & message are (and always been) present in the RDS output. |
Hey guys, can you confirm this is the recommended pattern. Does the $postgres_error variable exist in the current release?
|
I see the rds_json is turned on. It cannot display errstr message on your side directly? If not, maybe we should make it work rather than rely on the echo_after_body hack :) |
This is what i am seeing in my error.log file: |
I'm not sure how difficult it would be to add the postgresql condition names? This would be a huge feature allowing developers to key of the condition value in their own internal lookups. Here's a list of all the condition names: |
It's not really "recommended", but yes, that represents the idea that we were talking about.
No, it's not. Also, it seems that you're confused by what The
Would it? Could you give me code snippet that would represent how use of this feature would look like?
We're bailing-out on errors with |
@PiotrSikora import json import simplerequest response = simplrequest.post("/db/users", {'username': 'foobar'}) data = json.loads(response.text) if data.get('error'): if data.get('code')=='unique_violation': return 'sorry that unsername already exists' else: return 'sorry we can't create users right now' else: return 'your user account has been created' |
@docyes: Oh, I thought that you were talking about nginx.conf-side conditionals. This is definitely doable and it makes a lot of sense (other than the fact that you really should use Sadly, my "open-source time" is very limited lately, so I cannot give you any ETA on this... But don't expect it before November/December. |
@PiotrSikora |
On Thu, Sep 15, 2011 at 2:15 AM, Piotr Sikora
@PiotrSikora: Should we fix both ngx_postgres and ngx_drizzle such Best, |
@agentzh: yes, we should... Eventually. Just keep it mind that it shouldn't be enabled by default, at least not the verbose output, since it could leak potentially "secret" information. |
@PiotrSikora: |
What's the status of this? I'm using ngx_postgres for a project and it would be nice to see errors in my application. I'm using Postgres 9.2. Whenever there is an error, my query location returns 500 error with no body. |
Hello! On Tue, Nov 20, 2012 at 1:44 PM, leaf [email protected] wrote:
I have a local patch for this and it mostly works. I'll try to polish Thanks for reminding me of this :) Best regards, |
@agentzh I was wondering the current behavior is. I have a use case where Postgres is correctly returning a duplicate key error, and ngx_postgres is returning a 500 back to the client. I'd like to be able to intercept this and return something in the range of 4xx to indicate to the client that the actual request data was wrong, as opposed to a server error. If the actual duplicate key message could get passed back to the client, that would be nice as well, as opposed to having it land in the openresty error log. Is there a way to achieve this? |
I've since written a pure Lua Postgres driver for OpenResty that you might find useful: https://github.com/leafo/pgmoon |
@leafo Thanks for the heads up - do you have an example of where this is used in OpenResty to customize the error handling? |
If the |
@agentzh could you share your patch with $postgres_error var? |
Is possible to return error message as http header ?! |
@niquola AFAIK, the patch for ngx_postgres does not exist yet :) |
@agentzh could you give me a guidelines or similar examples - i'll try to do it by-myself I'm mostly clojure, ruby guy (: |
@niquola After the first build, you don't need to rerun ./configure for incremental changes in build/* (including ngx_postgres source there). Just run "make -j8 && sudo make install" and the build system will just pick up what has changed so it should be very fast. (If you have fewer available CPU logical cores than 8, try reducing that number in the -j option for "make"). Regarding exposing the Pg error message to an nginx variable, it should be an easy exercise for nginx C development. Just check out existing nginx C code for setting nginx variable values. |
Thx, I understand "this is 5 min task for you" - could you drop me a link
|
@niquola You can check out how ngx_postgres implements the |
I've created pull-request - waiting for review. |
Does a mechanism exist for capturing SQL exceptions and returning the error message in a http response?
The text was updated successfully, but these errors were encountered: