|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | +// |
| 5 | +// Project Oxford: http://ProjectOxford.ai |
| 6 | +// |
| 7 | +// ProjectOxford SDK Github: |
| 8 | +// https://github.com/Microsoft/ProjectOxfordSDK-Windows |
| 9 | +// |
| 10 | +// Copyright (c) Microsoft Corporation |
| 11 | +// All rights reserved. |
| 12 | +// |
| 13 | +// MIT License: |
| 14 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 15 | +// a copy of this software and associated documentation files (the |
| 16 | +// "Software"), to deal in the Software without restriction, including |
| 17 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 18 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 19 | +// permit persons to whom the Software is furnished to do so, subject to |
| 20 | +// the following conditions: |
| 21 | +// |
| 22 | +// The above copyright notice and this permission notice shall be |
| 23 | +// included in all copies or substantial portions of the Software. |
| 24 | +// |
| 25 | +// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, |
| 26 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 29 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 30 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | +// |
| 33 | + |
| 34 | +using System; |
| 35 | +using System.Net; |
| 36 | + |
| 37 | +namespace Microsoft.ProjectOxford.Face |
| 38 | +{ |
| 39 | + /// <summary> |
| 40 | + /// The Exception will be shown to client. |
| 41 | + /// </summary> |
| 42 | + public class ClientException : Exception |
| 43 | + { |
| 44 | + /// <summary> |
| 45 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 46 | + /// </summary> |
| 47 | + public ClientException() |
| 48 | + : base() |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 54 | + /// </summary> |
| 55 | + /// <param name="message">The corresponding error message.</param> |
| 56 | + public ClientException(string message) |
| 57 | + : base(message) |
| 58 | + { |
| 59 | + this.Error = new ClientError() |
| 60 | + { |
| 61 | + Code = HttpStatusCode.InternalServerError.ToString(), |
| 62 | + Message = message |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 68 | + /// </summary> |
| 69 | + /// <param name="message">The corresponding error message.</param> |
| 70 | + /// <param name="httpStatus">The Http Status code.</param> |
| 71 | + public ClientException(string message, HttpStatusCode httpStatus) |
| 72 | + : base(message) |
| 73 | + { |
| 74 | + this.HttpStatus = httpStatus; |
| 75 | + |
| 76 | + this.Error = new ClientError() |
| 77 | + { |
| 78 | + Code = this.HttpStatus.ToString(), |
| 79 | + Message = message |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 85 | + /// </summary> |
| 86 | + /// <param name="message">The corresponding error message.</param> |
| 87 | + /// <param name="innerException">The inner exception.</param> |
| 88 | + public ClientException(string message, Exception innerException) |
| 89 | + : base(message, innerException) |
| 90 | + { |
| 91 | + this.Error = new ClientError() |
| 92 | + { |
| 93 | + Code = HttpStatusCode.InternalServerError.ToString(), |
| 94 | + Message = message |
| 95 | + }; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 100 | + /// </summary> |
| 101 | + /// <param name="message">The corresponding error message.</param> |
| 102 | + /// <param name="errorCode">The error code.</param> |
| 103 | + /// <param name="httpStatus">The http status.</param> |
| 104 | + /// <param name="innerException">The inner exception.</param> |
| 105 | + public ClientException(string message, string errorCode, HttpStatusCode httpStatus, Exception innerException) |
| 106 | + : base(message, innerException) |
| 107 | + { |
| 108 | + this.HttpStatus = httpStatus; |
| 109 | + |
| 110 | + this.Error = new ClientError() |
| 111 | + { |
| 112 | + Code = errorCode, |
| 113 | + Message = message |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Initializes a new instance of the <see cref="ClientException"/> class. |
| 119 | + /// </summary> |
| 120 | + /// <param name="error">The error entity.</param> |
| 121 | + /// <param name="httpStatus">The http status.</param> |
| 122 | + public ClientException(ClientError error, HttpStatusCode httpStatus) |
| 123 | + { |
| 124 | + this.Error = error; |
| 125 | + this.HttpStatus = httpStatus; |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Gets http status of http response. |
| 130 | + /// </summary> |
| 131 | + /// <value> |
| 132 | + /// The HTTP status. |
| 133 | + /// </value> |
| 134 | + public HttpStatusCode HttpStatus { get; private set; } |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Gets or sets the httpError message. |
| 138 | + /// </summary> |
| 139 | + /// <value> |
| 140 | + /// The error. |
| 141 | + /// </value> |
| 142 | + public ClientError Error { get; set; } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Create Client Exception of Bad Request. |
| 146 | + /// </summary> |
| 147 | + /// <param name="message">The corresponding error message.</param> |
| 148 | + /// <returns>Client Exception Instance.</returns> |
| 149 | + public static ClientException BadRequest(string message) |
| 150 | + { |
| 151 | + return new ClientException( |
| 152 | + new ClientError() |
| 153 | + { |
| 154 | + Code = ((int)HttpStatusCode.BadRequest).ToString(), |
| 155 | + Message = message |
| 156 | + }, |
| 157 | + HttpStatusCode.BadRequest); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments