// Copyright (c) Richasy. All rights reserved.
using System;
using Richasy.Bili.Models.BiliBili;
namespace Richasy.Bili.Models.App.Other
{
///
/// Graph service exception.
///
public class ServiceException : Exception
{
///
/// Creates a new service exception.
///
/// The error that triggered the exception.
/// The possible innerException.
public ServiceException(ServerResponse error, Exception innerException = null)
: this(error, responseHeaders: null, statusCode: default, 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(ServerResponse 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(
ServerResponse 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 ServerResponse 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;
if (string.Equals(currentError.Code.ToString(), errorCode, StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
///
public override string ToString()
{
return $@"Status Code: {this.StatusCode}{Environment.NewLine}{base.ToString()}";
}
}
}