Example #1
0
        /// <summary>
        /// Extracts error details from an <see cref="ODataError"/>.
        /// </summary>
        /// <param name="error">The ODataError instance to extract the error details from.</param>
        /// <param name="code">A data service-defined string which serves as a substatus to the HTTP response code.</param>
        /// <param name="message">A human readable message describing the error.</param>
        /// <param name="messageLanguage">The language identifier representing the language the error message is in.</param>
        internal static void GetErrorDetails(ODataError error, out string code, out string message, out string messageLanguage)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(error != null, "error != null");

            code = error.ErrorCode ?? string.Empty;
            message = error.Message ?? string.Empty;
            messageLanguage = error.MessageLanguage ?? ErrorUtils.ODataErrorMessageDefaultLanguage;
        }
Example #2
0
 internal static void WriteError(JsonWriter jsonWriter, ODataError error, bool includeDebugInformation, int maxInnerErrorDepth)
 {
     string str;
     string str2;
     string str3;
     ErrorUtils.GetErrorDetails(error, out str, out str2, out str3);
     ODataInnerError innerError = includeDebugInformation ? error.InnerError : null;
     WriteError(jsonWriter, str, str2, str3, innerError, maxInnerErrorDepth);
 }
Example #3
0
 public ODataErrorException(string message, Exception innerException, ODataError error) : base(message, innerException)
 {
     EventHandler<SafeSerializationEventArgs> handler = null;
     this.state.ODataError = error;
     if (handler == null)
     {
         handler = (exception, eventArgs) => eventArgs.AddSerializedState(this.state);
     }
     base.SerializeObjectState += handler;
 }
Example #4
0
        /// <summary>
        /// Write an error message.
        /// </summary>
        /// <param name="writer">The Xml writer to write to.</param>
        /// <param name="error">The error instance to write.</param>
        /// <param name="includeDebugInformation">A flag indicating whether error details should be written (in debug mode only) or not.</param>
        /// <param name="maxInnerErrorDepth">The maximumum number of nested inner errors to allow.</param>
        internal static void WriteXmlError(XmlWriter writer, ODataError error, bool includeDebugInformation, int maxInnerErrorDepth)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(writer != null, "writer != null");
            Debug.Assert(error != null, "error != null");

            string code, message, messageLanguage;
            ErrorUtils.GetErrorDetails(error, out code, out message, out messageLanguage);

            ODataInnerError innerError = includeDebugInformation ? error.InnerError : null;
            WriteXmlError(writer, code, message, messageLanguage, innerError, maxInnerErrorDepth);
        }
        internal static ODataError ReadErrorElement(BufferingXmlReader xmlReader, int maxInnerErrorDepth)
        {
            ODataError error = new ODataError();
            DuplicateErrorElementPropertyBitMask none = DuplicateErrorElementPropertyBitMask.None;
            if (xmlReader.IsEmptyElement)
            {
                return error;
            }
            xmlReader.Read();
        Label_001A:
            switch (xmlReader.NodeType)
            {
                case XmlNodeType.Element:
                    string str;
                    if (xmlReader.NamespaceEquals(xmlReader.ODataMetadataNamespace) && ((str = xmlReader.LocalName) != null))
                    {
                        if (!(str == "code"))
                        {
                            if (str == "message")
                            {
                                VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.Message, "message");
                                error.MessageLanguage = xmlReader.GetAttribute(xmlReader.XmlLangAttributeName, xmlReader.XmlNamespace);
                                error.Message = xmlReader.ReadElementValue();
                                goto Label_00EA;
                            }
                            if (str == "innererror")
                            {
                                VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.InnerError, "innererror");
                                error.InnerError = ReadInnerErrorElement(xmlReader, 0, maxInnerErrorDepth);
                                goto Label_00EA;
                            }
                        }
                        else
                        {
                            VerifyErrorElementNotFound(ref none, DuplicateErrorElementPropertyBitMask.Code, "code");
                            error.ErrorCode = xmlReader.ReadElementValue();
                            goto Label_00EA;
                        }
                    }
                    break;

                case XmlNodeType.EndElement:
                    goto Label_00EA;
            }
            xmlReader.Skip();
        Label_00EA:
            if (xmlReader.NodeType != XmlNodeType.EndElement)
            {
                goto Label_001A;
            }
            return error;
        }
        public void ODataErrorSerializer_Works()
        {
            // Arrange
            ODataErrorSerializer serializer = new ODataErrorSerializer();
            MemoryStream stream = new MemoryStream();
            IODataResponseMessage message = new ODataMessageWrapper(stream);
            ODataError error = new ODataError { Message = "Error!!!" };

            // Act
            serializer.WriteObject(error, new ODataMessageWriter(message), new ODataSerializerContext());

            // Assert
            stream.Seek(0, SeekOrigin.Begin);
            XElement element = XElement.Load(stream);
            Assert.Equal("Error!!!", element.Descendants().Single(e => e.Name.LocalName == "message").Value);
        }
Example #7
0
 private static ODataError CreateODataErrorFromExceptionArgs(HandleExceptionArgs args)
 {
     string str;
     string str2;
     string str3;
     DataServiceException exception = ExtractErrorValues(args.Exception, out str, out str2, out str3);
     ODataError error = new ODataError {
         ErrorCode = str,
         Message = str2,
         MessageLanguage = str3
     };
     if (args.UseVerboseErrors)
     {
         Exception exception2 = (exception == null) ? args.Exception : exception.InnerException;
         error.InnerError = (exception2 == null) ? null : new ODataInnerError(exception2);
     }
     return error;
 }
Example #8
0
        public static HttpResponseException ResourceNotFoundError(HttpRequestMessage request)
        {
            HttpResponseException httpException;
            HttpResponseMessage response;
            ODataError error;
            
            error = new ODataError
            {
                Message = "Resource Not Found - 404",
                ErrorCode = "NotFound"
            };

            response = request.CreateResponse(HttpStatusCode.NotFound, error);

            httpException = new HttpResponseException(response);

            return httpException;
        }
 public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request,
     HttpStatusCode statusCode, ODataError oDataError)
 {
     if (request.ShouldIncludeErrorDetail())
     {
         return request.CreateResponse(statusCode, oDataError);
     }
     else
     {
         return request.CreateResponse(
             statusCode,
             new ODataError()
             {
                 ErrorCode = oDataError.ErrorCode,
                 Message = oDataError.Message,
                 MessageLanguage = oDataError.MessageLanguage
             });
     }
 }
 public static HttpResponseMessage CreateODataErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, ODataError oDataError)
 {
     HttpConfiguration config = request.GetConfiguration();
     if (config != null && ShouldIncludeErrorDetail(config, request))
     {
         return request.CreateResponse(statusCode, oDataError);
     }
     else
     {
         return request.CreateResponse(
             statusCode,
             new ODataError()
             {
                 ErrorCode = oDataError.ErrorCode,
                 Message = oDataError.Message,
                 MessageLanguage = oDataError.MessageLanguage
             });
     }
 }
        public void CreateODataError_Respects_IncludeErrorDetail(IncludeErrorDetailPolicy errorDetail, bool? isLocal, bool? includeErrorDetail, bool detailIncluded)
        {
            HttpConfiguration config = new HttpConfiguration() { IncludeErrorDetailPolicy = errorDetail };
            HttpRequestMessage request = new HttpRequestMessage();
            request.Properties.Add("MS_HttpConfiguration", config);
            if (isLocal.HasValue)
            {
                request.Properties.Add("MS_IsLocal", new Lazy<bool>(() => isLocal.Value));
            }
            if (includeErrorDetail.HasValue)
            {
                request.Properties.Add("MS_IncludeErrorDetail", new Lazy<bool>(() => includeErrorDetail.Value));
            }
            ODataError error = new ODataError()
            {
                ErrorCode = "36",
                Message = "Bad stuff",
                MessageLanguage = "en-US",
                InnerError = new ODataInnerError()
                {
                    Message = "Exception message"
                }
            };

            HttpResponseMessage response = request.CreateODataErrorResponse(HttpStatusCode.BadRequest, error);

            Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            ODataError contentError;
            Assert.True(response.TryGetContentValue<ODataError>(out contentError));
            Assert.Equal("36", contentError.ErrorCode);
            Assert.Equal("Bad stuff", contentError.Message);
            Assert.Equal("en-US", contentError.MessageLanguage);
            if (detailIncluded)
            {
                Assert.NotNull(contentError.InnerError);
                Assert.Equal("Exception message", contentError.InnerError.Message);
            }
            else
            {
                Assert.Null(contentError.InnerError);
            }
        }
Example #12
0
 internal virtual Task WriteInStreamErrorAsync(ODataError error, bool includeDebugInformation)
 {
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
Example #13
0
 /// <summary>
 /// Asynchronously writes an <see cref="ODataError"/> as the message payload.
 /// </summary>
 /// <param name="error">The error to write.</param>
 /// <param name="includeDebugInformation">
 /// A flag indicating whether debug information (e.g., the inner error from the <paramref name="error"/>) should
 /// be included in the payload. This should only be used in debug scenarios.
 /// </param>
 /// <returns>A task representing the asynchronous operation of writing the error.</returns>
 /// <remarks>It is the responsibility of this method to flush the output before the task finishes.</remarks>
 internal virtual Task WriteErrorAsync(ODataError error, bool includeDebugInformation)
 {
     DebugUtils.CheckNoExternalCallers();
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
Example #14
0
 internal override Task WriteInStreamErrorAsync(ODataError error, bool includeDebugInformation)
 {
     if (this.outputInStreamErrorListener != null)
     {
         this.outputInStreamErrorListener.OnInStreamError();
     }
     throw new ODataException(Microsoft.Data.OData.Strings.ODataMessageWriter_CannotWriteInStreamErrorForRawValues);
 }
 internal ODataError ReadTopLevelError()
 {
     base.JsonReader.DisableInStreamErrorDetection = true;
     ODataError error = new ODataError();
     try
     {
         base.ReadPayloadStart(false, false);
         base.JsonReader.ReadStartObject();
         ODataJsonReaderUtils.ErrorPropertyBitMask none = ODataJsonReaderUtils.ErrorPropertyBitMask.None;
         while (base.JsonReader.NodeType == JsonNodeType.Property)
         {
             string strB = base.JsonReader.ReadPropertyName();
             if (string.CompareOrdinal("error", strB) != 0)
             {
                 throw new ODataException(Strings.ODataJsonErrorDeserializer_TopLevelErrorWithInvalidProperty(strB));
             }
             ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.Error, "error");
             base.JsonReader.ReadStartObject();
             while (base.JsonReader.NodeType == JsonNodeType.Property)
             {
                 strB = base.JsonReader.ReadPropertyName();
                 string str2 = strB;
                 if (str2 == null)
                 {
                     goto Label_01B8;
                 }
                 if (!(str2 == "code"))
                 {
                     if (str2 == "message")
                     {
                         goto Label_00D9;
                     }
                     if (str2 == "innererror")
                     {
                         goto Label_019B;
                     }
                     goto Label_01B8;
                 }
                 ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.Code, "code");
                 error.ErrorCode = base.JsonReader.ReadStringValue("code");
                 continue;
             Label_00D9:
                 ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.Message, "message");
                 base.JsonReader.ReadStartObject();
                 while (base.JsonReader.NodeType == JsonNodeType.Property)
                 {
                     strB = base.JsonReader.ReadPropertyName();
                     string str3 = strB;
                     if (str3 == null)
                     {
                         goto Label_0171;
                     }
                     if (!(str3 == "lang"))
                     {
                         if (str3 == "value")
                         {
                             goto Label_014B;
                         }
                         goto Label_0171;
                     }
                     ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.MessageLanguage, "lang");
                     error.MessageLanguage = base.JsonReader.ReadStringValue("lang");
                     continue;
                 Label_014B:
                     ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.MessageValue, "value");
                     error.Message = base.JsonReader.ReadStringValue("value");
                     continue;
                 Label_0171:
                     throw new ODataException(Strings.ODataJsonErrorDeserializer_TopLevelErrorMessageValueWithInvalidProperty(strB));
                 }
                 base.JsonReader.ReadEndObject();
                 continue;
             Label_019B:
                 ODataJsonReaderUtils.VerifyErrorPropertyNotFound(ref none, ODataJsonReaderUtils.ErrorPropertyBitMask.InnerError, "innererror");
                 error.InnerError = this.ReadInnerError(0);
                 continue;
             Label_01B8:
                 throw new ODataException(Strings.ODataJsonErrorDeserializer_TopLevelErrorValueWithInvalidProperty(strB));
             }
             base.JsonReader.ReadEndObject();
         }
         base.JsonReader.ReadEndObject();
         base.ReadPayloadEnd(false, false);
     }
     finally
     {
         base.JsonReader.DisableInStreamErrorDetection = false;
     }
     return error;
 }
        /// <summary>
        /// Writes an <see cref="ODataError"/> into the message payload.
        /// </summary>
        /// <param name="error">The error to write.</param>
        /// <param name="includeDebugInformation">
        /// A flag indicating whether debug information (e.g., the inner error from the <paramref name="error"/>) should 
        /// be included in the payload. This should only be used in debug scenarios.
        /// </param>
        /// <returns>Task which represents the pending write operation.</returns>
        /// <remarks>
        /// This method is called if the ODataMessageWriter.WriteError is called once some other
        /// write operation has already started.
        /// The method should write the in-stream error representation for the specific format into the current payload.
        /// Before the method is called no flush is performed on the output context or any active writer.
        /// It is the responsibility of this method to make sure that all the data up to this point are written before
        /// the in-stream error is written.
        /// It is the responsibility of this method to flush the output before the task finishes.
        /// </remarks>
        internal override Task WriteInStreamErrorAsync(ODataError error, bool includeDebugInformation)
        {
            DebugUtils.CheckNoExternalCallers();
            if (this.outputInStreamErrorListener != null)
            {
                this.outputInStreamErrorListener.OnInStreamError();
            }

            throw new ODataException(Strings.ODataMessageWriter_CannotWriteInStreamErrorForRawValues);
        }
Example #17
0
 /// <summary>Creates a new instance of the <see cref="T:Microsoft.Data.OData.ODataErrorException" /> class with an <see cref="T:Microsoft.Data.OData.ODataError" /> object.</summary>
 /// <param name="error">The <see cref="T:Microsoft.Data.OData.ODataError" /> instance representing the error read from the payload.</param>
 /// <remarks>
 /// The Message property is initialized to a system-supplied message
 /// that describes the error. This message takes into account the
 /// current system culture.
 /// </remarks>
 public ODataErrorException(ODataError error)
     : this(Strings.ODataErrorException_GeneralError, null, error)
 {
 }
 /// <summary>
 /// Asynchronously writes an <see cref="ODataError"/> as the message payload.
 /// </summary>
 /// <param name="error">The error to write.</param>
 /// <param name="includeDebugInformation">
 /// A flag indicating whether debug information (e.g., the inner error from the <paramref name="error"/>) should 
 /// be included in the payload. This should only be used in debug scenarios.
 /// </param>
 /// <returns>A task representing the asynchronous operation of writing the error.</returns>
 /// <remarks>It is the responsibility of this method to flush the output before the task finishes.</remarks>
 internal virtual Task WriteErrorAsync(ODataError error, bool includeDebugInformation)
 {
     DebugUtils.CheckNoExternalCallers();
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
Example #19
0
 internal void WriteTopLevelError(ODataError error, bool includeDebugInformation)
 {
     this.WriteTopLevelPayload(() => ODataJsonWriterUtils.WriteError(this.JsonWriter, error, includeDebugInformation, this.MessageWriterSettings.MessageQuotas.MaxNestingDepth), true);
 }
Example #20
0
 internal static void GetErrorDetails(ODataError error, out string code, out string message, out string messageLanguage)
 {
     code            = error.ErrorCode ?? string.Empty;
     message         = error.Message ?? string.Empty;
     messageLanguage = error.MessageLanguage ?? "en-US";
 }
Example #21
0
 internal virtual void WriteError(ODataError error, bool includeDebugInformation)
 {
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
Example #22
0
 internal virtual Task WriteInStreamErrorAsync(ODataError error, bool includeDebugInformation)
 {
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
 public ErrorResult( string value, HttpRequestMessage request, HttpStatusCode status ) {
     this.value = value;
     this.request = request;
     this.status = status;
     this.err = new ODataError { ErrorCode = "Data Persit Exception", Message = value, MessageLanguage = string.Format( "{0}", Thread.CurrentThread.CurrentCulture ) };
 }
Example #24
0
 internal static void GetErrorDetails(ODataError error, out string code, out string message, out string messageLanguage)
 {
     code = error.ErrorCode ?? string.Empty;
     message = error.Message ?? string.Empty;
     messageLanguage = error.MessageLanguage ?? "en-US";
 }
        /// <summary>
        /// Write a top-level error message.
        /// </summary>
        /// <param name="error">The error instance to write.</param>
        /// <param name="includeDebugInformation">A flag indicating whether error details should be written (in debug mode only) or not.</param>
        internal void WriteTopLevelError(ODataError error, bool includeDebugInformation)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(error != null, "error != null");

            // Top-level error payloads in JSON don't use the "d" wrapper even in responses!
            this.WriteTopLevelPayload(
                () => ODataJsonWriterUtils.WriteError(this.JsonWriter, error, includeDebugInformation, this.MessageWriterSettings.MessageQuotas.MaxNestingDepth),
                /*disableResponseWrapper*/ true);
        }
Example #26
0
 internal virtual void WriteError(ODataError error, bool includeDebugInformation)
 {
     throw this.CreatePayloadKindNotSupportedException(ODataPayloadKind.Error);
 }
 public static HttpResponseMessage CreateODataErrorResponse(this HttpRequestMessage request,
     HttpStatusCode statusCode, ODataError oDataError)
 {
     return Extensions.HttpRequestMessageExtensions.CreateErrorResponse(request, statusCode, oDataError);
 }
Example #28
0
 /// <summary>Creates a new instance of the <see cref="T:Microsoft.Data.OData.ODataErrorException" /> class with an error message and an <see cref="T:Microsoft.Data.OData.ODataError" /> object.</summary>
 /// <param name="message">The plain text error message for this exception.</param>
 /// <param name="error">The <see cref="T:Microsoft.Data.OData.ODataError" /> instance representing the error read from the payload.</param>
 public ODataErrorException(string message, ODataError error)
     : this(message, null, error)
 {
 }
Example #29
0
 internal static void WriteError(XmlWriter writer, ODataError error, bool includeDebugInformation, int maxInnerErrorDepth)
 {
     ErrorUtils.WriteXmlError(writer, error, includeDebugInformation, maxInnerErrorDepth);
 }
Example #30
0
 internal void WriteTopLevelError(ODataError error, bool includeDebugInformation)
 {
     this.WritePayloadStart();
     ODataAtomWriterUtils.WriteError(this.XmlWriter, error, includeDebugInformation, base.MessageWriterSettings.MessageQuotas.MaxNestingDepth);
     this.WritePayloadEnd();
 }
        /// <summary>
        /// Writes an <see cref="ODataError"/> into the message payload.
        /// </summary>
        /// <param name="error">The error to write.</param>
        /// <param name="includeDebugInformation">
        /// A flag indicating whether debug information (e.g., the inner error from the <paramref name="error"/>) should 
        /// be included in the payload. This should only be used in debug scenarios.
        /// </param>
        /// <remarks>
        /// This method is called if the ODataMessageWriter.WriteError is called once some other
        /// write operation has already started.
        /// The method should write the in-stream error representation for the specific format into the current payload.
        /// Before the method is called no flush is performed on the output context or any active writer.
        /// It is the responsibility of this method to flush the output before the method returns.
        /// </remarks>
        internal override void WriteInStreamError(ODataError error, bool includeDebugInformation)
        {
            DebugUtils.CheckNoExternalCallers();
            this.AssertSynchronous();

            ODataAtomWriterUtils.WriteError(this.xmlWriter, error, includeDebugInformation, this.MessageWriterSettings.MessageQuotas.MaxNestingDepth);
            this.Flush();
        }
Example #32
0
 internal override void WriteInStreamError(ODataError error, bool includeDebugInformation)
 {
     this.WriteInStreamErrorImplementation(error, includeDebugInformation);
     this.Flush();
 }
Example #33
0
 internal override void WriteInStreamError(ODataError error, bool includeDebugInformation)
 {
     ODataAtomWriterUtils.WriteError(this.xmlWriter, error, includeDebugInformation, base.MessageWriterSettings.MessageQuotas.MaxNestingDepth);
     this.Flush();
 }
Example #34
0
 internal override Task WriteInStreamErrorAsync(ODataError error, bool includeDebugInformation)
 {
     return TaskUtils.GetTaskForSynchronousOperationReturningTask(delegate {
         this.WriteInStreamErrorImplementation(error, includeDebugInformation);
         return this.FlushAsync();
     });
 }
Example #35
0
 private void WriteInStreamErrorImplementation(ODataError error, bool includeDebugInformation)
 {
     if (this.outputInStreamErrorListener != null)
     {
         this.outputInStreamErrorListener.OnInStreamError();
     }
     ODataJsonWriterUtils.WriteError(this.jsonWriter, error, includeDebugInformation, base.MessageWriterSettings.MessageQuotas.MaxNestingDepth);
 }
Example #36
0
 private void WriteErrorImplementation(ODataError error, bool includeDebugInformation)
 {
     new ODataJsonSerializer(this).WriteTopLevelError(error, includeDebugInformation);
 }
        /// <summary>
        /// Writes a top-level error payload.
        /// </summary>
        /// <param name="error">The error instance to write.</param>
        /// <param name="includeDebugInformation">A flag indicating whether error details should be written (in debug mode only) or not.</param>
        internal void WriteTopLevelError(ODataError error, bool includeDebugInformation)
        {
            Debug.Assert(this.MessageWriterSettings != null, "this.MessageWriterSettings != null");
            DebugUtils.CheckNoExternalCallers();

            this.WritePayloadStart();
            ODataAtomWriterUtils.WriteError(this.XmlWriter, error, includeDebugInformation, this.MessageWriterSettings.MessageQuotas.MaxNestingDepth);
            this.WritePayloadEnd();
        }