serialize method Null safety

Map<String, dynamic> serialize(
  1. dynamic error,
  2. StackTrace stackTrace
)

Serializes error and stackTrace into a JSON-safe object.

Other than JSON- and isolate-safety, no guarantees are made about the serialized format.

Implementation

static Map<String, dynamic> serialize(error, StackTrace stackTrace) {
  String? message;
  if (error is String) {
    message = error;
  } else {
    try {
      message = error.message.toString();
    } on NoSuchMethodError catch (_) {
      // Do nothing.
    }
  }

  final supertype = (error is TestFailure) ? 'TestFailure' : null;

  return {
    'message': message,
    'type': error.runtimeType.toString(),
    'supertype': supertype,
    'toString': error.toString(),
    'stackChain': Chain.forTrace(stackTrace).toString()
  };
}