6 ms·
I'm pretty sure I've posted that in every pg 9.3 thread to date, but for application developers don't miss all the new diagnostic fields on PGResult[0]: you can
by masklinn 13y ago
I'm pretty sure I've posted that in every pg 9.3 thread to date, but for application developers don't miss all the new diagnostic fields on PGResult[0]: you can now get (when applicable on the error) the relevant schema, table, column, column type and constraint[1] in order to translate error information back into your application domain and generate logging and error messages which make sense to developers & end users.
It's already available in psycopg2 (since 2.5)[2] and ruby-pg (in 0.16)[3], it may be available in other adapters (outside of libpq obviously).
[0] http://www.postgresql.org/docs/9.3/static/libpq-exec.html#LIBPQ-PQRESULTERRORFIELD http://www.postgresql.org/docs/9.3/static/libpq-exec.html#LI...
[1] without having to perform string munging on localized error messages, which probably doesn't include half of them
[2] http://psycopg.lighthouseapp.com/projects/62710/tickets/149 http://psycopg.lighthouseapp.com/projects/62710/tickets/149
[3] https://bitbucket.org/ged/ruby-pg/issue/161/add-support-for-pg_diag_-_name-error https://bitbucket.org/ged/ruby-pg/issue/161/add-support-for-...
- kingkilr 13y agodiag stuff is also supported by psycopg2cffi (the postgresql adapter for use with PyPy)
- dspillett 13y ago> error messages which make sense to developers & end users. Giving this sort of detail direct to users is generally a bad idea for two reasons: 1. It can scare the living bejesus out of some of them. 2. You are handing out knowledge of your applications inner workings. If you have an unfound injection route a malicious entity might be able to use of your error messages to either find the injection flaw and/or find things to do with it once found. This sort of detail should be logged of course, but give the users a simple code to report to you when reporting the problem ("please quote issue XY0009 when contacting support about this issue") and record the detail against that for your reference.
- sspiff 13y agoDepends on who you're targeting (developers know how to make sense of types etc) and how well you translate the error info to human-readable error messages. More information should allow you to produce better reporting. It's also not necessary to present all the information to your users, you can also make better error logs that users can send to the developers.
- dspillett 13y ago> Depends on who you're targeting (developers By all means have a developers mode which does hand out the information more readily (though I tend to be wary of that in case it gets left on in production, and because it is an extra code path that needs to be properly tested), or report the detail directly if it is an internal tool. But the post I was referring to infored (to my mind) handing the detail out to end users (and by "end users" I mean the untrustworthy mob that is the general public). > It's also not necessary to present all the information to your users, you can also make better error logs that users can send to the developers. Hence my last para, where I suggest giving the user reference to the detail without giving them the actual details (the code to give to you for looking up the stored exception report). Giving all the info to the user as an encrypted package would work too (in that case "please quote this code in any issue reports" becomes "please copy+paste this block of text into any problem report").
- einhverfr 13y agoOne option is to log everything but display something simple to the user.
- gaius 13y agoSure, but it's got to be better than the error you get from JDBC, which will be 200 lines long and not bother to include the SQL being executed, nor the username nor the database, making it utterly useless to anyone.
- dspillett 13y agoI'm not questioning the feature as a diagnostics tool, but the post I replied to seemed to be suggesting giving the error details out fairly directly to the user which is not good security practise (IMO). That 200 line JDBC error should not be going out to the end-user at all either, especially not if it did include the SQL being executed or worse still the db/authentication details (db name, user name).
- masklinn 13y ago> the post I replied to seemed to be suggesting giving the error details out fairly directly to the user I can't fathom how you'd come to such a conclusion from: > translate error information back into your application domain and generate logging and error messages which make sense to developers & end users. which describes more or less the opposite of "giving the error details out fairly directly".
- masklinn 13y agoI think you missed the whole point, which is to not give these details directly to the user: if you were a crazy pervert you could already do that by sending them Postgres's generated error messages. By having error details in a programmatic format, you can now generates messages which are actually useful and sensible to developers and/or end users if you wish to. Something which you could not do before.
- dspillett 13y agoI'm not questioning the enhancement generally: it sounds invaluable for diagnostic purposes for developers (and potentially 3rd line support), far easier than manually plucking details out of an arbitrary error string (or not getting any specific information at all). Using the information to give a user more indication of whether they should try again or report the issue and wait would be useful and safe, but if you are trying to report to the user "that code already exists, they must be unique" or "name missing, it must be provided" then you have failed input validation as that sort of thing should be picked up on much earlier in a request lifecycle IMO. I'm surprised how often I still see a full exception report echoed all the way from the data layer of an application up to my browser.
- joevandyk 13y agoYou can't catch all errors in the application server. Data in transactions that haven't committed yet isn't available (usually) to the application servers.
- masklinn 13y ago> if you are trying to report to the user "that code already exists, they must be unique" or "name missing, it must be provided" then you have failed input validation as that sort of thing should be picked up on much earlier in a request lifecycle IMO. 1. Not necessarily, most applications could use a well-designed schema to let the database do these validations and send improved results back to the user, it avoids duplicating (or triplicating) validation information. Especially for things like UNIQUE constraints, you're going to hit the database in-application when the DB will do it anyway? Unless it skips a significant amount of application work it's a complete waste of developer time (and likely application time as well). 2. A good schema can do significantly more complex, interesting and expensive (especially in-application if they need database data) validations than a mere NOT NULL check. 3. In-transaction issues can't be caught by the application server, they'll only blow up in the database. > I'm surprised how often I still see a full exception report echoed all the way from the data layer of an application up to my browser. Thus confirming that you're completely missing the point. Again, if one wanted to send database error messages to the end user one could already easily do so.
- asdasf 13y agoAny idea if it is feasible/plausible/possible for postgresql to return multiple errors instead of just one? The only reason I can see to have to do validation in the app now is so you can report 5 validation errors at once, rather than make the user submit the form and correct one field five times.
- masklinn 13y agoNot that I know, you get a single PGResult which may be in an error state and have a bunch of error fields set. I don't think there is a way to get multiple errors out of it. But I'd suggest asking on the mailing list about the possibility of getting multiple errors in the future. Not sure that makes sense from the database's point of view though, they're not "validation" issues as far as it's concerned they're integrity errors, a query is putting the db in an invalid state and thus rejected. Because the state is invalid, I'm not sure how the db would progress to get more errors.
- jacques_chester 13y agoThen don't call them errors. Call them "speculative problems" or something.
- masklinn 13y agoLying about what they are doesn't change the underlying issue: > they're integrity errors, a query is putting the db in an invalid state and thus rejected. Because the state is invalid, I'm not sure how the db would progress to get more errors.
- jacques_chester 13y agoI agree with you that it's logically insensible to consider them "errors" because the resolution of the current constraint may change the downstream interpretation. However it's a tremendously useful development tool.