using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CryptoExchange.Net.Objects.Errors { /// /// Error mapping collection /// public class ErrorMapping { private Dictionary _evaluators = new Dictionary(); private Dictionary _directMapping = new Dictionary(); /// /// ctor /// public ErrorMapping(ErrorInfo[] errorMappings, ErrorEvaluator[]? errorTypeEvaluators = null) { foreach (var item in errorMappings) { if (!item.ErrorCodes.Any()) throw new Exception("Error codes can't be null in error mapping"); foreach(var code in item.ErrorCodes!) _directMapping.Add(code, item); } if (errorTypeEvaluators == null) return; foreach (var item in errorTypeEvaluators) { foreach(var code in item.ErrorCodes) _evaluators.Add(code, item); } } /// /// Get error info for an error code /// public ErrorInfo GetErrorInfo(string code, string? message) { if (_directMapping.TryGetValue(code!, out var info)) return info with { Message = message }; if (_evaluators.TryGetValue(code!, out var eva)) return eva.ErrorTypeEvaluator.Invoke(code!, message) with { Message = message }; return ErrorInfo.Unknown with { Message = message }; } } }