1

Say my uri expects GUID whereas client is supplying string.empty.

I would like to have control on the error message being send to the client.

The default error message is like this -

<Error>
  <Message>
    The request is invalid.
  </Message>
  <MessageDetail>
    The parameters dictionary contains a null entry for parameter 'id' of non-nullable 
    type 'System.Guid' for method 'xx.xx.MyResponse Get(System.Guid)' in 
    'xx.xx.MyController'. An optional parameter must be a reference type, a nullable 
    type, or be declared as an optional parameter.
  </MessageDetail>
</Error>

But what if I want to throw the custom error like this -

<Error>
  <Message>
    Invalid GUID.....Please enter a valid GUID.
  </Message>
</Error>

Here is the REST Method definition -

public MyResponse Get(Guid id)
{
    // method body
}

I also implemented custom exception filter but it didn't reached to it -

public class ResponseExceptionFilter : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
      actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(statusCode, "Invalid GUID....Please enter a valid GUID.");
    }
}

it didn't reached to OnException and throws system error....which I don't want.

Now what if the client passed "abcd" instead of a valid guid, it should display my own exception instead of system defined exception.

Anil Purswani
  • 1,857
  • 6
  • 35
  • 63

2 Answers2

2

It didn't reach to your custom exception filter because the error didn't happen in the controller/action level in the pipeline. Now you have two choices:

  1. Modify http error response message with a message handler: Although this way you can reshape the HttpError response, but it'll reshape all your HttpErrors. Take a look at this post asp.net Web Api - Default Error Messages (also note that, in this post, if you're using .NET 4.5, instead of using ContinueWith, you can simple use await, as a reference take a look at: Http Message Handlers )

  2. Workaround this by removing route constraint and accepting null values in your action method: In this case, remove the route constraint and modify your action to accept null values, then validate your input parameter in the action method like this:

    [Route("{id}")]
    public MyResponse Get(Guid? id)
    {
        if (id == null)
        {
            throw new ArgumentException("Please enter a valid Guid.");
        }
    
        ...
    }
    
Community
  • 1
  • 1
Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
  • 1
    second workaround doesn't seems fit as user may not pass 'null' but something like "abc" which is invalid parameter, so nullable Guid will not resolve this issue.....first suggestion seems reasonable....I need add some message handler to handle the response.... +1 for close suggestions – Anil Purswani Mar 24 '14 at 11:20
  • Actually since your input parameter is Guid, user inputs such as, abcd or even 1234 will be interpreted as null. – Arin Ghazarian Mar 24 '14 at 17:31
0

If I'm reading your question correctly, it sounds like you are seeing the system defined .NET error message instead of the one you're looking for.

I think you simply need to enable pass-through exceptions: https://stackoverflow.com/a/702809/753958

Community
  • 1
  • 1
David Savage
  • 760
  • 5
  • 15
  • the link didn't helped much.... I concern is not w.r.t. custom error pages but it is related to custom error...moreover it is hitting "onexception" for anything wrong other then parameter ... – Anil Purswani Mar 24 '14 at 06:13