errorStatusMessage property

String get errorStatusMessage

Error message — prefers the message sent by the backend.

Implementation

String get errorStatusMessage {
  final body = _errorBody;

  // 1. The most informative field in the backend response body
  if (body != null) {
    const keys = [
      'detail',
      'Detail',
      'message',
      'Message',
      'error_description',
      'errorDescription',
      'title',
      'Title',
      'error',
      'Error',
    ];
    for (final key in keys) {
      final candidate = body[key];
      if (candidate is String && candidate.trim().isNotEmpty) {
        return candidate;
      }
    }

    // Validation errors — {errors: {field: [msg]}}
    final errors = body['errors'] ?? body['Errors'];
    if (errors is Map && errors.isNotEmpty) {
      final firstField = errors.values.first;
      if (firstField is List && firstField.isNotEmpty) {
        return '${firstField.first}';
      }
      if (firstField is String) return firstField;
    }
  }

  // 2. Body is plain text
  final rawData = _effectiveResponse?.data;
  if (rawData is String && rawData.trim().isNotEmpty && body == null) {
    return rawData.trim();
  }

  // 3. HTTP statusMessage
  final httpMsg = _effectiveResponse?.statusMessage;
  if (httpMsg != null && httpMsg.trim().isNotEmpty) return httpMsg;

  // 4. Dio exception message
  final exceptionMsg = dioError?.message;
  if (exceptionMsg != null && exceptionMsg.trim().isNotEmpty) {
    return exceptionMsg;
  }

  // 5. Dio error type
  if (dioError != null) return _errorTypeReadable(dioError!.type);

  return 'Unknown error occurred';
}