Skip to content

Instantly share code, notes, and snippets.

@sunderee
Created February 24, 2025 12:43
Show Gist options
  • Save sunderee/bed6392fc7f4f6d3933be55f7485c79c to your computer and use it in GitHub Desktop.
Save sunderee/bed6392fc7f4f6d3933be55f7485c79c to your computer and use it in GitHub Desktop.
Native Dart helpers that use records to return the result or thrown error/exception object.
(T?, Object?) tryCatch<T extends Object>(T Function() function) {
try {
final result = function.call();
return (result, null);
} catch (error) {
return (null, error);
}
}
Future<(T?, Object?)> tryCatchAsync<T extends Object>(
Future<T> Function() function,
) async {
try {
final result = await function.call();
return (result, null);
} catch (error) {
return (null, error);
}
}
@sunderee
Copy link
Author

Below is the test file.

import 'package:flutter_test/flutter_test.dart';

(T?, Object?) tryCatch<T extends Object>(T Function() function) {
  try {
    final result = function.call();
    return (result, null);
  } catch (error) {
    return (null, error);
  }
}

Future<(T?, Object?)> tryCatchAsync<T extends Object>(
  Future<T> Function() function,
) async {
  try {
    final result = await function.call();
    return (result, null);
  } catch (error) {
    return (null, error);
  }
}

void main() {
  group('tryCatch', () {
    test('should return result and null error on success', () {
      final (result, error) = tryCatch(() => 42);

      expect(result, equals(42));
      expect(error, isNull);
    });

    test('should return null result and error on exception', () {
      final (result, error) = tryCatch(() => throw Exception('Test error'));

      expect(result, isNull);
      expect(error, isA<Exception>());
      expect((error as Exception).toString(), contains('Test error'));
    });

    test('should handle different return types', () {
      final (stringResult, stringError) = tryCatch(() => 'hello');
      expect(stringResult, equals('hello'));
      expect(stringError, isNull);

      final (listResult, listError) = tryCatch(() => [1, 2, 3]);
      expect(listResult, equals([1, 2, 3]));
      expect(listError, isNull);
    });
  });

  group('tryCatchAsync', () {
    test('should return result and null error on success', () async {
      final (result, error) = await tryCatchAsync(() async => 42);

      expect(result, equals(42));
      expect(error, isNull);
    });

    test('should return null result and error on exception', () async {
      final (result, error) = await tryCatchAsync(
        () async => throw Exception('Test error'),
      );

      expect(result, isNull);
      expect(error, isA<Exception>());
      expect((error as Exception).toString(), contains('Test error'));
    });

    test('should handle delayed operations', () async {
      final (result, error) = await tryCatchAsync(() async {
        await Future<void>.delayed(const Duration(milliseconds: 100));
        return 'delayed result';
      });

      expect(result, equals('delayed result'));
      expect(error, isNull);
    });

    test('should handle different return types', () async {
      final (stringResult, stringError) = await tryCatchAsync(
        () async => 'hello async',
      );
      expect(stringResult, equals('hello async'));
      expect(stringError, isNull);

      final (listResult, listError) = await tryCatchAsync(
        () async => [1, 2, 3],
      );
      expect(listResult, equals([1, 2, 3]));
      expect(listError, isNull);
    });
  });
}

@sunderee
Copy link
Author

If there's someone enthusiastic enough (or perhaps that's a note to myself at some point in the future), with this approach, we might loose stack trace. Refer to this comment on Twitter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment