Skip to content

Commit

Permalink
fix: more unit tests, get rid of asdictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
gigajuwels committed Sep 10, 2023
1 parent d933f98 commit 79211ad
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 44 deletions.
10 changes: 5 additions & 5 deletions Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static class SdkErrors
/// <param name="type">The error type message to generate</param>
/// <param name="params">A dictionary (or anonymous type) of parameters for the error message</param>
/// <returns>The error message as a string</returns>
public static string MessageFromType(ErrorType type, object @params = null)
public static string MessageFromType(ErrorType type, Dictionary<string, object> @params = null)
{
return MessageFromType(type, null, @params.AsDictionary());
return MessageFromType(type, null, @params);
}

/// <summary>
Expand All @@ -47,8 +47,8 @@ public static string MessageFromType(ErrorType type, string message = null, Dict
@params = new Dictionary<string, object>();
}

if (!string.IsNullOrWhiteSpace(message) && !@params.ContainsKey("message"))
@params.Add("message", message);
if (!string.IsNullOrWhiteSpace(message))
@params.TryAdd("message", message);

string errorMessage;
switch (type)
Expand Down Expand Up @@ -231,4 +231,4 @@ private static string FormatErrorText(string formattedText, Dictionary<string, o
return text;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public static WalletConnectException FromType(ErrorType type, string message = n
/// <param name="type">The error type of the exception</param>
/// <param name="params">Additional (optional) parameters for the generated error message</param>
/// <returns>A new exception</returns>
public static WalletConnectException FromType(ErrorType type, object @params = null)
public static WalletConnectException FromType(ErrorType type, Dictionary<string, object> @params = null)
{
return FromType(type, null, @params.AsDictionary());
return FromType(type, null, @params);
}
}
}
}
28 changes: 0 additions & 28 deletions Core Modules/WalletConnectSharp.Common/Utils/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@ namespace WalletConnectSharp.Common.Utils
/// </summary>
public static class Extensions
{
/// <summary>
/// Convert an anonymous type to a Dictionary
/// </summary>
/// <param name="obj">The anonymous type instance to convert to a dictionary</param>
/// <param name="enforceLowercase">Enforce all keys to be lowercased</param>
/// <returns>A dictionary where each key is the property name of the anonymous type
/// and each value is the property's value</returns>
public static Dictionary<string, object> AsDictionary(this object obj, bool enforceLowercase = true)
{
if (obj is Dictionary<string, object> objects)
return objects;

var dict = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

if (obj != null)
{
foreach (PropertyInfo propertyDescriptor in obj.GetType().GetProperties())
{
object value = propertyDescriptor.GetValue(obj, null);
var key = enforceLowercase ? propertyDescriptor.Name.ToLower() : propertyDescriptor.Name;

dict.Add(key, value);
}
}

return dict;
}

/// <summary>
/// Returns true if the given object is a numeric type
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion Core Modules/WalletConnectSharp.Crypto/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ private void IsInitialized()
{
if (!this._initialized)
{
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name});
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary<string, object>()
{
{ "Name", Name }
});
}
}

Expand Down
15 changes: 12 additions & 3 deletions Core Modules/WalletConnectSharp.Crypto/KeyChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ public async Task<string> Get(string tag)

if (!await Has(tag))
{
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {tag});
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary<string, object>()
{
{"tag", tag}
});
}

return this._keyChain[tag];
Expand All @@ -160,7 +163,10 @@ public async Task Delete(string tag)

if (!await Has(tag))
{
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {tag});
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary<string, object>()
{
{"tag", tag}
});
}

_keyChain.Remove(tag);
Expand All @@ -172,7 +178,10 @@ private void IsInitialized()
{
if (!this._initialized)
{
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new {Name});
throw WalletConnectException.FromType(ErrorType.NOT_INITIALIZED, new Dictionary<string, object>()
{
{"Name", Name}
});
}
}

Expand Down
13 changes: 12 additions & 1 deletion Core Modules/WalletConnectSharp.Network/Models/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public class Error
/// </summary>
[JsonProperty("data")]
public string Data;

/// <summary>
/// Create an ErrorResponse with a given ErrorType and (optional) parameters
/// </summary>
/// <param name="type">The error type of the ErrorResponse to create</param>
/// <param name="message">The message to attach to the error</param>
/// <returns>A new ErrorResponse</returns>
public static Error FromErrorType(ErrorType type, string message)
{
return FromErrorType(type, new Dictionary<string, object>() { { "message", message } });
}

/// <summary>
/// Create an ErrorResponse with a given ErrorType and (optional) parameters
Expand All @@ -36,7 +47,7 @@ public class Error
/// <param name="params">Extra parameters for the error message</param>
/// <param name="extraData">Extra data that is stored in the Data field of the newly created ErrorResponse</param>
/// <returns>A new ErrorResponse</returns>
public static Error FromErrorType(ErrorType type, object @params = null, string extraData = null)
public static Error FromErrorType(ErrorType type, Dictionary<string, object> @params = null, string extraData = null)
{
string message = SdkErrors.MessageFromType(type, @params);

Expand Down
4 changes: 2 additions & 2 deletions WalletConnectSharp.Auth/Controllers/AuthEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ private async Task OnAuthResponse(string topic, JsonRpcResponse<Cacao> response)
{
this.Client.OnAuthResponse(new AuthErrorResponse()
{
Id = id, Topic = topic, Error = Error.FromErrorType(ErrorType.GENERIC, new
Id = id, Topic = topic, Error = Error.FromErrorType(ErrorType.GENERIC, new Dictionary<string, object>()
{
Message = "Invalid signature"
{"Message", "Invalid signature"}
})
});
}
Expand Down
5 changes: 4 additions & 1 deletion WalletConnectSharp.Core/Controllers/JsonRpcHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ private JsonRpcRecord<T, TR> GetRecord(long id)

if (!_records.ContainsKey(id))
{
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new {Tag = $"{Name}: {id}"});
throw WalletConnectException.FromType(ErrorType.NO_MATCHING_KEY, new Dictionary<string, object>()
{
{"Tag", $"{Name}: {id}"}
});
}

return _records[id];
Expand Down

0 comments on commit 79211ad

Please sign in to comment.