private void BuildODataError(out ODataError error, out HttpStatusCode statusCode) { // Default to 500 (InternalServerError), it will be overried below if necessary. statusCode = HttpStatusCode.InternalServerError; ODataServiceException ose = this.HandledException as ODataServiceException; if (ose != null) { statusCode = ose.StatusCode; } ODataContentTypeException octe = this.HandledException as ODataContentTypeException; if (octe != null) { statusCode = HttpStatusCode.UnsupportedMediaType; } ODataUnrecognizedPathException oupe = this.HandledException as ODataUnrecognizedPathException; if (oupe != null) { statusCode = HttpStatusCode.NotFound; } ODataErrorException oee = this.HandledException as ODataErrorException; if (oee != null) { error = this.BuildODataError(statusCode, this.HandledException); if (!string.IsNullOrEmpty(oee.Error.ErrorCode)) { error.ErrorCode = oee.Error.ErrorCode; } if (!string.IsNullOrEmpty(oee.Error.Message)) { error.Message = oee.Error.Message; } if (oee.Error.InnerError != null) { error.InnerError = oee.Error.InnerError; } } else { // All the other exception will go here. error = this.BuildODataError(statusCode, this.HandledException); } }
internal bool HandleException(Exception exception) { Nullable <HttpStatusCode> httpStatusCode = null; DataServiceRequestException requestException = exception as DataServiceRequestException; if (requestException != null) { OperationResponse opResponse = requestException.Response.FirstOrDefault(); httpStatusCode = opResponse != null ? (HttpStatusCode)opResponse.StatusCode : (HttpStatusCode)requestException.Response.BatchStatusCode; } DataServiceClientException clientException = exception as DataServiceClientException; if (!httpStatusCode.HasValue && clientException != null) { httpStatusCode = (HttpStatusCode)clientException.StatusCode; } DataServiceQueryException queryException = exception as DataServiceQueryException; if (!httpStatusCode.HasValue && queryException != null) { httpStatusCode = (HttpStatusCode)queryException.Response.StatusCode; } ODataErrorException odataException = exception as ODataErrorException; if (!httpStatusCode.HasValue && odataException != null) { var _errorCode = odataException.Error; } if (exception is AdalException || exception is ODataErrorException) { return(false); } return(true); }
public void ReadErrorMetadataDocumentThrows() { string payload = @"<?xml version=""1.0"" encoding=""utf-8""?> <m:error xmlns:m=""http://docs.oasis-open.org/odata/ns/metadata""> <m:code>code42</m:code> <m:message>message text</m:message> </m:error>"; Dictionary <string, string> map = new Dictionary <string, string>() { { "main", payload } }; Action test = () => this.ReadMetadataDocument(map, "main"); ODataErrorException exception = Assert.Throws <ODataErrorException>(test); Assert.Equal("An error was read from the payload. See the 'Error' property for more details.", exception.Message); Assert.Equal("code42", exception.Error.ErrorCode); Assert.Equal("message text", exception.Error.Message); }