Skip to content

Commit

Permalink
Merge pull request #200 from WalletConnect/chore/improve-errors
Browse files Browse the repository at this point in the history
chore: improve errors
  • Loading branch information
skibitsky authored Jul 16, 2024
2 parents ef40c94 + 911fbbe commit a179727
Show file tree
Hide file tree
Showing 34 changed files with 612 additions and 694 deletions.
40 changes: 8 additions & 32 deletions Core Modules/WalletConnectSharp.Common/Model/Errors/ErrorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,6 @@ public enum ErrorType : uint
{
// 0 (Generic)
GENERIC = 0,

// 10 (Internal)
NON_CONFORMING_NAMESPACES = 9,

// 1000 (Internal)
MISSING_OR_INVALID = 1000,
MISSING_RESPONSE = 1001,
MISSING_DECRYPT_PARAMS = 1002,
INVALID_UPDATE_REQUEST = 1003,
INVALID_UPGRADE_REQUEST = 1004,
INVALID_EXTEND_REQUEST = 1005,
INVALID_STORAGE_KEY_NAME = 1020,
RECORD_ALREADY_EXISTS = 1100,
RESTORE_WILL_OVERRIDE = 1200,
NO_MATCHING_ID = 1300,
NO_MATCHING_TOPIC = 1301,
NO_MATCHING_RESPONSE = 1302,
NO_MATCHING_KEY = 1303,
UNKNOWN_JSONRPC_METHOD = 1400,
MISMATCHED_TOPIC = 1500,
MISMATCHED_ACCOUNTS = 1501,
SETTLED = 1600,
NOT_APPROVED = 1601,
PROPOSAL_RESPONDED = 1602,
RESPONSE_ACKNOWLEDGED = 1603,
EXPIRED = 1604,
DELETED = 1605,
RESUBSCRIBED = 1606,
NOT_INITIALIZED = 1607,

// 2000 (Timeout)
SETTLE_TIMEOUT = 2000,
Expand All @@ -58,7 +29,7 @@ public enum ErrorType : uint
JSONRPC_REQUEST_METHOD_UNSUPPORTED = 4200,
DISCONNECTED_ALL_CHAINS = 4900,
DISCONNECTED_TARGET_CHAIN = 4901,

// 5000 (CAIP-25)
DISAPPROVED_CHAINS = 5000,
DISAPPROVED_JSONRPC = 5001,
Expand All @@ -67,9 +38,14 @@ public enum ErrorType : uint
UNSUPPORTED_JSONRPC = 5101,
UNSUPPORTED_NOTIFICATION = 5102,
UNSUPPORTED_ACCOUNTS = 5103,
USER_DISCONNECTED = 5900,

// 6000 (Reason)
USER_DISCONNECTED = 6000,

// 8000 (Session)
SESSION_REQUEST_EXPIRED = 8000,

// 9000 (Unknown)
UNKNOWN = 9000,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Runtime.Serialization;

namespace WalletConnectSharp.Common.Model.Errors;

public class ExpiredException : Exception
{
public ExpiredException()
{
}

public ExpiredException(string message)
: base(message)
{
}

public ExpiredException(string message, Exception innerException)
: base(message, innerException)
{
}

protected ExpiredException(SerializationInfo info, StreamingContext context)
: base(info, context)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/ExpiredException.cs

View workflow job for this annotation

GitHub Actions / build

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/ExpiredException.cs

View workflow job for this annotation

GitHub Actions / test (unit-tests)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/ExpiredException.cs

View workflow job for this annotation

GitHub Actions / test (integration-tests)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Runtime.Serialization;

namespace WalletConnectSharp.Common.Model.Errors;

public class NamespacesException : Exception
{
public NamespacesException()
{
}

public NamespacesException(string message)
: base(message)
{
}

public NamespacesException(string message, Exception innerException)
: base(message, innerException)
{
}

protected NamespacesException(SerializationInfo info, StreamingContext context)
: base(info, context)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/NamespacesException.cs

View workflow job for this annotation

GitHub Actions / build

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/NamespacesException.cs

View workflow job for this annotation

GitHub Actions / test (unit-tests)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)

Check warning on line 22 in Core Modules/WalletConnectSharp.Common/Model/Errors/NamespacesException.cs

View workflow job for this annotation

GitHub Actions / test (integration-tests)

'Exception.Exception(SerializationInfo, StreamingContext)' is obsolete: 'This API supports obsolete formatter-based serialization. It should not be called or extended by application code.' (https://aka.ms/dotnet-warnings/SYSLIB0051)
{
}
}
150 changes: 11 additions & 139 deletions Core Modules/WalletConnectSharp.Common/Model/Errors/SdkErrors.cs
Original file line number Diff line number Diff line change
@@ -1,148 +1,38 @@
using System.Collections.Generic;
using WalletConnectSharp.Common.Utils;

namespace WalletConnectSharp.Common.Model.Errors
namespace WalletConnectSharp.Common.Model.Errors
{
/// <summary>
/// A helper class for generating error messages
/// based on an ErrorType
/// </summary>
public static class SdkErrors
{
private static readonly Dictionary<string, object> DefaultParameters = new Dictionary<string, object>()
{
{"topic", "undefined"},
{"message", "Something went wrong"},
{"name", "parameter"},
{"context", "session"},
{"blockchain", "Ethereum"}
};

/// <summary>
/// Generate an error message using an ErrorType code and a dictionary
/// of parameters for the error message
/// An anonymous type can also be passed, which will be converted to a
/// dictionary
/// </summary>
/// <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, Dictionary<string, object> @params = null)
{
return MessageFromType(type, null, @params);
}

/// <summary>
/// Generate an error message using an ErrorType code, a message parameters
/// and a dictionary of parameters for the error message
/// </summary>
/// <param name="type">The error type message to generate</param>
/// <param name="message">The message parameter</param>
/// <param name="params">A dictionary of parameters for the error message</param>
/// <param name="context">Additional context</param>
/// <returns>The error message as a string</returns>
public static string MessageFromType(ErrorType type, string message = null, Dictionary<string, object> @params = null)
public static string MessageFromType(ErrorType type, string context = null)
{
if (@params == null)
{
@params = new Dictionary<string, object>();
}

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

string errorMessage;
switch (type)
{
default:
case ErrorType.GENERIC:
errorMessage = "{message}";
break;
case ErrorType.MISSING_OR_INVALID:
errorMessage = "Missing or invalid {name}";
break;
case ErrorType.MISSING_RESPONSE:
errorMessage = "Response is required for approved {context} proposals";
break;
case ErrorType.MISSING_DECRYPT_PARAMS:
errorMessage = "Decrypt params required for {context}";
break;
case ErrorType.INVALID_UPDATE_REQUEST:
errorMessage = "Invalid {context} update request";
break;
case ErrorType.INVALID_UPGRADE_REQUEST:
errorMessage = "Invalid {context} upgrade request";
break;
case ErrorType.INVALID_EXTEND_REQUEST:
errorMessage = "Invalid {context} extend request";
break;
case ErrorType.INVALID_STORAGE_KEY_NAME:
errorMessage = "Invalid storage key name: {name}";
break;
case ErrorType.RECORD_ALREADY_EXISTS:
errorMessage = "Record already exists for {context} matching id: {id}";
break;
case ErrorType.RESTORE_WILL_OVERRIDE:
errorMessage = "Restore will override already set {context}";
break;
case ErrorType.NO_MATCHING_ID:
errorMessage = "No matching {context} with id: {id}";
break;
case ErrorType.NO_MATCHING_TOPIC:
errorMessage = "No matching {context} with topic {topic}";
break;
case ErrorType.NO_MATCHING_RESPONSE:
errorMessage = "No response found in pending {context} proposal";
break;
case ErrorType.NO_MATCHING_KEY:
errorMessage = "No matching key with tag: {tag}";
break;
case ErrorType.UNKNOWN_JSONRPC_METHOD:
errorMessage = "Unknown JSON-RPC Method Requested: {method}";
break;
case ErrorType.MISMATCHED_TOPIC:
errorMessage = "Mismatched topic for {context} with id: {id}";
break;
case ErrorType.MISMATCHED_ACCOUNTS:
errorMessage = "Invalid accounts with mismatched chains: {mismatched}";
break;
case ErrorType.SETTLED:
errorMessage = "{context} settled";
break;
case ErrorType.NOT_APPROVED:
errorMessage = "{context} not approved";
break;
case ErrorType.PROPOSAL_RESPONDED:
errorMessage = "{context} proposal responded";
break;
case ErrorType.RESPONSE_ACKNOWLEDGED:
errorMessage = "{context} response acknowledge";
break;
case ErrorType.EXPIRED:
errorMessage = "{context} expired";
break;
case ErrorType.DELETED:
errorMessage = "{context} deleted";
break;
case ErrorType.RESUBSCRIBED:
errorMessage = "Subscription resubscribed with topic: {topic}";
break;
case ErrorType.NOT_INITIALIZED:
errorMessage = "{params} was not initialized";
break;
case ErrorType.SETTLE_TIMEOUT:
errorMessage = "{context} failed to settle after {timeout} seconds";
break;
case ErrorType.JSONRPC_REQUEST_TIMEOUT:
errorMessage = "JSON-RPC Request timeout after {timeout} seconds: {method}";
break;
case ErrorType.UNAUTHORIZED_TARGET_CHAIN:
errorMessage = "Unauthorized Target ChainId Requested: {chainId}";
errorMessage = "Unauthorized Target ChainId Requested/";
break;
case ErrorType.UNAUTHORIZED_JSON_RPC_METHOD:
errorMessage = "Unauthorized JSON-RPC Method Requested: {method}";
errorMessage = "Unauthorized JSON-RPC Method Requested.";
break;
case ErrorType.UNAUTHORIZED_NOTIFICATION_TYPE:
errorMessage = "Unauthorized Notification Type Requested: {type}";
errorMessage = "Unauthorized Notification Type Requested.";
break;
case ErrorType.UNAUTHORIZED_UPDATE_REQUEST:
errorMessage = "Unauthorized {context} update request";
Expand Down Expand Up @@ -196,39 +86,21 @@ public static string MessageFromType(ErrorType type, string message = null, Dict
errorMessage = "{message}";
break;
case ErrorType.USER_DISCONNECTED:
errorMessage = "User disconnected {context}";
errorMessage = "User disconnected.";
break;
case ErrorType.UNKNOWN:
errorMessage = "Unknown error {params}";
break;
case ErrorType.NON_CONFORMING_NAMESPACES:
errorMessage = @params["message"].ToString();
break;
}

errorMessage = FormatErrorText(errorMessage, DefaultParameters, @params);

return errorMessage;
}

private static string FormatErrorText(string formattedText, Dictionary<string, object> defaultArgs, Dictionary<string, object> args = null)
{
if (args == null)
args = new Dictionary<string, object>();

string text = formattedText;

foreach (var key in args.Keys)
if (context == null)
{
text = text.Replace("{" + key.ToLower() + "}", args[key].ToString());
return errorMessage;
}

foreach (var key in defaultArgs.Keys)
else
{
text = text.Replace("{" + key.ToLower() + "}", defaultArgs[key].ToString());
return $"{errorMessage} {context}";
}

return text;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using WalletConnectSharp.Common.Utils;

namespace WalletConnectSharp.Common.Model.Errors
{
Expand All @@ -18,7 +15,7 @@ public class WalletConnectException : Exception
public uint Code { get; private set; }

/// <summary>
/// The error tyep of this exception
/// The error type of this exception
/// </summary>
[JsonProperty("type")]
public string Type { get; private set; }
Expand Down Expand Up @@ -64,29 +61,17 @@ public WalletConnectException(string message, Exception innerException, ErrorTyp
/// exception
/// </summary>
/// <param name="type">The error type of the exception</param>
/// <param name="message">The message parameter</param>
/// <param name="params">Additional (optional) parameters for the generated error message</param>
/// <param name="message">An (optional) message for the error</param>
/// <param name="context">An (optional) context for the error message</param>
/// <param name="innerException">An (optional) inner exception that caused this exception</param>
/// <returns>A new exception</returns>
public static WalletConnectException FromType(ErrorType type, string message = null, Dictionary<string, object> @params = null, Exception innerException = null)
public static WalletConnectException FromType(ErrorType type, string message = null, string context = null, Exception innerException = null)
{
string errorMessage = SdkErrors.MessageFromType(type, message, @params);
var errorMessage = message ?? SdkErrors.MessageFromType(type, context);

if (innerException != null)
return new WalletConnectException(errorMessage, innerException, type);
return new WalletConnectException(errorMessage, type);
}

/// <summary>
/// A helper function that creates an exception given an ErrorType, and
/// an (optional) dictionary of parameters for the error message
/// </summary>
/// <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, Dictionary<string, object> @params = null)
{
return FromType(type, null, @params);
return innerException != null
? new WalletConnectException(errorMessage, innerException, type)
: new WalletConnectException(errorMessage, type);
}
}
}
Loading

0 comments on commit a179727

Please sign in to comment.