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.

4
  • have you registered your exception filter? and also have you set route constraint for you action? Commented Mar 22, 2014 at 5:30
  • Yes I did.... infact for any other kind of error it hit onException method....like database error etc......but for invalid parameter it doesn't hit Commented Mar 22, 2014 at 6:07
  • So you've set the route constraint for Guid, right? Commented Mar 22, 2014 at 8:57
  • yeah i added route constraints Commented Mar 24, 2014 at 6:11

2 Answers 2

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.");
        }
    
        ...
    }
    
Sign up to request clarification or add additional context in comments.

2 Comments

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
Actually since your input parameter is Guid, user inputs such as, abcd or even 1234 will be interpreted as null.
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

1 Comment

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 ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.