Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Handling exception thrown during controller Initialize override.

$
0
0

I have a base controller class that's inherited by several controller classes. The base class overrides Initialize to do some preparation so that any action handler will have access to some important basic data when it begins to execute.

My question is: if an exception is thrown within this method then what is the preferred way to handle and send a response back to client?

This exception will be thrown (or rather might be thrown) before any action method has yet been invoked and so the Request member is null at that point.

Within action handlers themselves I have this:

            catch (Exception e)
            {
                HttpResponseMessage message = Request.CreateErrorResponse(HttpStatusCode.BadRequest, e);
                throw new HttpResponseException(message);
            }

but because Request is null in the Initialize override this cannot work, beside I don't know if throwing a new HttpResponseException within Initialize is the right thing to do.

Thanks


Viewing all articles
Browse latest Browse all 4850

Trending Articles