7

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
6
  • And what's on the front end - language, drivers, etc.? Commented Oct 21, 2013 at 11:43
  • @MilenA.Radev language. By the way it is VB.net. Commented Oct 21, 2013 at 11:45
  • And the driver? Npgsql? Commented Oct 21, 2013 at 11:51
  • @MilenA.Radev i have updated that detail into my question Commented Oct 21, 2013 at 11:55
  • 1
    @MilenA.Radev Can you give any samples regarding to that.? Commented Oct 21, 2013 at 12:38

4 Answers 4

1
+50

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
Sign up to request clarification or add additional context in comments.

1 Comment

Seems interesting, I'll try this out and post back the result soon.
0

try this..
Inside your store procedure place an extra character in message. e.g

Raise Exception '%', '|Hello world';

On Front end split your message on character "|", and display second index of array. e.g

    Try
      ' your code..
    Catch ex As Exception
        Dim arr As String() = ex.Message.Split("|")
        If arr.Count > 1 Then
            MessageBox.Show(Convert.ToString(arr(1)))
        Else
            MessageBox.Show(Convert.ToString(ex.Message))
        End If
    End Try

6 Comments

The Note which was given by OP at the bottom of the question
There is no other way to handle this, SQL Server also generate such type of Message, when Raiserror in Trigger is used.
There is no other way to handle this. Do you have any concrete proof for that?
No, i haven't, If you have any other better solution, please provide it.
No offense, I have been suffering from the same problem too, And i have been searching for a elegant way to handle this.
|
0

If i understood, you can try this once

IF condition match -- (your condition)
BEGIN
    RAISERROR('Hello world', 16,16)
    return
 END

and in expception

can use like

catch(exception ex)
{
  // ex.Message
  //  ex.InnerException.Message
}

Comments

0

This should work:

Catch ex As Exception

    lblMsg.Text = ex.Message.Replace("ERROR: P0001: ", "")

End Try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.