Beispiel #1
0
            public void ProvideFault(Exception exception, MessageVersion version, ref Message fault)
            {
                if (exception.GetType() == typeof(HttpException))
                {
                    var httpException = (HttpException)exception;

                    var inResponse = WebOperationContext.Current.IncomingRequest;

                    var webGetAttribute = GetCurrentAttribute(inResponse);

                    if (webGetAttribute == null)
                    {
                        throw new RestException();
                    }

                    var currentResponseFormat = webGetAttribute.ResponseFormat;

                    var restErrorMessage = new RestErrorMessage(httpException.Data, httpException.GetHttpCode(), httpException.ErrorCode);
                    fault = CreateMessage(restErrorMessage, version, currentResponseFormat);
                    fault.Properties.Add(WebBodyFormatMessageProperty.Name, GetBodyFormat(currentResponseFormat));

                    var outResponse = WebOperationContext.Current.OutgoingResponse;
                    outResponse.StatusCode  = (HttpStatusCode)httpException.GetHttpCode();
                    outResponse.ContentType = string.Format("application/{0}", currentResponseFormat);
                }
                else
                {
                    throw new RestException();
                }
            }
Beispiel #2
0
 private static Message CreateMessage(RestErrorMessage restErrorMessage, MessageVersion version, WebMessageFormat webMessageFormat)
 {
     if (webMessageFormat == WebMessageFormat.Json)
     {
         return(Message.CreateMessage(version, null, restErrorMessage, new DataContractJsonSerializer(restErrorMessage.GetType())));
     }
     if (webMessageFormat == WebMessageFormat.Xml)
     {
         return(Message.CreateMessage(version, null, restErrorMessage));
     }
     return(null);
 }