Accessing SQL error messages

I'm looking for ways to get hold of the error message produced by an SQL query.

The query in question is calling a stored procedure in a legacy MSSQL system, and an ActiveRecord::StatementInvalid exception is being raised. Is there any way to get information about the exception (like the error message) so it can be displayed to the user?

In a begin..rescue block I can access .message as follows, but I'd rather not be parsing that to get the error message on the third line for obvious reasons

irb(Mssql):005:0> err.message => "DBI::DatabaseError: Execute\n OLE error code:80040E14 in Microsoft OLE DB Provider for SQL Server\n Invalid surname, title or address.\n HRESU LT error code:0x80020009\n Exception occurred.: EXEC lgg_createuserdetails 'Gareth', [snip], 0"

Thanks guys, Gareth

Other than parsing .message, there's no handy way I know to get the error message produced by SQL Server. As errors thrown should indicate exceptional circumstances, the messages within them are really intended for debugging purposes, rather than to display to end users.

The message you seem to be trying to extract looks more like a validation failure. A better approach would be to perform validation before calling the stored procedure. Other than that, parsing the error message is your only hope.