formattedStatusEx method

MsgConvResult formattedStatusEx(
  1. IStatus status
)

Formats an error message (like formattedStatus), but additionally informs whether the error message was succesfully converted from UTF-8 and provides the raw byte array of the message (before any decoding).

Implementation

MsgConvResult formattedStatusEx(IStatus status) {
  try {
    const bufSize = 512;
    Pointer<Utf8> buf = mem.allocate(bufSize);
    try {
      final len = formatStatus(buf, bufSize, status);
      final codePoints = buf.toDartMem(len);
      String str;
      bool converted = false;
      try {
        str = Utf8Decoder(allowMalformed: false).convert(codePoints);
        converted = true;
      } on FormatException {
        // decoding from UTF-8 failed, will decode whatever can be salvaged
        str = Utf8Decoder(allowMalformed: true).convert(codePoints);
        converted = false;
      }
      return MsgConvResult(str, converted, codePoints);
    } finally {
      mem.free(buf);
    }
  } catch (_) {
    // automatic format didn't work, will try to return the status
    // formatted by hand
    final errors = status.errors.where((e) => e != 0);
    final msg = "Could not format error message. Error codes: $errors";
    return MsgConvResult(msg, true, Utf8Encoder().convert(msg));
  }
}