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 }); }
private static Task <HttpResponseMessage> SendAsyncRequestAcceptedException(HttpRequestMessage request, CancellationToken cancellationToken) { var requestId = Guid.NewGuid().ToString(); var accept = new RequestAcceptedContent() { RequestId = requestId, PollingUrl = $"Polling url for {requestId}", RegisterCallbackUrl = $"Register callback url for {requestId}" }; var response = new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent(JsonConvert.SerializeObject(accept), Encoding.UTF8) }; return(Task.FromResult(response)); }