0

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?

Animesh
  • 4,926
  • 14
  • 68
  • 110
  • 1
    Could it be that since you are telling PetaPoco to expect a string and it gets one that it does not see anything wrong? Have you tried creating a class to hold the returned data? – saarrrr Aug 23 '16 at 15:47
  • Shit! okay that was stupid. – Animesh Aug 23 '16 at 15:50
  • Have you solved it? – Eduardo Molteni Aug 25 '16 at 11:45
  • No, I avoided the problem for the time being by making sure the stored proc is available. I tried having a class with a string property, but that gives me output like this: `[{"propname","val1"},{"propname","val2"}]`. I am trying for something like `["val1","val2"]` which was possible with the `Query` approach. – Animesh Aug 25 '16 at 16:51

1 Answers1

0

Are you not re-throwing it?

throw;
Plebsori
  • 1,075
  • 9
  • 23