public async Task InvokeAsync(HttpContext context) { context.Response.StatusCode = _statusCode; FulcrumException fulcrumException = null; if (_statusCode >= 500) { fulcrumException = new FulcrumAssertionFailedException("Internal error message"); } else if (_statusCode >= 400) { fulcrumException = new FulcrumServiceContractException("Client error message"); } if (_statusCode >= 400) { await context.Response.WriteAsync("Test"); context.Response.Body = new MemoryStream(); context.Response.ContentType = "application/json"; var fulcrumError = ExceptionConverter.ToFulcrumError(fulcrumException); var content = ExceptionConverter.ToJsonString(fulcrumError, Formatting.Indented); await context.Response.WriteAsync(content); } }
public void ExceptionToError() { var fulcrumException = new FulcrumAssertionFailedException(Guid.NewGuid().ToString()) { TechnicalMessage = Guid.NewGuid().ToString(), FriendlyMessage = Guid.NewGuid().ToString(), CorrelationId = Guid.NewGuid().ToString(), Code = Guid.NewGuid().ToString(), ErrorLocation = Guid.NewGuid().ToString(), MoreInfoUrl = Guid.NewGuid().ToString(), RecommendedWaitTimeInSeconds = 100.0, ServerTechnicalName = Guid.NewGuid().ToString() }; var fulcrumError = new FulcrumError(); fulcrumError.CopyFrom(fulcrumException); // Equal UT.Assert.AreEqual(fulcrumError.Code, fulcrumException.Code); UT.Assert.AreEqual(fulcrumError.RecommendedWaitTimeInSeconds, fulcrumException.RecommendedWaitTimeInSeconds); UT.Assert.AreEqual(fulcrumError.ServerTechnicalName, fulcrumException.ServerTechnicalName); UT.Assert.AreEqual(fulcrumError.FriendlyMessage, fulcrumException.FriendlyMessage); UT.Assert.AreEqual(fulcrumError.Type, fulcrumException.Type); UT.Assert.AreEqual(fulcrumError.MoreInfoUrl, fulcrumException.MoreInfoUrl); UT.Assert.AreEqual(fulcrumError.ServerTechnicalName, fulcrumException.ServerTechnicalName); UT.Assert.AreEqual(fulcrumError.CorrelationId, fulcrumException.CorrelationId); UT.Assert.AreEqual(fulcrumError.InstanceId, fulcrumException.InstanceId); UT.Assert.AreEqual(fulcrumError.IsRetryMeaningful, fulcrumException.IsRetryMeaningful); UT.Assert.AreEqual(fulcrumError.ErrorLocation, fulcrumException.ErrorLocation); UT.Assert.AreEqual(fulcrumError.InstanceId, fulcrumException.InstanceId); UT.Assert.AreEqual(fulcrumError.InnerInstanceId, fulcrumException.InnerInstanceId); // Other tests UT.Assert.IsNull(fulcrumException.InnerException); }
private static StatusAndContent ToStatusAndContent(Exception e) { if (e is RequestAcceptedException acceptedException) { var acceptedContent = new RequestAcceptedContent() { RequestId = acceptedException.RequestId, PollingUrl = acceptedException.PollingUrl, RegisterCallbackUrl = acceptedException.RegisterCallbackUrl }; FulcrumAssert.IsValidated(acceptedContent, CodeLocation.AsString()); return(new StatusAndContent { // ReSharper disable once PossibleInvalidOperationException StatusCode = HttpStatusCode.Accepted, Content = JsonConvert.SerializeObject(acceptedContent) }); } if (e is RequestPostponedException postponedException) { var postponedContent = new RequestPostponedContent() { TryAgain = postponedException.TryAgain, WaitingForRequestIds = postponedException.WaitingForRequestIds, ReentryAuthentication = postponedException.ReentryAuthentication }; FulcrumAssert.IsValidated(postponedContent, CodeLocation.AsString()); return(new StatusAndContent { // ReSharper disable once PossibleInvalidOperationException StatusCode = HttpStatusCode.Accepted, Content = JsonConvert.SerializeObject(postponedContent) }); } if (!(e is FulcrumException fulcrumException)) { var message = $"Application threw an exception that didn't inherit from {typeof(FulcrumException)}.\r{e.GetType().FullName}: {e.Message}\rFull exception:\r{e}"; Log.LogError(message, e); fulcrumException = new FulcrumAssertionFailedException(message, e); } var error = ExceptionConverter.ToFulcrumError(fulcrumException, true); var statusCode = ExceptionConverter.ToHttpStatusCode(error); FulcrumAssert.IsNotNull(statusCode, CodeLocation.AsString()); Log.LogVerbose( $"{error.Type} => HTTP status {statusCode}"); var content = ExceptionConverter.ToJsonString(error, Formatting.Indented); return(new StatusAndContent { // ReSharper disable once PossibleInvalidOperationException StatusCode = statusCode.Value, Content = content }); }
public void FulcrumAssertionException() { var message = Guid.NewGuid().ToString(); var exception = new FulcrumAssertionFailedException(message); #if NETCOREAPP var result = AspNetExceptionConverter.ToContentResult(exception); #else var result = AspNetExceptionConverter.ToHttpResponseMessage(exception); #endif // ReSharper disable once PossibleInvalidOperationException Assert.AreEqual((int)HttpStatusCode.InternalServerError, (int)result.StatusCode); }
public void FulcrumException() { var exceptionMessage = Guid.NewGuid().ToString(); var exception = new FulcrumAssertionFailedException(exceptionMessage); try { throw exception; } catch (FulcrumException e) { Assert.IsNotNull(e.StackTrace); var error = ExceptionConverter.ToFulcrumError(exception); Assert.IsNotNull(error); Assert.AreEqual(exceptionMessage, error.TechnicalMessage); Assert.IsNull(error.ErrorLocation, $"Error location was expected to be null, but contained the following: {error.ErrorLocation}"); } }
/// <summary> /// Convert an exception (<paramref name="e"/>) into an HTTP response message. /// </summary> public static HttpResponseMessage ToHttpResponseMessage(Exception e) { InternalContract.RequireNotNull(e, nameof(e)); var error = ToFulcrumError(e); if (error == null) { var message = $"The exception {e.GetType().FullName} was not recognized as a Fulcrum Exception. Message: {e.Message}"; e = new FulcrumAssertionFailedException(message, e); error = ToFulcrumError(e); if (error == null) { return(FatalErrorAsHttpResponse(e)); } } var statusCode = ToHttpStatusCode(error); if (statusCode == null) { var message = $"The Type of the following error could not be converted to an HTTP status code: {ToJsonString(error, Formatting.Indented)}."; e = new FulcrumAssertionFailedException(message, e); error = ToFulcrumError(e); if (error == null) { return(FatalErrorAsHttpResponse(e)); } statusCode = ToHttpStatusCode(error); if (statusCode == null) { return(FatalErrorAsHttpResponse(e)); } } var content = ToJsonString(error, Formatting.Indented); var stringContent = new StringContent(content, Encoding.UTF8); var response = new HttpResponseMessage(statusCode.Value) { Content = stringContent }; return(response); }