{
PQERRORS_TERSE,
PQERRORS_DEFAULT,
- PQERRORS_VERBOSE
+ PQERRORS_VERBOSE,
+ PQERRORS_SQLSTATE
} PGVerbosity;
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
- PQsetErrorVerbosity sets the verbosity mode, returning
- the connection's previous setting. In TERSE mode,
- returned messages include severity, primary text, and position only;
- this will normally fit on a single line. The default mode produces
- messages that include the above plus any detail, hint, or context
- fields (these might span multiple lines). The VERBOSE
- mode includes all available fields. Changing the verbosity does not
- affect the messages available from already-existing
- PGresult objects, only subsequently-created ones.
+ PQsetErrorVerbosity sets the verbosity mode,
+ returning the connection's previous setting.
+ In TERSE mode, returned messages include
+ severity, primary text, and position only; this will normally fit on a
+ single line. The default mode produces messages that include the above
+ plus any detail, hint, or context fields (these might span multiple
+ lines). The VERBOSE mode includes all available
+ fields. The SQLSTATE mode includes only the
+ error severity and the SQLSTATE error code, if one is
+ available (if not, the output is like TERSE
+ mode).
+
+
+ Changing the verbosity setting does not affect the messages available
+ from already-existing PGresult objects, only
+ subsequently-created ones.
(But see PQresultVerboseErrorMessage if you
want to print a previous error with a different verbosity.)
PQsetErrorContextVisibility sets the context display mode,
returning the connection's previous setting. This mode controls
- whether the CONTEXT field is included in messages
- (unless the verbosity setting is TERSE, in which
- case CONTEXT is never shown). The NEVER mode
+ whether the CONTEXT field is included in messages.
+ The NEVER mode
never includes CONTEXT, while ALWAYS always
includes it if available. In ERRORS mode (the
- default), CONTEXT fields are included only for error
- messages, not for notices and warnings. Changing this mode does not
+ default), CONTEXT fields are included only in error
+ messages, not in notices and warnings.
+ (However, if the verbosity setting is TERSE
+ or SQLSTATE, CONTEXT fields
+ are omitted regardless of the context display mode.)
+
+
+ Changing this mode does not
affect the messages available from
already-existing PGresult objects, only
subsequently-created ones.
messages from the server. The default is errors (meaning
that context will be shown in error messages, but not in notice or
warning messages). This setting has no effect
- when VERBOSITY is set to terse.
+ when VERBOSITY is set to terse
+ or sqlstate.
(See also \errverbose, for use when you want a verbose
version of the error you just got.)
This variable can be set to the values default,
- verbose, or terse to control the verbosity
- of error reports.
+ verbose, terse,
+ or sqlstate to control the verbosity of error
+ reports.
(See also \errverbose, for use when you want a verbose
version of the error you just got.)
* Windows builds currently print one more line than non-Windows builds.
* Using the larger number is fine.
*/
- output = PageOutput(156, pager ? &(pset.popt.topt) : NULL);
+ output = PageOutput(158, pager ? &(pset.popt.topt) : NULL);
fprintf(output, _("List of specially treated variables\n\n"));
fprintf(output, _(" USER\n"
" the currently connected database user\n"));
fprintf(output, _(" VERBOSITY\n"
- " controls verbosity of error reports [default, verbose, terse]\n"));
+ " controls verbosity of error reports [default, verbose, terse, sqlstate]\n"));
fprintf(output, _(" VERSION\n"
" VERSION_NAME\n"
" VERSION_NUM\n"
Assert(newval != NULL); /* else substitute hook messed up */
if (pg_strcasecmp(newval, "default") == 0)
pset.verbosity = PQERRORS_DEFAULT;
- else if (pg_strcasecmp(newval, "terse") == 0)
- pset.verbosity = PQERRORS_TERSE;
else if (pg_strcasecmp(newval, "verbose") == 0)
pset.verbosity = PQERRORS_VERBOSE;
+ else if (pg_strcasecmp(newval, "terse") == 0)
+ pset.verbosity = PQERRORS_TERSE;
+ else if (pg_strcasecmp(newval, "sqlstate") == 0)
+ pset.verbosity = PQERRORS_SQLSTATE;
else
{
- PsqlVarEnumError("VERBOSITY", newval, "default, terse, verbose");
+ PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
return false;
}
else if (TailMatchesCS("SHOW_CONTEXT"))
COMPLETE_WITH_CS("never", "errors", "always");
else if (TailMatchesCS("VERBOSITY"))
- COMPLETE_WITH_CS("default", "verbose", "terse");
+ COMPLETE_WITH_CS("default", "verbose", "terse", "sqlstate");
}
else if (TailMatchesCS("\\sf*"))
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines, NULL);
val = PQresultErrorField(res, PG_DIAG_SEVERITY);
if (val)
appendPQExpBuffer(msg, "%s: ", val);
+
+ if (verbosity == PQERRORS_SQLSTATE)
+ {
+ /*
+ * If we have a SQLSTATE, print that and nothing else. If not (which
+ * shouldn't happen for server-generated errors, but might possibly
+ * happen for libpq-generated ones), fall back to TERSE format, as
+ * that seems better than printing nothing at all.
+ */
+ val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
+ if (val)
+ {
+ appendPQExpBuffer(msg, "%s\n", val);
+ return;
+ }
+ verbosity = PQERRORS_TERSE;
+ }
+
if (verbosity == PQERRORS_VERBOSE)
{
val = PQresultErrorField(res, PG_DIAG_SQLSTATE);
{
PQERRORS_TERSE, /* single-line error messages */
PQERRORS_DEFAULT, /* recommended style */
- PQERRORS_VERBOSE /* all the facts, ma'am */
+ PQERRORS_VERBOSE, /* all the facts, ma'am */
+ PQERRORS_SQLSTATE /* only error severity and SQLSTATE code */
} PGVerbosity;
typedef enum
last error message: table "this_table_does_not_exist" does not exist
\echo 'last error code:' :LAST_ERROR_SQLSTATE
last error code: 42P01
+-- nondefault verbosity error settings (except verbose, which is too unstable)
+\set VERBOSITY terse
+SELECT 1 UNION;
+ERROR: syntax error at or near ";" at character 15
+\echo 'error:' :ERROR
+error: true
+\echo 'error code:' :SQLSTATE
+error code: 42601
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+last error message: syntax error at or near ";"
+\set VERBOSITY sqlstate
+SELECT 1/0;
+ERROR: 22012
+\echo 'error:' :ERROR
+error: true
+\echo 'error code:' :SQLSTATE
+error code: 22012
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+last error message: division by zero
+\set VERBOSITY default
-- working \gdesc
SELECT 3 AS three, 4 AS four \gdesc
Column | Type
\echo 'last error message:' :LAST_ERROR_MESSAGE
\echo 'last error code:' :LAST_ERROR_SQLSTATE
+-- nondefault verbosity error settings (except verbose, which is too unstable)
+\set VERBOSITY terse
+SELECT 1 UNION;
+\echo 'error:' :ERROR
+\echo 'error code:' :SQLSTATE
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+
+\set VERBOSITY sqlstate
+SELECT 1/0;
+\echo 'error:' :ERROR
+\echo 'error code:' :SQLSTATE
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+
+\set VERBOSITY default
+
-- working \gdesc
SELECT 3 AS three, 4 AS four \gdesc
\echo 'error:' :ERROR