exception.dart 1.0 KB

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