using System;
using System.Collections.Generic;
using System.Linq;
using Json.LitJson;
namespace ServiceClientGenerator
{
#region ExceptionShape
///
/// The model that represents exceptions for the service
///
public class ExceptionShape : Shape
{
///
/// Creates a model that represents an exception in the service processed in the response
///
/// The json data that contains information about the exception found in the model
/// The name of the exception
/// The documentation for the exception found in the service model json
/// The shape that represents the exception
public ExceptionShape(ServiceModel model, string name, JsonData data) : base(model, name, data)
{
}
///
/// Returns the name of the exception with the word Exception appended to the end, removes the word fault from the end if it's there
///
public override string Name
{
get
{
string normalizedName;
if (base.Name.EndsWith("Exception"))
{
normalizedName = base.Name;
}
else if (base.Name.EndsWith("Fault"))
{
string trimmedName = base.Name.Substring(0, base.Name.Length - 5);
normalizedName = $"{trimmedName}Exception";
}
else
{
normalizedName = $"{base.Name}Exception";
}
return normalizedName;
}
}
///
/// Members of the shape, defined by another shape.
/// This overrider removes Message member becuase it's passed to System.Exception directly
///
public override IList Members
{
get
{
return base.Members
.Where(member => !member.PropertyName.Equals("Message"))
.ToList();
}
}
///
/// The error code for the exception, returns the name if one is not specified in the json model.
/// We first check in any referenced structure, then fall back to the exception shape to discover
/// the code.
///
public string Code
{
get
{
if (HasErrorCode)
{
return ErrorCode;
}
var code = data[ErrorKey]?[ErrorCodeKey]?.ToString();
return code ?? base.Name;
}
}
///
/// Determines if the exception is marked retryable
///
public bool IsRetryable
{
get
{
return this.data[RetryableKey] != null;
}
}
///
/// Determines if a retryable exception is marked as throttling
///
public bool Throttling
{
get
{
var throttling = this.data[RetryableKey]?[ThrottlingKey];
return (bool)(throttling ?? false);
}
}
}
#endregion
}