exception.dart 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /// JSON-RPC exception
  2. class JsonRpcException implements Exception {
  3. String? message;
  4. int? code;
  5. dynamic data;
  6. JsonRpcException({
  7. this.code,
  8. this.message,
  9. this.data,
  10. });
  11. @override
  12. String toString() {
  13. return 'JSON-RPC Exception: $code $message';
  14. }
  15. Map<String, dynamic> toJson() {
  16. var map = <String, dynamic>{'code': code, 'message': message};
  17. if (data != null) {
  18. map['data'] = data;
  19. }
  20. return map;
  21. }
  22. }
  23. /// JSON-RPC network exception
  24. class JsonRpcNetworkException extends JsonRpcException {
  25. JsonRpcNetworkException(String message, [code = -32000, data])
  26. : super(message: message);
  27. }
  28. /// JSON-RPC Server Error
  29. class JsonRpcServerError extends JsonRpcException {
  30. JsonRpcServerError(String message, [code = -32000, data])
  31. : super(message: message, code: code, data: data);
  32. JsonRpcServerError.fromJson(Map<String, dynamic> e) : super() {
  33. if (e.containsKey('message')) {
  34. message = e['message'];
  35. }
  36. if (e.containsKey('code')) {
  37. code = e['code'];
  38. }
  39. if (e.containsKey('data')) {
  40. data = e['data'];
  41. }
  42. }
  43. }