Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract the user-defined Error message from exception

Tags:

sql

postgresql

Inside my stored procedure, i just checked a particular condition and i'll throw Exception if that condition fails. like below,

Raise Exception '%', 'Hello world';

It is working fine, But the error message Hello world also comes with some other error code like stuffs like below in my front end,

DA00014:ERROR: P0001: Hello world
|___________________|
        |
      I really dont want this part to get displayed in the front end.

Is there any proper way available to filter out/Extract the right message from thrown exception.?

[Note: Please don't suggest me to do some string manipulations.]

DBMS      : POSTGRESQL 9.0.3
Driver    : Npgsql 1.0
Front End : VB.net
like image 620
Rajaprabhu Aravindasamy Avatar asked Oct 21 '13 11:10

Rajaprabhu Aravindasamy


People also ask

How do I get the detailed exception message in Python?

To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e . You can now print the error message with "print(e)" or use it for further processing.

How do I print exception text in Python?

Use traceback. print_exc() to print the current exception to standard error, just like it would be printed if it remained uncaught, or traceback. format_exc() to get the same output as a string.

How do I read an exception message in Java?

Following are the different ways to handle exception messages in Java. Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception.

How do you attain user defined exceptions in Python?

In Python, users can define custom exceptions by creating a new class. This exception class has to be derived, either directly or indirectly, from the built-in Exception class. Most of the built-in exceptions are also derived from this class.


1 Answers

Npgsql Fastpath

If NpgsqlException.Errors is null in your exception handler then you have most likely hit a bug in Npgsql Fastpath (well I consider it a bug anyway). Here is the offending line in version 2 but the same issue exists in version 1.

It basically boils down to this code where a proper NpgsqlError is constructed and then thrown away when raising the exception by calling ToString() on it.

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(e.ToString());

The fix would be as simple as changing the code to this and using the already supported ability of NpgsqlException to take in a list of NpgsqlError in the constructor.

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(new ArrayList(e));

So in summary you can't do it without string manipulation unless you compile your own version of Npgsql patching the issue.

Npgsql

If you aren't using Fastpath then the thrown NpgsqlException object contains an Errors property which should be a non null list. This is an example of extracting the message from the first error.

(ex.Errors[0] as NpgsqlError).Message
like image 93
David Ewen Avatar answered Oct 13 '22 21:10

David Ewen