custom eth api exception

This commit is contained in:
julian 2023-02-28 16:03:36 -06:00
parent 6b7eb8f463
commit 70335286be

View file

@ -110,11 +110,20 @@ class EthTokenTx {
} }
} }
class EthApiException with Exception {
EthApiException(this.message);
final String message;
@override
String toString() => "$runtimeType: $message";
}
class EthereumResponse<T> { class EthereumResponse<T> {
EthereumResponse(this.value, this.exception); EthereumResponse(this.value, this.exception);
final T? value; final T? value;
final Exception? exception; final EthApiException? exception;
@override @override
toString() => "EthereumResponse{ value: $value, exception: $exception"; toString() => "EthereumResponse{ value: $value, exception: $exception";
@ -176,14 +185,19 @@ abstract class EthereumAPI {
null, null,
); );
} else { } else {
throw Exception(json["message"] as String); throw EthApiException(json["message"] as String);
} }
} else { } else {
throw Exception( throw EthApiException(
"getWalletTokens($address) failed with status code: " "getWalletTokens($address) failed with status code: "
"${response.statusCode}", "${response.statusCode}",
); );
} }
} on EthApiException catch (e) {
return EthereumResponse(
null,
e,
);
} catch (e, s) { } catch (e, s) {
Logging.instance.log( Logging.instance.log(
"getWalletTokens(): $e\n$s", "getWalletTokens(): $e\n$s",
@ -191,7 +205,7 @@ abstract class EthereumAPI {
); );
return EthereumResponse( return EthereumResponse(
null, null,
Exception(e.toString()), EthApiException(e.toString()),
); );
} }
} }
@ -233,7 +247,8 @@ abstract class EthereumAPI {
), ),
); );
} else { } else {
throw Exception("Unsupported token type found: ${map["type"]}"); throw EthApiException(
"Unsupported token type found: ${map["type"]}");
} }
} }
@ -242,14 +257,19 @@ abstract class EthereumAPI {
null, null,
); );
} else { } else {
throw Exception(json["message"] as String); throw EthApiException(json["message"] as String);
} }
} else { } else {
throw Exception( throw EthApiException(
"getWalletTokens($address) failed with status code: " "getWalletTokens($address) failed with status code: "
"${response.statusCode}", "${response.statusCode}",
); );
} }
} on EthApiException catch (e) {
return EthereumResponse(
null,
e,
);
} catch (e, s) { } catch (e, s) {
Logging.instance.log( Logging.instance.log(
"getWalletTokens(): $e\n$s", "getWalletTokens(): $e\n$s",
@ -257,7 +277,7 @@ abstract class EthereumAPI {
); );
return EthereumResponse( return EthereumResponse(
null, null,
Exception(e.toString()), EthApiException(e.toString()),
); );
} }
} }
@ -300,7 +320,7 @@ abstract class EthereumAPI {
EthToken? token; EthToken? token;
if (map["type"] == "ERC-20") { if (map["type"] == "ERC-20") {
token = Erc20Token( token = Erc20Token(
balance: int.parse(map["balance"] as String), balance: 0, //int.parse(map["balance"] as String),
contractAddress: map["contractAddress"] as String, contractAddress: map["contractAddress"] as String,
decimals: int.parse(map["decimals"] as String), decimals: int.parse(map["decimals"] as String),
name: map["name"] as String, name: map["name"] as String,
@ -308,14 +328,15 @@ abstract class EthereumAPI {
); );
} else if (map["type"] == "ERC-721") { } else if (map["type"] == "ERC-721") {
token = Erc721Token( token = Erc721Token(
balance: int.parse(map["balance"] as String), balance: 0, //int.parse(map["balance"] as String),
contractAddress: map["contractAddress"] as String, contractAddress: map["contractAddress"] as String,
decimals: int.parse(map["decimals"] as String), decimals: int.parse(map["decimals"] as String),
name: map["name"] as String, name: map["name"] as String,
symbol: map["symbol"] as String, symbol: map["symbol"] as String,
); );
} else { } else {
throw Exception("Unsupported token type found: ${map["type"]}"); throw EthApiException(
"Unsupported token type found: ${map["type"]}");
} }
return EthereumResponse( return EthereumResponse(
@ -323,14 +344,19 @@ abstract class EthereumAPI {
null, null,
); );
} else { } else {
throw Exception(json["message"] as String); throw EthApiException(json["message"] as String);
} }
} else { } else {
throw Exception( throw EthApiException(
"getTokenByContractAddress($contractAddress) failed with status code: " "getTokenByContractAddress($contractAddress) failed with status code: "
"${response.statusCode}", "${response.statusCode}",
); );
} }
} on EthApiException catch (e) {
return EthereumResponse(
null,
e,
);
} catch (e, s) { } catch (e, s) {
Logging.instance.log( Logging.instance.log(
"getWalletTokens(): $e\n$s", "getWalletTokens(): $e\n$s",
@ -338,7 +364,7 @@ abstract class EthereumAPI {
); );
return EthereumResponse( return EthereumResponse(
null, null,
Exception(e.toString()), EthApiException(e.toString()),
); );
} }
} }