/// <summary> /// Process the exception happened on rest call. /// </summary> /// <param name="exception">Exception object.</param> private void HandleException(Exception exception) { WebException webException = exception as WebException; if (webException != null && webException.Response != null) { if (webException.Response.ContentType.ToLower().Contains("application/json")) { Stream stream = null; try { stream = webException.Response.GetResponseStream(); if (stream != null) { string errorObjectString; using (StreamReader reader = new StreamReader(stream)) { stream = null; errorObjectString = reader.ReadToEnd(); } ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString); // HandwritingOcr error message use the latest format, so add the logic to handle this issue. if (errorCollection.Code == null && errorCollection.Message == null) { var errorType = new { Error = new ClientError() }; var errorObj = JsonConvert.DeserializeAnonymousType(errorObjectString, errorType); errorCollection = errorObj.Error; } if (errorCollection != null) { throw new ClientException { Error = errorCollection, }; } } } finally { if (stream != null) { stream.Dispose(); } } } } throw exception; }
/// <summary> /// Process the exception happened on rest call. /// </summary> /// <param name="exception">Exception object.</param> private void HandleException(Exception exception) { WebException webException = exception as WebException; if (webException != null && webException.Response != null) { if (webException.Response.ContentType.ToLower().Contains("application/json")) { Stream stream = null; try { stream = webException.Response.GetResponseStream(); if (stream != null) { string errorObjectString; using (StreamReader reader = new StreamReader(stream)) { stream = null; errorObjectString = reader.ReadToEnd(); } ClientError errorCollection = JsonConvert.DeserializeObject <ClientError>(errorObjectString); if (errorCollection != null) { throw new ClientException { Error = errorCollection, }; } } } finally { if (stream != null) { stream.Dispose(); } } } } throw exception; }
/// <summary> /// Initializes a new instance of the <see cref="ClientException"/> class. /// </summary> /// <param name="error">The error entity.</param> /// <param name="httpStatus">The http status.</param> public ClientException(ClientError error, HttpStatusCode httpStatus) { this.Error = error; this.HttpStatus = httpStatus; }
/// <summary> /// Initializes a new instance of the <see cref="ClientException"/> class. /// </summary> /// <param name="error">The error entity.</param> /// <param name="httpStatus">The http status.</param> public ClientException(ClientError error, HttpStatusCode httpStatus) { this.Error = error; this.HttpStatus = httpStatus; }