| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Runtime.Serialization; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure |
| | 9 | | { |
| | 10 | | #pragma warning disable CA2229, CA2235 // False positive |
| | 11 | | /// <summary> |
| | 12 | | /// An exception thrown when service request fails. |
| | 13 | | /// </summary> |
| | 14 | | [Serializable] |
| | 15 | | public class RequestFailedException : Exception, ISerializable |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Gets the HTTP status code of the response. Returns. <code>0</code> if response was not received. |
| | 19 | | /// </summary> |
| 18 | 20 | | public int Status { get; } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Gets the service specific error code if available. Please refer to the client documentation for the list of |
| | 24 | | /// </summary> |
| 6 | 25 | | public string? ErrorCode { get; } |
| | 26 | |
|
| | 27 | | /// <summary>Initializes a new instance of the <see cref="RequestFailedException"></see> class with a specified |
| | 28 | | /// <param name="message">The message that describes the error.</param> |
| 0 | 29 | | public RequestFailedException(string message) : this(0, message) |
| | 30 | | { |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary>Initializes a new instance of the <see cref="RequestFailedException"></see> class with a specified |
| | 34 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 35 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference ( |
| 8 | 36 | | public RequestFailedException(string message, Exception? innerException) : this(0, message, innerException) |
| | 37 | | { |
| 8 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <summary>Initializes a new instance of the <see cref="RequestFailedException"></see> class with a specified |
| | 41 | | /// <param name="status">The HTTP status code, or <c>0</c> if not available.</param> |
| | 42 | | /// <param name="message">The message that describes the error.</param> |
| | 43 | | public RequestFailedException(int status, string message) |
| 4 | 44 | | : this(status, message, null) |
| | 45 | | { |
| 4 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary>Initializes a new instance of the <see cref="RequestFailedException"></see> class with a specified |
| | 49 | | /// <param name="status">The HTTP status code, or <c>0</c> if not available.</param> |
| | 50 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 51 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference ( |
| | 52 | | public RequestFailedException(int status, string message, Exception? innerException) |
| 12 | 53 | | : this(status, message, null, innerException) |
| | 54 | | { |
| 12 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary>Initializes a new instance of the <see cref="RequestFailedException"></see> class with a specified |
| | 58 | | /// <param name="status">The HTTP status code, or <c>0</c> if not available.</param> |
| | 59 | | /// <param name="message">The error message that explains the reason for the exception.</param> |
| | 60 | | /// <param name="errorCode">The service specific error code.</param> |
| | 61 | | /// <param name="innerException">The exception that is the cause of the current exception, or a null reference ( |
| | 62 | | public RequestFailedException(int status, string message, string? errorCode, Exception? innerException) |
| 28 | 63 | | : base(message, innerException) |
| | 64 | | { |
| 28 | 65 | | Status = status; |
| 28 | 66 | | ErrorCode = errorCode; |
| 28 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <inheritdoc /> |
| | 70 | | protected RequestFailedException(SerializationInfo info, StreamingContext context) |
| 2 | 71 | | : base(info, context) |
| | 72 | | { |
| 2 | 73 | | Status = info.GetInt32(nameof(Status)); |
| 2 | 74 | | ErrorCode = info.GetString(nameof(ErrorCode)); |
| 2 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <inheritdoc /> |
| | 78 | | public override void GetObjectData(SerializationInfo info, StreamingContext context) |
| | 79 | | { |
| 2 | 80 | | Argument.AssertNotNull(info, nameof(info)); |
| | 81 | |
|
| 2 | 82 | | info.AddValue(nameof(Status), Status); |
| 2 | 83 | | info.AddValue(nameof(ErrorCode), ErrorCode); |
| | 84 | |
|
| 2 | 85 | | base.GetObjectData(info, context); |
| 2 | 86 | | } |
| | 87 | | } |
| | 88 | | } |