namespace UInsight;
public enum InsightErrorCategory
{
Unknown,
InvalidInput,
ParseFailed,
AnalysisFailed,
InsufficientData,
InvalidParameter,
DegenerateData,
ComputationFailed,
}
public class InsightException : Exception
{
public int ErrorCode { get; }
public InsightErrorCategory Category => ErrorCode switch
{
Interop.NativeLibrary.INSIGHT_ERR_INVALID_INPUT => InsightErrorCategory.InvalidInput,
Interop.NativeLibrary.INSIGHT_ERR_PARSE_FAILED => InsightErrorCategory.ParseFailed,
Interop.NativeLibrary.INSIGHT_ERR_ANALYSIS_FAILED => InsightErrorCategory.AnalysisFailed,
Interop.NativeLibrary.INSIGHT_ERR_INSUFFICIENT_DATA => InsightErrorCategory.InsufficientData,
Interop.NativeLibrary.INSIGHT_ERR_INVALID_PARAM => InsightErrorCategory.InvalidParameter,
Interop.NativeLibrary.INSIGHT_ERR_DEGENERATE_DATA => InsightErrorCategory.DegenerateData,
Interop.NativeLibrary.INSIGHT_ERR_COMPUTATION_FAILED => InsightErrorCategory.ComputationFailed,
_ => InsightErrorCategory.Unknown
};
public InsightException(int errorCode, string message)
: base(message)
{
ErrorCode = errorCode;
}
public InsightException(int errorCode, string message, Exception innerException)
: base(message, innerException)
{
ErrorCode = errorCode;
}
internal static InsightException FromCode(int code, string? nativeError)
{
var baseMsg = Interop.NativeLibrary.GetErrorMessage(code);
var msg = nativeError is not null ? $"{baseMsg}: {nativeError}" : baseMsg;
return new InsightException(code, msg);
}
}