Skip to content

Commit

Permalink
minor changes in error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
quetool committed Feb 22, 2024
1 parent 8660262 commit d6fedb3
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 30 deletions.
2 changes: 1 addition & 1 deletion example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _MyHomePageState extends State<MyHomePage> {
debugPrint('Project ID: ${DartDefines.projectId}');
_web3App = await Web3App.createInstance(
projectId: DartDefines.projectId,
logLevel: LogLevel.info,
logLevel: LogLevel.debug,
metadata: const PairingMetadata(
name: 'Sample dApp Flutter',
description: 'WalletConnect\'s sample dapp with Flutter',
Expand Down
2 changes: 1 addition & 1 deletion example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Web3WalletService extends IWeb3WalletService {
_web3Wallet = Web3Wallet(
core: Core(
projectId: DartDefines.projectId,
logLevel: LogLevel.error,
logLevel: LogLevel.debug,
),
metadata: const PairingMetadata(
name: 'Sample Wallet Flutter',
Expand Down
2 changes: 1 addition & 1 deletion lib/apis/auth_api/auth_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ class AuthEngine implements IAuthEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down
9 changes: 9 additions & 0 deletions lib/apis/core/core.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:walletconnect_flutter_v2/apis/core/crypto/crypto.dart';
import 'package:walletconnect_flutter_v2/apis/core/crypto/i_crypto.dart';
Expand Down Expand Up @@ -75,6 +76,8 @@ class Core implements ICore {
@override
late IStore<Map<String, dynamic>> storage;

void _logListener(LogEvent event) => debugPrint('${event.message}');

Core({
required this.projectId,
this.relayUrl = WalletConnectConstants.DEFAULT_RELAY_URL,
Expand All @@ -88,6 +91,12 @@ class Core implements ICore {
level: logLevel.toLevel(),
printer: PrettyPrinter(),
);
try {
Logger.removeLogListener(_logListener);
} catch (_) {}
if (kDebugMode && logLevel == LogLevel.debug) {
Logger.addLogListener(_logListener);
}
heartbeat = HeartBeat();
storage = SharedPrefsStores(
memoryStore: memoryStore,
Expand Down
12 changes: 6 additions & 6 deletions lib/apis/core/relay_client/json_rpc_2/error_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
///
/// [spec]: http://www.jsonrpc.org/specification#error_object
/// An error code indicating that invalid JSON was received by the server.
const PARSE_ERROR = -32700;
// const PARSE_ERROR = -32700;

/// An error code indicating that the request JSON was invalid according to the
/// JSON-RPC 2.0 spec.
Expand All @@ -24,7 +24,7 @@ const METHOD_NOT_FOUND = -32601;

/// An error code indicating that the request parameters are invalid for the
/// requested method.
const INVALID_PARAMS = -32602;
// const INVALID_PARAMS = -32602;

/// An internal JSON-RPC error.
const INTERNAL_ERROR = -32603;
Expand All @@ -41,14 +41,14 @@ const SERVER_ERROR = -32000;
/// If [errorCode] isn't defined in the JSON-RPC 2.0 spec, returns null.
String? name(int errorCode) {
switch (errorCode) {
case PARSE_ERROR:
return 'parse error';
// case PARSE_ERROR:
// return 'parse error';
case INVALID_REQUEST:
return 'invalid request';
case METHOD_NOT_FOUND:
return 'method not found';
case INVALID_PARAMS:
return 'invalid parameters';
// case INVALID_PARAMS:
// return 'invalid parameters';
case INTERNAL_ERROR:
return 'internal error';
default:
Expand Down
2 changes: 1 addition & 1 deletion lib/apis/core/relay_client/json_rpc_2/src/exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class RpcException implements Exception {
///
/// Methods can use this to reject requests with invalid parameters.
RpcException.invalidParams(String message)
: this(error_code.INVALID_PARAMS, message);
: this(error_code.INVALID_REQUEST, message);

/// Converts this exception into a JSON-serializable object that's a valid
/// JSON-RPC 2.0 error response.
Expand Down
4 changes: 2 additions & 2 deletions lib/apis/core/relay_client/json_rpc_2/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class _RespondToFormatExceptionsTransformer
return channel.changeStream((stream) {
return stream.handleError((dynamic error) {
final formatException = error as FormatException;
var exception = RpcException(
error_code.PARSE_ERROR, 'Invalid JSON: ${formatException.message}');
var exception = RpcException(error_code.INVALID_REQUEST,
'Invalid JSON: ${formatException.message}');
channel.sink.add(exception.serialize(formatException.source));
}, test: (error) => error is FormatException);
});
Expand Down
8 changes: 4 additions & 4 deletions lib/apis/models/json_rpc_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class JsonRpcError with _$JsonRpcError {

factory JsonRpcError.serverError(String message) =>
JsonRpcError(code: -32000, message: message);
factory JsonRpcError.invalidParams(String message) =>
JsonRpcError(code: -32602, message: message);
// factory JsonRpcError.invalidParams(String message) =>
// JsonRpcError(code: -32602, message: message);
factory JsonRpcError.invalidRequest(String message) =>
JsonRpcError(code: -32600, message: message);
factory JsonRpcError.parseError(String message) =>
JsonRpcError(code: -32700, message: message);
// factory JsonRpcError.parseError(String message) =>
// JsonRpcError(code: -32700, message: message);
factory JsonRpcError.methodNotFound(String message) =>
JsonRpcError(code: -32601, message: message);

Expand Down
20 changes: 10 additions & 10 deletions lib/apis/sign_api/sign_engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class SignEngine implements ISignEngine {
);
// print('approve session topic: $sessionTopic');
final relay = Relay(
relayProtocol ?? 'irn',
relayProtocol ?? WalletConnectConstants.RELAYER_DEFAULT_PROTOCOL,
);

// Respond to the proposal
Expand Down Expand Up @@ -1101,7 +1101,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down Expand Up @@ -1139,7 +1139,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down Expand Up @@ -1176,7 +1176,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down Expand Up @@ -1207,7 +1207,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand All @@ -1233,7 +1233,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.toString(),
),
);
Expand Down Expand Up @@ -1337,7 +1337,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down Expand Up @@ -1375,7 +1375,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.toString(),
),
);
Expand Down Expand Up @@ -1413,7 +1413,7 @@ class SignEngine implements ISignEngine {
payload.id,
topic,
payload.method,
JsonRpcError.invalidParams(
JsonRpcError.invalidRequest(
err.message,
),
);
Expand Down
8 changes: 4 additions & 4 deletions test/sign_api/tests/sign_request_and_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void signRequestAndHandler({
} on JsonRpcError catch (e) {
expect(
e.code,
JsonRpcError.invalidParams('swag').code,
JsonRpcError.invalidRequest('swag').code,
);
}

Expand Down Expand Up @@ -276,7 +276,7 @@ void signRequestAndHandler({
topic: request.topic,
response: JsonRpcResponse(
id: request.id,
error: JsonRpcError.invalidParams(request.params.toString()),
error: JsonRpcError.invalidRequest(request.params.toString()),
),
);
}
Expand Down Expand Up @@ -308,7 +308,7 @@ void signRequestAndHandler({
// print(e);
expect(
e.code,
JsonRpcError.invalidParams('swag').code,
JsonRpcError.invalidRequest('swag').code,
);
expect(e.message!.contains(TEST_MESSAGE_1.toString()), true);
}
Expand All @@ -328,7 +328,7 @@ void signRequestAndHandler({
topic: session.topic,
response: JsonRpcResponse<String>(
id: session.id,
error: JsonRpcError.invalidParams('invalid'),
error: JsonRpcError.invalidRequest('invalid'),
),
);

Expand Down

0 comments on commit d6fedb3

Please sign in to comment.