I am using PetaPoco and have the following code to get a list of names from the database.
public IEnumerable<string> GetManufacturers()
{
try
{
statement = "EXEC P_GET_ALL_MANUFACTURER_NAMES";
var data = db.Query<string>(statement);
return data;
}
catch (SqlException sqlex)
{
LogManager.WriteException(sqlex);
throw;
}
catch (InvalidOperationException ioex)
{
LogManager.WriteException(ioex);
throw;
}
}
When the stored procedure doesn't exist in the database, I noticed that the exception isn't being caught in the catch block and is being written to the data
variable which is being written to the API layer above where it is getting rendered as a JSON object.
{
"Message" : "An error has occurred.",
"ExceptionMessage" : "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
"ExceptionType" : "System.InvalidOperationException",
"StackTrace" : null,
"InnerException" : {
"Message" : "An error has occurred.",
"ExceptionMessage" : "Could not find stored procedure 'P_GET_ALL_MANUFACTURER_NAMES'.",
"ExceptionType" : "System.Data.SqlClient.SqlException",
"StackTrace" : "..."
}
}
What am I doing wrong here? How can I get PetaPoco to throw the exception?