/// <summary> /// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided. /// </summary> /// <param name="exception">The exception caught.</param> /// <param name="catchBlock">The catch block where the exception was caught.</param> /// <param name="actionContext">The action context in which the exception occurred.</param> 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; }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided. /// </summary> /// <param name="exception">The exception caught.</param> /// <param name="catchBlock">The catch block where the exception was caught.</param> /// <param name="request">The request being processed when the exception was caught.</param> /// <param name="response">The repsonse being returned when the exception was caught.</param> public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request, HttpResponseMessage response) { if (exception == null) { throw new ArgumentNullException("exception"); } Exception = exception; if (catchBlock == null) { throw new ArgumentNullException("catchBlock"); } CatchBlock = catchBlock; if (request == null) { throw new ArgumentNullException("request"); } Request = request; RequestContext = request.GetRequestContext(); if (response == null) { throw new ArgumentNullException("response"); } Response = response; }
public void ConstructorWithActionContext_SetsPropertiesToSpecifiedValues() { // Arrange Exception expectedException = CreateException(); HttpRequestContext expectedRequestContext = CreateRequestContext(); using (HttpRequestMessage expectedRequest = CreateRequest()) { HttpControllerContext expectedControllerContext = CreateControllerContext(expectedRequestContext, expectedRequest); HttpActionContext expectedActionContext = CreateActionContext(expectedControllerContext); ExceptionContextCatchBlock expectedCatchBlock = CreateCatchBlock(); // Act ExceptionContext product = CreateProductUnderTest(expectedException, expectedCatchBlock, expectedActionContext); // Assert Assert.Same(expectedException, product.Exception); Assert.Same(expectedCatchBlock, product.CatchBlock); Assert.Same(expectedActionContext, product.ActionContext); Assert.Same(expectedControllerContext, product.ControllerContext); Assert.Same(expectedRequestContext, product.RequestContext); Assert.Same(expectedRequest, product.Request); Assert.Null(product.Response); } }
private static ExceptionHandlerContext CreateValidContext( HttpRequestMessage request, ExceptionContextCatchBlock catchBlock ) { return(CreateContext(new ExceptionContext(CreateException(), catchBlock, request))); }
public void Log(Exception ex, HttpRequestMessage request, string catchBlockName) { var catchBlock = new ExceptionContextCatchBlock(catchBlockName, true, false); var exceptionContext = new ExceptionContext(ex, catchBlock, request); var exceptionLoggerContext = new ExceptionLoggerContext(exceptionContext); Log(exceptionLoggerContext); }
private static ExceptionContext CreateProductUnderTest( Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request ) { return(new ExceptionContext(exception, catchBlock, request)); }
private static ExceptionContext CreateProductUnderTest( Exception exception, ExceptionContextCatchBlock catchBlock, HttpActionContext actionContext ) { return(new ExceptionContext(exception, catchBlock, actionContext)); }
private ExceptionHandlerContext GetAuthorizedExceptionContext(AuthenticationHeaderValue authHeader) { var ex = new Exception(); var catchBlock = new ExceptionContextCatchBlock("catch", false, false); var context = new ExceptionHandlerContext(new ExceptionContext(ex, catchBlock)); context.ExceptionContext.Request = new HttpRequestMessage(); context.ExceptionContext.Request.Headers.Authorization = authHeader; return context; }
public void HttpBatchHandler_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = ExceptionCatchBlocks.HttpBatchHandler; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpBatchHandler", isTopLevel: false, callsHandler: true); AssertEqual(expected, catchBlock); }
public void ConstructorWithRequest_IfRequestIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpRequestMessage request = null; // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, request), "request"); }
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request, ExceptionContextCatchBlock catchBlock) { return(CreateContext(new ExceptionContext { Exception = CreateException(), Request = request, CatchBlock = catchBlock })); }
public void HttpBatchHandler_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = ExceptionCatchBlocks.HttpBatchHandler; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpBatchHandler", isTopLevel: false); AssertEqual(expected, catchBlock); }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided. /// </summary> /// <param name="exception">The exception caught.</param> /// <param name="catchBlock">The catch block where the exception was caught.</param> /// <param name="request">The request being processed when the exception was caught.</param> public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request) : this(exception, catchBlock) { if (request == null) { throw new ArgumentNullException("request"); } Request = request; RequestContext = request.GetRequestContext(); }
public void IExceptionFilter_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = ExceptionCatchBlocks.IExceptionFilter; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("IExceptionFilter", isTopLevel: false, callsHandler: true); AssertEqual(expected, catchBlock); }
public void HttpServer_IsSameInstance() { // Arrange ExceptionContextCatchBlock first = ExceptionCatchBlocks.HttpServer; // Act ExceptionContextCatchBlock second = ExceptionCatchBlocks.HttpServer; // Assert Assert.Same(first, second); }
public void HttpMessageHandlerAdapterBufferError_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = OwinExceptionCatchBlocks.HttpMessageHandlerAdapterBufferError; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpMessageHandlerAdapter.BufferError", isTopLevel: true, callsHandler: false); AssertEqual(expected, catchBlock); }
public void HttpControllerHandlerComputeContentLength_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = WebHostExceptionCatchBlocks.HttpControllerHandlerComputeContentLength; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpControllerHandler.ComputeContentLength", isTopLevel: true, callsHandler: false); AssertEqual(expected, catchBlock); }
public void HttpControllerHandlerBufferContent_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = WebHostExceptionCatchBlocks.HttpControllerHandlerBufferContent; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpControllerHandler.BufferContent", isTopLevel: true, callsHandler: true); AssertEqual(expected, catchBlock); }
public void ConstructorWithActionContext_IfActionContextIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpActionContext actionContext = null; // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, actionContext), "actionContext"); }
public void IsTopLevel_IsSpecifiedValue(bool expectedIsTopLevel) { // Arrange string name = "IgnoreName"; ExceptionContextCatchBlock product = CreateProductUnderTest(name, expectedIsTopLevel); // Act bool isTopLevel = product.IsTopLevel; // Assert Assert.Equal(expectedIsTopLevel, isTopLevel); }
private static void AssertEqual(ExceptionContextCatchBlock expected, ExceptionContextCatchBlock actual) { if (expected == null) { Assert.Null(actual); return; } Assert.NotNull(actual); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.IsTopLevel, actual.IsTopLevel); }
public void CallsHandler_IsSpecifiedValue(bool expectedCallsHandler) { // Arrange string name = "IgnoreName"; bool isTopLevel = true; ExceptionContextCatchBlock product = CreateProductUnderTest(name, isTopLevel, expectedCallsHandler); // Act bool callsHandler = product.CallsHandler; // Assert Assert.Equal(expectedCallsHandler, callsHandler); }
public void Name_IsSpecifiedInstance() { // Arrange string expectedName = "TheName"; bool isTopLevel = true; ExceptionContextCatchBlock product = CreateProductUnderTest(expectedName, isTopLevel); // Act string name = product.Name; // Assert Assert.Same(expectedName, name); }
public void CatchBlockGet_ReturnsSpecifiedInstance() { // Arrange ExceptionContextCatchBlock expectedCatchBlock = new ExceptionContextCatchBlock("IgnoreName", false, false); ExceptionContext context = new ExceptionContext(new Exception(), expectedCatchBlock); ExceptionLoggerContext product = CreateProductUnderTest(context); // Act ExceptionContextCatchBlock catchBlock = product.CatchBlock; // Assert Assert.Same(expectedCatchBlock, catchBlock); }
public void ConstructorWithActionContext_IfControllerContextIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpActionContext actionContext = new HttpActionContext(); Assert.Null(actionContext.ControllerContext); // Guard // Act & Assert Assert.ThrowsArgument(() => CreateProductUnderTest(exception, catchBlock, actionContext), "actionContext", "HttpActionContext.ControllerContext must not be null."); }
/// <summary> /// Initializes a new instance of the <see cref="ExceptionContext"/> class using the values provided. /// </summary> /// <param name="exception">The exception caught.</param> /// <param name="catchBlock">The catch block where the exception was caught.</param> /// <param name="actionContext">The action context in which the exception occurred.</param> 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; }
public void CatchBlockSet_UpdatesValue() { // Arrange ExceptionContext product = CreateProductUnderTest(); ExceptionContextCatchBlock expectedCatchBlock = CreateCatchBlock(); // Act product.CatchBlock = expectedCatchBlock; // Assert ExceptionContextCatchBlock catchBlock = product.CatchBlock; Assert.Same(expectedCatchBlock, catchBlock); }
public void ConstructorWithRequestAndResponse_IfResponseIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); using (HttpRequestMessage request = CreateRequest()) { HttpResponseMessage response = null; // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, request, response), "response"); } }
public IHttpActionResult Record(AngularExceptionModel model) { // Create the exception and exception context var exception = new AngularException(model.ToString()); var catchBlock = new ExceptionContextCatchBlock("catchBlock", true, false); var context = new ExceptionContext(exception, catchBlock, Request); var loggerContext = new ExceptionLoggerContext(context); // Call elmah & log the exception var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(loggerContext); // Return return Ok(); }
public void ConstructorWithRequest_IfCatchBlockIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = null; HttpRequestContext requestContext = CreateRequestContext(); using (HttpRequestMessage request = CreateRequest()) { request.SetRequestContext(requestContext); // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, request), "catchBlock"); } }
/// <summary>Determines whether the exception should be handled.</summary> /// <param name="context">The exception handler context.</param> /// <returns> /// <see langword="true"/> if the exception should be handled; otherwise, <see langword="false"/>. /// </returns> /// <remarks>The default decision is only to handle exceptions caught at top-level catch blocks.</remarks> public virtual bool ShouldHandle(ExceptionHandlerContext context) { if (context == null) { throw new ArgumentNullException("context"); } ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock; return(catchBlock.IsTopLevel); }
/// <summary>Initializes a new instance of the <see cref="ExceptionContext"/> class.</summary> /// <param name="exception">The exception caught.</param> /// <param name="catchBlock">The catch block where the exception was caught.</param> /// <remarks>This constructor is for unit testing purposes only.</remarks> public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock) { if (exception == null) { throw new ArgumentNullException("exception"); } Exception = exception; if (catchBlock == null) { throw new ArgumentNullException("catchBlock"); } CatchBlock = catchBlock; }
public void ConstructorWithActionContext_IfExceptionIsNull_Throws() { // Arrange Exception exception = null; ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpRequestContext requestContext = CreateRequestContext(); using (HttpRequestMessage request = CreateRequest()) { HttpControllerContext controllerContext = CreateControllerContext(requestContext, request); HttpActionContext actionContext = CreateActionContext(controllerContext); // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, actionContext), "exception"); } }
public void ConstructorWithRequestAndResponse_IfExceptionIsNull_Throws() { // Arrange Exception exception = null; ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpRequestContext requestContext = CreateRequestContext(); using (HttpRequestMessage request = CreateRequest()) using (HttpResponseMessage response = CreateResponse()) { request.SetRequestContext(requestContext); // Act & Assert Assert.ThrowsArgumentNull(() => CreateProductUnderTest(exception, catchBlock, request, response), "exception"); } }
public void ConstructorWithRequest_IfRequestContextIsNull_IgnoresRequestContext() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); using (HttpRequestMessage request = CreateRequest()) { Assert.Null(request.GetRequestContext()); // Guard // Act ExceptionContext product = CreateProductUnderTest(exception, catchBlock, request); // Assert Assert.Null(product.RequestContext); } }
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken) { if (context != null) { ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock; if (catchBlock != null && catchBlock.IsTopLevel) { context.Result = CreateDefaultLastChanceResult(exceptionContext); } } return(_innerHandler.HandleAsync(context, cancellationToken)); }
public void ConstructorWithActionContext_IfRequestIsNull_Throws() { // Arrange Exception exception = CreateException(); ExceptionContextCatchBlock catchBlock = CreateCatchBlock(); HttpRequestContext requestContext = CreateRequestContext(); HttpControllerContext controllerContext = new HttpControllerContext { RequestContext = requestContext }; Assert.Null(controllerContext.Request); // Guard HttpActionContext actionContext = CreateActionContext(controllerContext); // Act & Assert Assert.ThrowsArgument(() => CreateProductUnderTest(exception, catchBlock, actionContext), "actionContext", "HttpControllerContext.Request must not be null"); }
/// <summary>Determines whether the exception should be handled.</summary> /// <param name="context">The exception handler context.</param> /// <returns> /// <see langword="true"/> if the exception should be handled; otherwise, <see langword="false"/>. /// </returns> /// <remarks>The default decision is only to handle exceptions caught at top-level catch blocks.</remarks> public virtual bool ShouldHandle(ExceptionHandlerContext context) { if (context == null) { throw new ArgumentNullException("context"); } ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock; if (catchBlock == null) { throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, typeof(ExceptionContext), "CatchBlock"), "context"); } return(catchBlock.IsTopLevel); }
internal static async Task<bool> CopyErrorResponseAsync(ExceptionContextCatchBlock catchBlock, HttpContextBase httpContextBase, HttpRequestMessage request, HttpResponseMessage response, Exception exception, IExceptionLogger exceptionLogger, IExceptionHandler exceptionHandler, CancellationToken cancellationToken) { Contract.Assert(httpContextBase != null); Contract.Assert(httpContextBase.Response != null); Contract.Assert(request != null); Contract.Assert(exception != null); Contract.Assert(catchBlock != null); Contract.Assert(catchBlock.CallsHandler); HttpResponseBase httpResponseBase = httpContextBase.Response; HttpResponseMessage errorResponse = null; HttpResponseException responseException = exception as HttpResponseException; // Ensure all headers and content are cleared to eliminate any partial results. ClearContentAndHeaders(httpResponseBase); // If the exception we are handling is HttpResponseException, // that becomes the error response. if (responseException != null) { errorResponse = responseException.Response; } else { ExceptionContext exceptionContext = new ExceptionContext(exception, catchBlock, request) { Response = response }; await exceptionLogger.LogAsync(exceptionContext, cancellationToken); errorResponse = await exceptionHandler.HandleAsync(exceptionContext, cancellationToken); if (errorResponse == null) { return false; } } Contract.Assert(errorResponse != null); if (!await CopyResponseStatusAndHeadersAsync(httpContextBase, request, errorResponse, exceptionLogger, cancellationToken)) { // Don't rethrow the original exception unless explicitly requested to do so. In this case, the // exception handler indicated it wanted to handle the exception; it simply failed create a stable // response to send. return true; } // The error response may return a null content if content negotiation // fails to find a formatter, or this may be an HttpResponseException without // content. In either case, cleanup and return a completed task. if (errorResponse.Content == null) { errorResponse.Dispose(); return true; } CopyHeaders(errorResponse.Content.Headers, httpContextBase); await WriteErrorResponseContentAsync(httpResponseBase, request, errorResponse, cancellationToken, exceptionLogger); return true; }
public void HttpWebRoute_IsSpecifiedValue() { // Act ExceptionContextCatchBlock catchBlock = WebHostExceptionCatchBlocks.HttpWebRoute; // Assert ExceptionContextCatchBlock expected = new ExceptionContextCatchBlock("HttpWebRoute", isTopLevel: true, callsHandler: true); AssertEqual(expected, catchBlock); }
private static void AssertEqual(ExceptionContextCatchBlock expected, ExceptionContextCatchBlock actual) { if (expected == null) { Assert.Null(actual); return; } Assert.NotNull(actual); Assert.Equal(expected.Name, actual.Name); Assert.Equal(expected.IsTopLevel, actual.IsTopLevel); Assert.Equal(expected.CallsHandler, actual.CallsHandler); }
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request, ExceptionContextCatchBlock catchBlock) { return CreateContext(CreateMinimalValidExceptionContext(catchBlock, request)); }
private static ExceptionContext CreateProductUnderTest(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request, HttpResponseMessage response) { return new ExceptionContext(exception, catchBlock, request, response); }
private static ExceptionContext CreateMinimalValidExceptionContext(ExceptionContextCatchBlock catchBlock, HttpRequestMessage request = null) { return new ExceptionContext(new InvalidOperationException(), catchBlock) { Request = request, }; }
private static ExceptionContext CreateProductUnderTest(Exception exception, ExceptionContextCatchBlock catchBlock, HttpActionContext actionContext) { return new ExceptionContext(exception, catchBlock, actionContext); }