// ------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ namespace Microsoft.Graph { using System; /// /// Graph service exception. /// public class ServiceException : Exception { /// /// Creates a new service exception. /// /// The error that triggered the exception. /// The possible innerException. public ServiceException(Error error, Exception innerException = null) : this(error, responseHeaders: null, statusCode: default(System.Net.HttpStatusCode), innerException: innerException) { } /// /// Creates a new service exception. /// /// The error that triggered the exception. /// The possible innerException. /// The HTTP response headers from the response. /// The HTTP status code from the response. public ServiceException(Error error, System.Net.Http.Headers.HttpResponseHeaders responseHeaders, System.Net.HttpStatusCode statusCode, Exception innerException = null) : base(error?.ToString(), innerException) { this.Error = error; this.ResponseHeaders = responseHeaders; this.StatusCode = statusCode; } /// /// Creates a new service exception. /// /// The error that triggered the exception. /// The possible innerException. /// The HTTP response headers from the response. /// The HTTP status code from the response. /// The raw JSON response body. public ServiceException(Error error, System.Net.Http.Headers.HttpResponseHeaders responseHeaders, System.Net.HttpStatusCode statusCode, string rawResponseBody, Exception innerException = null) : this(error, responseHeaders, statusCode, innerException) { this.RawResponseBody = rawResponseBody; } /// /// The error from the service exception. /// public Error Error { get; } // ResponseHeaders and StatusCode exposed as pass-through. /// /// The HTTP response headers from the response. /// public System.Net.Http.Headers.HttpResponseHeaders ResponseHeaders { get; } /// /// The HTTP status code from the response. /// public System.Net.HttpStatusCode StatusCode { get; } /// /// Provide the raw JSON response body. /// public string RawResponseBody { get; } /// /// Checks if a given error code has been returned in the response at any level in the error stack. /// /// The error code. /// True if the error code is in the stack. public bool IsMatch(string errorCode) { if (string.IsNullOrEmpty(errorCode)) { throw new ArgumentException("errorCode cannot be null or empty", "errorCode"); } var currentError = this.Error; while (currentError != null) { if (string.Equals(currentError.Code, errorCode, StringComparison.OrdinalIgnoreCase)) { return true; } currentError = currentError.InnerError; } return false; } /// public override string ToString() { return $@"Status Code: {this.StatusCode}{Environment.NewLine}{base.ToString()}"; } } }