SDammann.WebApi.Versioning icon indicating copy to clipboard operation
SDammann.WebApi.Versioning copied to clipboard

Exception handling

Open richardp-au opened this issue 10 years ago • 2 comments

I'd like to return different error messages to the user to attempt to explain the problems better. For example, rather than say that an API doesn't exist, I'd like to tell them if:

  1. They've omitted the version from the Accept header completely. I'd rather report this in a nice way than have a default version number.
  2. They've supplied an invalid version in the Accept header. This could just end up being the same as above.
  3. They've asked for a version number that doesn't exist (on any route). I'm not sure that this would be possible or not but it feels nicer than just saying that the API wasn't found when in fact that version number is not ever used.

I also found that I get The API 'SDammann.WebApi.Versioning.ControllerIdentification' doesn't exist returned when I omit the version header. Not sure why it's referring to a namespace that isn't mine but it's definitely not clear to users what's going on.

The problem is that since the code throws HttpResponseExceptions, I'm unable to handle them in a global error handler.

What do you think about either changing the type of exception thrown so it can be caught in the global error handler and handled appropriately, or even providing an error handling class that can be derived and injected into the configuration?

At the moment, the best I've come up with is deriving from VersionedApiControllerSelector and wrapping OnSelectController in a try/catch block so I can create a different response and throw another HttpResponseException. It would be nice to have a cleaner way of doing this.

(Using v3.0.0-beta1.)

richardp-au avatar Feb 23 '15 07:02 richardp-au

You're right. Throwing a custom exception, and then using an exception handler to rethrow it in the proper format is a good choice, of which I didn't think during development of this lib.

Sebazzz avatar Feb 23 '15 19:02 Sebazzz

In beta3, just pushed to nuget.org, you may either implement your own global exception filter or inherit from DefaultVersioningExceptionHandler and register IVersioningExceptionHandler with your DI container.

See the readme file for more information.

Sebazzz avatar Mar 01 '15 09:03 Sebazzz