explain method

String explain({
  1. bool includeSensitive = false,
})

The diagnostic as one printable block; includeSensitive adds query values, messages and the body, which carry customer data.

Implementation

String explain({bool includeSensitive = false}) {
  final lines = StringBuffer(toString());

  final info = requestInfo;
  if (info is RequestDetail) {
    if (info.queryParams.isNotEmpty) {
      // Names only by default: the values are the customer's own data.
      final names = info.queryParams.keys.toList()..sort();
      lines.write(includeSensitive
          ? '\n  params  : ${_flatten(Redaction.redactValue(info.queryParams))}'
          : '\n  params  : ${_flatten(names.join(', '))}');
    }
    if (info.attemptCount > 0 && info.maxAttempts > 0) {
      lines.write('\n  attempt : ${info.attemptCount}/${info.maxAttempts}');
    }
    lines.write('\n  elapsed : ${info.elapsedSeconds}s');
    final url = info.url;
    if (includeSensitive && url != null && url.isNotEmpty) {
      lines.write('\n  url     : ${_flatten(Redaction.redactText(url))}');
    }
  }

  // The v4 envelope's identifiers name the failure, not the customer's data.
  final code = errorCode;
  if (code != null) lines.write('\n  code    : ${_flatten(code)}');
  final id = requestId;
  if (id != null) lines.write('\n  request : ${_flatten(id)}');

  final location = _redirectTarget(responseHeaders);
  if (location != null && includeSensitive) {
    lines.write('\n  location: ${_flatten(Redaction.redactText(location))}');
  }

  final reported = getErrorMessages();
  if (reported.isNotEmpty) {
    if (includeSensitive) {
      lines.write('\n  reported:');
      for (final reportedMessage in reported) {
        lines.write(
            '\n    - ${_flatten(Redaction.redactText(reportedMessage))}');
      }
    } else {
      // Counted, not quoted: a message from this API quotes the rejected value.
      lines.write('\n  reported: ${reported.length} message(s) from the '
          'server, withheld');
    }
  }

  final hint = getHint();
  if (hint != null) lines.write('\n  hint    : $hint');

  if (rawResponse != null) {
    if (includeSensitive) {
      var body = _flatten(Redaction.redactValue(rawResponse));
      if (body.length > 500) {
        body = '${body.substring(0, 500)}... +${body.length - 500} chars';
      }
      lines.write('\n  body    : $body');
    } else {
      lines.write('\n  body    : ${describeSafeShape(rawResponse)}');
    }
  }
  if (!includeSensitive) {
    lines.write('\n  detail  : explain(includeSensitive: true) adds the '
        'values, the messages and the body');
  }
  return lines.toString();
}