This reverts commit c67eafa87d2de441a195a8aa46c92eac37ce4616.
This commit is contained in:
parent
c67eafa87d
commit
9e162cb310
@ -80,10 +80,9 @@ abstract class MethodCodec {
|
|||||||
|
|
||||||
/// Encodes an error result into a binary envelope.
|
/// Encodes an error result into a binary envelope.
|
||||||
///
|
///
|
||||||
/// The specified error [code], human-readable error [message], error
|
/// The specified error [code], human-readable error [message], and error
|
||||||
/// [details] correspond to the fields of [PlatformException] and error
|
/// [details] correspond to the fields of [PlatformException].
|
||||||
/// [stacktrace] correspond to stacktrace from native platforms.
|
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details });
|
||||||
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details, String? stacktrace});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -108,7 +107,6 @@ class PlatformException implements Exception {
|
|||||||
required this.code,
|
required this.code,
|
||||||
this.message,
|
this.message,
|
||||||
this.details,
|
this.details,
|
||||||
this.stacktrace,
|
|
||||||
}) : assert(code != null);
|
}) : assert(code != null);
|
||||||
|
|
||||||
/// An error code.
|
/// An error code.
|
||||||
@ -120,18 +118,8 @@ class PlatformException implements Exception {
|
|||||||
/// Error details, possibly null.
|
/// Error details, possibly null.
|
||||||
final dynamic details;
|
final dynamic details;
|
||||||
|
|
||||||
/// Native stacktrace for the error, possibly null.
|
|
||||||
/// This is strictly for native platform stacktrace.
|
|
||||||
/// The stacktrace info on dart platform can be found within the try-catch block for example:
|
|
||||||
/// try {
|
|
||||||
/// ...
|
|
||||||
/// } catch (e, stacktrace) {
|
|
||||||
/// print(stacktrace);
|
|
||||||
/// }
|
|
||||||
final String? stacktrace;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() => 'PlatformException($code, $message, $details, $stacktrace)';
|
String toString() => 'PlatformException($code, $message, $details)';
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Thrown to indicate that a platform interaction failed to find a handling
|
/// Thrown to indicate that a platform interaction failed to find a handling
|
||||||
|
@ -152,16 +152,6 @@ class JSONMethodCodec implements MethodCodec {
|
|||||||
message: decoded[1] as String,
|
message: decoded[1] as String,
|
||||||
details: decoded[2],
|
details: decoded[2],
|
||||||
);
|
);
|
||||||
if (decoded.length == 4
|
|
||||||
&& decoded[0] is String
|
|
||||||
&& (decoded[1] == null || decoded[1] is String)
|
|
||||||
&& (decoded[3] == null || decoded[3] is String))
|
|
||||||
throw PlatformException(
|
|
||||||
code: decoded[0] as String,
|
|
||||||
message: decoded[1] as String,
|
|
||||||
details: decoded[2],
|
|
||||||
stacktrace: decoded[3] as String,
|
|
||||||
);
|
|
||||||
throw FormatException('Invalid envelope: $decoded');
|
throw FormatException('Invalid envelope: $decoded');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,9 +161,9 @@ class JSONMethodCodec implements MethodCodec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details, String? stacktrace}) {
|
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details }) {
|
||||||
assert(code != null);
|
assert(code != null);
|
||||||
return const JSONMessageCodec().encodeMessage(<dynamic>[code, message, details, stacktrace])!;
|
return const JSONMessageCodec().encodeMessage(<dynamic>[code, message, details])!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,13 +547,12 @@ class StandardMethodCodec implements MethodCodec {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details, String? stacktrace}) {
|
ByteData encodeErrorEnvelope({ required String code, String? message, dynamic details }) {
|
||||||
final WriteBuffer buffer = WriteBuffer();
|
final WriteBuffer buffer = WriteBuffer();
|
||||||
buffer.putUint8(1);
|
buffer.putUint8(1);
|
||||||
messageCodec.writeValue(buffer, code);
|
messageCodec.writeValue(buffer, code);
|
||||||
messageCodec.writeValue(buffer, message);
|
messageCodec.writeValue(buffer, message);
|
||||||
messageCodec.writeValue(buffer, details);
|
messageCodec.writeValue(buffer, details);
|
||||||
messageCodec.writeValue(buffer, stacktrace);
|
|
||||||
return buffer.done();
|
return buffer.done();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -578,9 +567,8 @@ class StandardMethodCodec implements MethodCodec {
|
|||||||
final dynamic errorCode = messageCodec.readValue(buffer);
|
final dynamic errorCode = messageCodec.readValue(buffer);
|
||||||
final dynamic errorMessage = messageCodec.readValue(buffer);
|
final dynamic errorMessage = messageCodec.readValue(buffer);
|
||||||
final dynamic errorDetails = messageCodec.readValue(buffer);
|
final dynamic errorDetails = messageCodec.readValue(buffer);
|
||||||
final String? errorStacktrace = (buffer.hasRemaining) ? messageCodec.readValue(buffer) as String : null;
|
|
||||||
if (errorCode is String && (errorMessage == null || errorMessage is String) && !buffer.hasRemaining)
|
if (errorCode is String && (errorMessage == null || errorMessage is String) && !buffer.hasRemaining)
|
||||||
throw PlatformException(code: errorCode, message: errorMessage as String, details: errorDetails, stacktrace: errorStacktrace);
|
throw PlatformException(code: errorCode, message: errorMessage as String, details: errorDetails);
|
||||||
else
|
else
|
||||||
throw const FormatException('Invalid envelope');
|
throw const FormatException('Invalid envelope');
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:matcher/matcher.dart';
|
|
||||||
import '../flutter_test_alternative.dart';
|
import '../flutter_test_alternative.dart';
|
||||||
import 'message_codecs_testing.dart';
|
import 'message_codecs_testing.dart';
|
||||||
|
|
||||||
@ -37,72 +36,14 @@ void main() {
|
|||||||
final ByteData helloByteData = string.encodeMessage('hello');
|
final ByteData helloByteData = string.encodeMessage('hello');
|
||||||
|
|
||||||
final ByteData offsetByteData = ByteData.view(
|
final ByteData offsetByteData = ByteData.view(
|
||||||
helloWorldByteData.buffer,
|
helloWorldByteData.buffer,
|
||||||
helloByteData.lengthInBytes,
|
helloByteData.lengthInBytes,
|
||||||
helloWorldByteData.lengthInBytes - helloByteData.lengthInBytes,
|
helloWorldByteData.lengthInBytes - helloByteData.lengthInBytes,
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(string.decodeMessage(offsetByteData), ' world');
|
expect(string.decodeMessage(offsetByteData), ' world');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
group('Standard method codec', () {
|
|
||||||
const MethodCodec method = StandardMethodCodec();
|
|
||||||
test('should decode error envelope without native stacktrace', () {
|
|
||||||
final ByteData errorData = method.encodeErrorEnvelope(
|
|
||||||
code: 'errorCode',
|
|
||||||
message: 'errorMessage',
|
|
||||||
details: 'errorDetails',
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
() => method.decodeEnvelope(errorData),
|
|
||||||
throwsA(predicate((PlatformException e) =>
|
|
||||||
e is PlatformException &&
|
|
||||||
e.code == 'errorCode' &&
|
|
||||||
e.message == 'errorMessage' &&
|
|
||||||
e.details == 'errorDetails')));
|
|
||||||
});
|
|
||||||
test('should decode error envelope with native stacktrace.', () {
|
|
||||||
final ByteData errorData = method.encodeErrorEnvelope(
|
|
||||||
code: 'errorCode',
|
|
||||||
message: 'errorMessage',
|
|
||||||
details: 'errorDetails',
|
|
||||||
stacktrace: 'errorStacktrace',
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
() => method.decodeEnvelope(errorData),
|
|
||||||
throwsA(predicate((PlatformException e) =>
|
|
||||||
e is PlatformException && e.stacktrace == 'errorStacktrace')));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group('Json method codec', () {
|
|
||||||
const JSONMethodCodec json = JSONMethodCodec();
|
|
||||||
test('should decode error envelope without native stacktrace', () {
|
|
||||||
final ByteData errorData = json.encodeErrorEnvelope(
|
|
||||||
code: 'errorCode',
|
|
||||||
message: 'errorMessage',
|
|
||||||
details: 'errorDetails',
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
() => json.decodeEnvelope(errorData),
|
|
||||||
throwsA(predicate((PlatformException e) =>
|
|
||||||
e is PlatformException &&
|
|
||||||
e.code == 'errorCode' &&
|
|
||||||
e.message == 'errorMessage' &&
|
|
||||||
e.details == 'errorDetails')));
|
|
||||||
});
|
|
||||||
test('should decode error envelope with native stacktrace.', () {
|
|
||||||
final ByteData errorData = json.encodeErrorEnvelope(
|
|
||||||
code: 'errorCode',
|
|
||||||
message: 'errorMessage',
|
|
||||||
details: 'errorDetails',
|
|
||||||
stacktrace: 'errorStacktrace',
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
() => json.decodeEnvelope(errorData),
|
|
||||||
throwsA(predicate((PlatformException e) =>
|
|
||||||
e is PlatformException && e.stacktrace == 'errorStacktrace')));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group('JSON message codec', () {
|
group('JSON message codec', () {
|
||||||
const MessageCodec<dynamic> json = JSONMessageCodec();
|
const MessageCodec<dynamic> json = JSONMessageCodec();
|
||||||
test('should encode and decode simple messages', () {
|
test('should encode and decode simple messages', () {
|
||||||
@ -210,22 +151,8 @@ void main() {
|
|||||||
standard,
|
standard,
|
||||||
1.0,
|
1.0,
|
||||||
<int>[
|
<int>[
|
||||||
6,
|
6, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0,
|
0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0,
|
|
||||||
0xf0,
|
|
||||||
0x3f,
|
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -131,7 +131,6 @@ void main() {
|
|||||||
'bad',
|
'bad',
|
||||||
'Something happened',
|
'Something happened',
|
||||||
<String, dynamic>{'a': 42, 'b': 3.14},
|
<String, dynamic>{'a': 42, 'b': 3.14},
|
||||||
'errorStacktrace',
|
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -142,7 +141,6 @@ void main() {
|
|||||||
expect(e.code, equals('bad'));
|
expect(e.code, equals('bad'));
|
||||||
expect(e.message, equals('Something happened'));
|
expect(e.message, equals('Something happened'));
|
||||||
expect(e.details, equals(<String, dynamic>{'a': 42, 'b': 3.14}));
|
expect(e.details, equals(<String, dynamic>{'a': 42, 'b': 3.14}));
|
||||||
expect(e.stacktrace, equals('errorStacktrace'));
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
fail('PlatformException expected');
|
fail('PlatformException expected');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user