Exemple #1
0
        public async SystemTask.Task Invoke(HttpContext context)
        {
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            var ex = context.Features.Get <IExceptionHandlerFeature>()?.Error;

            if (ex == null)
            {
                return;
            }

            var ooException = new HttpFhirException();

            OperationOutcome.IssueComponent fhirIssue = null;
            var issue = new Issue
            {
                Message = "An unkown error has occured."
            };

            if (ex.GetType() == typeof(HttpFhirException))
            {
                ooException = ex as HttpFhirException;
                fhirIssue   = ooException.OperationOutcome?.Issue.FirstOrDefault();
            }

            if (fhirIssue != null)
            {
                issue.Message  = fhirIssue?.Code.ToString();
                issue.Severity = EnumHelpers.GetEnum <IssueSeverity>(fhirIssue.Severity.ToString());
                issue.Details  = fhirIssue.Details;
            }

            if (_env.IsDevelopment())
            {
                issue.Diagnostics = $"Message: {ex.Message}. StackTrace: {ex.StackTrace}.";

                if (ex.InnerException != null)
                {
                    issue.Diagnostics = $"{issue.Diagnostics}. Inner Exception Stack Trace: {ex.InnerException.StackTrace}";
                }

                if (fhirIssue != null)
                {
                    issue.Diagnostics = fhirIssue?.Diagnostics;
                }
            }

            context.Response.ContentType = "application/json";

            using (var writer = new StreamWriter(context.Response.Body))
            {
                new JsonSerializer().Serialize(writer, issue);
                await writer.FlushAsync().ConfigureAwait(false);
            }
        }
Exemple #2
0
        public async SystemTasks.Task Invoke(HttpContext context)
        {
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;

            HttpFhirException ooException = null;

            var ex = context.Features.Get <IExceptionHandlerFeature>()?.Error;

            if (ex == null)
            {
                return;
            }

            if (ex.GetType() == typeof(HttpFhirException))
            {
                ooException = ex as HttpFhirException;
                context.Response.StatusCode = (int)ooException.StatusCode;
            }

            var diagnostics = "An unknown error has occured.";

            if (_env.IsDevelopment())
            {
                diagnostics = $"Message: {ex.Message}. StackTrace: {ex.StackTrace}";

                if (ex.InnerException != null)
                {
                    diagnostics = $"Message: {ex.Message}. StackTrace: {ex.StackTrace}. Inner Exception Stack Trace: {ex.InnerException.StackTrace}";
                }
            }

            var    outcome = ooException?.OperationOutcome != null ? ooException.OperationOutcome : OperationOutcomeFactory.CreateInternalError(diagnostics);
            string output;

            if (UseJsonOutput(context))
            {
                output = new FhirJsonSerializer().SerializeToString(outcome);
                context.Response.ContentType = $"{ContentType.JSON_CONTENT_HEADER}; charset={Encoding.UTF8.WebName}";
            }
            else
            {
                output = new FhirXmlSerializer().SerializeToString(outcome);
                output = Regex.Replace(output, @"((\s){1})(/>)", "/>", RegexOptions.Compiled);
                context.Response.ContentType = $"{ContentType.XML_CONTENT_HEADER}; charset={Encoding.UTF8.WebName}";
            }

            await context.Response.WriteAsync(output, Encoding.UTF8);

            return;
        }