/// <summary>Handles an exception when processing a batch response.</summary> /// <param name='service'>Data service doing the processing.</param> /// <param name="host">host to which we need to write the exception message</param> /// <param name='exception'>Exception thrown.</param> /// <param name='writer'>Output writer for the batch.</param> internal static void HandleBatchProcessException(IDataService service, DataServiceHostWrapper host, Exception exception, StreamWriter writer) { Debug.Assert(service != null, "service != null"); Debug.Assert(host != null, "host != null"); Debug.Assert(exception != null, "exception != null"); Debug.Assert(writer != null, "writer != null"); Debug.Assert(service.Configuration != null, "service.Configuration != null"); Debug.Assert(WebUtil.IsCatchableExceptionType(exception), "WebUtil.IsCatchableExceptionType(exception)"); string contentType; Encoding encoding; TryGetResponseFormatForError(host, out contentType, out encoding); HandleExceptionArgs args = new HandleExceptionArgs(exception, false, contentType, service.Configuration.UseVerboseErrors); service.InternalHandleException(args); host.ResponseVersion = XmlConstants.DataServiceVersion1Dot0 + ";"; host.ProcessException(args); writer.Flush(); Action <Stream> errorWriter = ProcessBenignException(exception, service); if (errorWriter == null) { errorWriter = CreateErrorSerializer(args, encoding); } errorWriter(writer.BaseStream); writer.WriteLine(); }
/// <summary>Handles an exception before the response has been written out.</summary> /// <param name='exception'>Exception thrown.</param> /// <param name='service'>Data service doing the processing.</param> /// <param name='accept'>'Accept' header value; possibly null.</param> /// <param name='acceptCharset'>'Accept-Charset' header; possibly null.</param> /// <returns>An action that can serialize the exception into a stream.</returns> internal static Action <Stream> HandleBeforeWritingException(Exception exception, IDataService service, string accept, string acceptCharset) { Debug.Assert(WebUtil.IsCatchableExceptionType(exception), "WebUtil.IsCatchableExceptionType(exception)"); Debug.Assert(exception != null, "exception != null"); Debug.Assert(service != null, "service != null"); string contentType; Encoding encoding; TryGetResponseFormatForError(accept, acceptCharset, out contentType, out encoding); bool verbose = (service.Configuration != null) ? service.Configuration.UseVerboseErrors : false; HandleExceptionArgs args = new HandleExceptionArgs(exception, false, contentType, verbose); service.InternalHandleException(args); DataServiceHostWrapper host = service.OperationContext.Host; host.ResponseVersion = XmlConstants.DataServiceVersion1Dot0 + ";"; host.ProcessException(args); Action <Stream> action = ProcessBenignException(exception, service); if (action != null) { return(action); } else { return(CreateErrorSerializer(args, encoding)); } }
internal static void HandleBatchOperationError(IDataService service, DataServiceHostWrapper host, Exception exception, ODataBatchWriter batchWriter, Stream responseStream, Version defaultResponseVersion) { string str; Encoding encoding; string str2; Version version; TryGetResponseFormatForError(service, host, defaultResponseVersion, out str, out encoding, out str2, out version); HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, service.Configuration.UseVerboseErrors); service.InternalHandleException(args); Action <Stream> action = null; if (host != null) { host.ResponseVersion = version.ToString(2) + ";"; host.ProcessException(args); action = ProcessBenignException(exception, service); } if (action == null) { ODataBatchOperationResponseMessage operationResponseMessage; if (host != null) { operationResponseMessage = host.BatchServiceHost.GetOperationResponseMessage(); WebUtil.SetResponseHeadersForBatchRequests(operationResponseMessage, host.BatchServiceHost); } else { operationResponseMessage = batchWriter.CreateOperationResponseMessage(); operationResponseMessage.StatusCode = args.ResponseStatusCode; } using (ODataMessageWriter writer = ResponseBodyWriter.CreateMessageWriter(null, service, version, operationResponseMessage, str, null)) { SerializeODataError(args, writer, responseStream, encoding); } } }
internal static Action <Stream> HandleBeforeWritingException(Exception exception, IDataService service) { string str; Encoding encoding; string str2; Version responseVersion = null; DataServiceHostWrapper host = service.OperationContext.Host; TryGetResponseFormatForError(service, host, RequestDescription.DataServiceDefaultResponseVersion, out str, out encoding, out str2, out responseVersion); bool verboseResponse = (service.Configuration != null) ? service.Configuration.UseVerboseErrors : false; HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, verboseResponse); service.InternalHandleException(args); host.ResponseVersion = responseVersion.ToString(2) + ";"; host.ProcessException(args); Action <Stream> action = ProcessBenignException(exception, service); ODataFormat atom = ODataFormat.Atom; if (WebUtil.GetContentFormat(str) == ContentFormat.VerboseJson) { atom = ODataFormat.VerboseJson; } return(action ?? CreateErrorSerializer(atom, args, encoding)); }