// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics.Contracts; using System.Net.Http; using System.Web.Http.Controllers; using System.Web.Http.Properties; namespace System.Web.Http.ExceptionHandling { /// Represents an exception and the contextual data associated with it when it was caught. public class ExceptionContext { /// Initializes a new instance of the class. /// The exception caught. /// The catch block where the exception was caught. /// This constructor is for unit testing purposes only. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock) { if (exception == null) { throw new ArgumentNullException("exception"); } Exception = exception; if (catchBlock == null) { throw new ArgumentNullException("catchBlock"); } CatchBlock = catchBlock; } /// /// Initializes a new instance of the class using the values provided. /// /// The exception caught. /// The catch block where the exception was caught. /// The action context in which the exception occurred. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpActionContext actionContext) : this(exception, catchBlock) { if (actionContext == null) { throw new ArgumentNullException("actionContext"); } ActionContext = actionContext; HttpControllerContext controllerContext = actionContext.ControllerContext; if (controllerContext == null) { throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, typeof(HttpActionContext).Name, "ControllerContext"), "actionContext"); } ControllerContext = controllerContext; HttpRequestContext requestContext = controllerContext.RequestContext; Contract.Assert(requestContext != null); RequestContext = requestContext; HttpRequestMessage request = controllerContext.Request; if (request == null) { throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, typeof(HttpControllerContext).Name, "Request"), "actionContext"); } Request = request; } /// /// Initializes a new instance of the class using the values provided. /// /// The exception caught. /// The catch block where the exception was caught. /// The request being processed when the exception was caught. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request) : this(exception, catchBlock) { if (request == null) { throw new ArgumentNullException("request"); } Request = request; RequestContext = request.GetRequestContext(); } /// /// Initializes a new instance of the class using the values provided. /// /// The exception caught. /// The catch block where the exception was caught. /// The request being processed when the exception was caught. /// The repsonse being returned when the exception was caught. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request, HttpResponseMessage response) : this(exception, catchBlock) { if (request == null) { throw new ArgumentNullException("request"); } Request = request; RequestContext = request.GetRequestContext(); if (response == null) { throw new ArgumentNullException("response"); } Response = response; } /// Gets the exception caught. public Exception Exception { get; private set; } /// Gets the catch block in which the exception was caught. public ExceptionContextCatchBlock CatchBlock { get; private set; } /// Gets the request being processed when the exception was caught. /// The setter is for unit testing purposes only. public HttpRequestMessage Request { get; set; } /// Gets the request context in which the exception occurred. /// The setter is for unit testing purposes only. public HttpRequestContext RequestContext { get; set; } /// Gets the controller context in which the exception occurred, if available. /// /// This property will be in most cases. /// The setter is for unit testing purposes only. /// public HttpControllerContext ControllerContext { get; set; } /// Gets the action context in which the exception occurred, if available. /// /// This property will be in most cases. /// The setter is for unit testing purposes only. /// public HttpActionContext ActionContext { get; set; } /// Gets the response being sent when the exception was caught. /// /// This property will be in most cases. /// The setter is for unit testing purposes only. /// public HttpResponseMessage Response { get; set; } } }