0

The ExceptionHandler(set inside the overridden Configure method of AppHostBase) of servicestack has the 'exception' parameter of generic Exception type in the lambda.

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    if(exception is ArgumentException)
    {
      // some code
    }
}

Inside the lambda, I wish to add a specific condition if the exception is of ArgumentException type. Is there any way to identify which specific type of exception was thrown? Checking the type with 'is' keyword is not working as given by this link

FYI, a custom ServiceRunner is implemented for the servicestack instance that we use.

ronilk
  • 303
  • 2
  • 4
  • 14
  • As far as I understand the answer in the linked question it IS working!? – Romano Zumbé Jun 14 '17 at 11:58
  • On yes it IS. If your code block is not running then the type of exception is not an ArguementException. Try debugging the code to see what type of exception it is or try `exception.GetType().ToString()`. – Scrobi Jun 14 '17 at 12:39
  • There is no `ExceptionHandler` in ServiceStack v4, if this is for [ServiceStack v3 you need to use the \[servicestack-bsd\] hash tag](https://github.com/servicestackv3/servicestackv3#support). – mythz Jun 15 '17 at 01:42
  • I checked again, but the exception object for some reason cannot be recognised as an `ArgumentException` inside the `ExceptionHandler`. Finally, the solution as answered below works. – ronilk Jun 23 '17 at 12:17

1 Answers1

0

The piece of code that caused the ArgumentException was

return serializer.Deserialize(querystring, TEMP);

For some reason, the exception object cannot be recognised as an ArgumentException inside the ExceptionHandler

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    httpResp.StatusCode = 500;
    bool isArgEx = exception is ArgumentException; // returns false        
    if(isArgEx)
    {
        //do something
    }
}

Although, as mentioned in the link(pls refer question) the InnerException can be identified using is keyword.

Hence the solution applied was to throw the ArgumentException as an inner exception as follows:

public const string ARG_EX_MSG = "Deserialize|ArgumentException";

try
{
    return serializer.Deserialize(querystring, TEMP);
}
catch(ArgumentException argEx)
{
    throw new Exception(ARG_EX_MSG, argEx);
}

Hence, now the ExceptionHandler code is:

this.ExceptionHandler = (httpReq, httpResp, operationName, exception) =>
{
    httpResp.StatusCode = 500;
    bool isArgEx = exception.InnerException is ArgumentException; // returns true
    if(isArgEx)
    {
        //do something
    }
}
ronilk
  • 303
  • 2
  • 4
  • 14