|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using SendGrid.Helpers.Errors.Model; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace SendGrid.Helpers.Errors |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Error handler for requests. |
| 10 | + /// </summary> |
| 11 | + public class ErrorHandler |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Throw the exception based on HttpResponseMessage |
| 15 | + /// </summary> |
| 16 | + /// <param name="message">Response Message from API</param> |
| 17 | + /// <returns>Return the exception. Obs: Exceptions from an Async Void Method Can’t Be Caught with Catch </returns> |
| 18 | + public static async Task ThrowException(HttpResponseMessage message) |
| 19 | + { |
| 20 | + var errorMessage = await ErrorHandler.GetErrorMessage(message).ConfigureAwait(false); |
| 21 | + |
| 22 | + var errorStatusCode = (int)message.StatusCode; |
| 23 | + |
| 24 | + switch (errorStatusCode) |
| 25 | + { |
| 26 | + // 400 - BAD REQUEST |
| 27 | + case 400: |
| 28 | + throw new BadRequestException(errorMessage); |
| 29 | + |
| 30 | + // 401 - UNAUTHORIZED |
| 31 | + case 401: |
| 32 | + throw new UnauthorizedException(errorMessage); |
| 33 | + |
| 34 | + // 403 - FORBIDDEN |
| 35 | + case 403: |
| 36 | + throw new ForbiddenException(errorMessage); |
| 37 | + |
| 38 | + // 404 - NOT FOUND |
| 39 | + case 404: |
| 40 | + throw new NotFoundException(errorMessage); |
| 41 | + |
| 42 | + // 405 - METHOD NOT ALLOWED |
| 43 | + case 405: |
| 44 | + throw new MethodNotAllowedException(errorMessage); |
| 45 | + |
| 46 | + // 413 - PAYLOAD TOO LARGE |
| 47 | + case 413: |
| 48 | + throw new PayloadTooLargeException(errorMessage); |
| 49 | + |
| 50 | + // 415 - UNSUPPORTED MEDIA TYPE |
| 51 | + case 415: |
| 52 | + throw new UnsupportedMediaTypeException(errorMessage); |
| 53 | + |
| 54 | + // 429 - TOO MANY REQUESTS |
| 55 | + case 429: |
| 56 | + throw new TooManyRequestsException(errorMessage); |
| 57 | + |
| 58 | + // 500 - SERVER UNAVAILABLE |
| 59 | + case 500: |
| 60 | + throw new ServerUnavailableException(errorMessage); |
| 61 | + |
| 62 | + // 503 - SERVICE NOT AVAILABLE |
| 63 | + case 503: |
| 64 | + throw new ServiceNotAvailableException(errorMessage); |
| 65 | + } |
| 66 | + |
| 67 | + // 4xx - Error with the request |
| 68 | + if (errorStatusCode >= 400 && errorStatusCode < 500) |
| 69 | + { |
| 70 | + throw new RequestErrorException(errorMessage); |
| 71 | + } |
| 72 | + |
| 73 | + // 5xx - Error made by SendGrid |
| 74 | + if (errorStatusCode >= 500) |
| 75 | + { |
| 76 | + throw new SendGridInternalException(errorMessage); |
| 77 | + } |
| 78 | + |
| 79 | + throw new BadRequestException(errorMessage); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Get error based on Response from SendGrid API |
| 84 | + /// Method taken from the StrongGrid project (https://github.com/Jericho/StrongGrid) with some minor changes. Thanks Jericho (https://github.com/Jericho) |
| 85 | + /// </summary> |
| 86 | + /// <param name="message">Response Message from API</param> |
| 87 | + /// <returns>Return string with the error Status Code and the Message</returns> |
| 88 | + private static async Task<string> GetErrorMessage(HttpResponseMessage message) |
| 89 | + { |
| 90 | + var errorStatusCode = (int)message.StatusCode; |
| 91 | + var errorReasonPhrase = message.ReasonPhrase; |
| 92 | + |
| 93 | + string errorValue = null; |
| 94 | + string fieldValue = null; |
| 95 | + string helpValue = null; |
| 96 | + |
| 97 | + if (message.Content != null) |
| 98 | + { |
| 99 | + var responseContent = await message.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 100 | + |
| 101 | + if (!string.IsNullOrEmpty(responseContent)) |
| 102 | + { |
| 103 | + try |
| 104 | + { |
| 105 | + // Check for the presence of property called 'errors' |
| 106 | + var jObject = JObject.Parse(responseContent); |
| 107 | + var errorsArray = (JArray)jObject["errors"]; |
| 108 | + if (errorsArray != null && errorsArray.Count > 0) |
| 109 | + { |
| 110 | + // Get the first error message |
| 111 | + errorValue = errorsArray[0]["message"].Value<string>(); |
| 112 | + |
| 113 | + // Check for the presence of property called 'field' |
| 114 | + if (errorsArray[0]["field"] != null) |
| 115 | + { |
| 116 | + fieldValue = errorsArray[0]["field"].Value<string>(); |
| 117 | + } |
| 118 | + |
| 119 | + // Check for the presence of property called 'help' |
| 120 | + if (errorsArray[0]["help"] != null) |
| 121 | + { |
| 122 | + helpValue = errorsArray[0]["help"].Value<string>(); |
| 123 | + } |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + // Check for the presence of property called 'error' |
| 128 | + var errorProperty = jObject["error"]; |
| 129 | + if (errorProperty != null) |
| 130 | + { |
| 131 | + errorValue = errorProperty.Value<string>(); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + catch |
| 136 | + { |
| 137 | + // Intentionally ignore parsing errors to return default error message |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + SendGridErrorResponse errorResponse = new SendGridErrorResponse |
| 143 | + { |
| 144 | + ErrorHttpStatusCode = errorStatusCode, |
| 145 | + ErrorReasonPhrase = errorReasonPhrase, |
| 146 | + SendGridErrorMessage = errorValue, |
| 147 | + FieldWithError = fieldValue, |
| 148 | + HelpLink = helpValue |
| 149 | + }; |
| 150 | + |
| 151 | + return Newtonsoft.Json.JsonConvert.SerializeObject(errorResponse); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments