cleanCashuErrorMessage function

String cleanCashuErrorMessage(
  1. String raw
)

Strips exception prefixes and noisy JSON bodies from error messages so they read cleanly in the UI.

Implementation

String cleanCashuErrorMessage(String raw) {
  var message = raw.trim();
  for (final prefix in const [
    'Exception: ',
    'StateError: ',
    'Bad state: ',
    'HttpException: ',
    'SocketException: ',
    'TimeoutException: ',
  ]) {
    if (message.startsWith(prefix)) {
      message = message.substring(prefix.length).trim();
      break;
    }
  }
  // mint API errors often embed a JSON body; prefer the `detail` field
  final detailMatch = RegExp(
    r'"detail"\s*:\s*"([^"\\]*(?:\\.[^"\\]*)*)"',
  ).firstMatch(message);
  if (detailMatch != null) return detailMatch.group(1)!;
  return message;
}