private static Exception buildFhirOperationException(HttpStatusCode status, Resource body)
        {
            string message;

            if (status.IsInformational())
            {
                message = $"Operation resulted in an informational response ({status})";
            }
            else if (status.IsRedirection())
            {
                message = $"Operation resulted in a redirection response ({status})";
            }
            else if (status.IsClientError())
            {
                message = $"Operation was unsuccessful because of a client error ({status})";
            }
            else
            {
                message = $"Operation was unsuccessful, and returned status {status}";
            }

            if (body is OperationOutcome outcome)
            {
                return(new FhirOperationException($"{message}. OperationOutcome: {outcome.ToString()}.", status, outcome));
            }
            else if (body != null)
            {
                return(new FhirOperationException($"{message}. Body contains a {body.TypeName}.", status));
            }
            else
            {
                return(new FhirOperationException($"{message}. Body has no content.", status));
            }
        }
 public static bool IsErrorStatus(this HttpStatusCode statusCode)
 {
     if (statusCode.IsClientError())
     {
         return(true);
     }
     if (statusCode.IsServerError())
     {
         return(true);
     }
     return(false);
 }
Exemple #3
0
 /// <summary>
 /// Convert a status code into an exception, or null if everything is fine.
 /// </summary>
 /// <param name="status">HTTP status code</param>
 /// <param name="body">Content delivered by the server, parsed as a FHIR resource</param>
 /// <returns></returns>
 private static Exception httpNonSuccessStatusToException(HttpStatusCode status, Resource body)
 {
     if (status.IsInformational() || status.IsRedirection())      // 1xx and 3xx codes - we don't handle them, unless the .NET API did it for us
     {
         return(Error.NotSupported("Server returned a status code '{0}', which is not supported by the FhirClient".FormatWith(status)));
     }
     else if (status.IsClientError() || status.IsServerError())      // 4xx/5xx codes - client or server error.
     {
         return(buildFhirOperationException(status, body));
     }
     else
     {
         return(Error.NotSupported("Server returned an illegal http status code '{0}', which is not defined by the Http standard".FormatWith(status)));
     }
 }
 public void IsClientError(bool expected, HttpStatusCode httpStatusCode)
 => Assert.Equal(expected, httpStatusCode.IsClientError());