简体   繁体   中英

Elegant error handling in Dart like Scala's `Try`

A data class in Dart:

import 'package:validate/validate.dart';
class AuthUser {
  final String email, token, username, bio, image;

  AuthUser(this.email, this.token, this.username, this.bio, this.image) {
    Validate.isEmail(this.email);
  }

  @override
  String toString() {
    return 'AuthUser{email: $email, token: $token, username: $username, bio: $bio, image: $image}';
  }
}

where Validate.isEmail will throws an Error when failed to match:

static void matchesPattern(String input, RegExp pattern,[String message = DEFAULT_MATCHES_PATTERN_EX]) {
    if (pattern.hasMatch(input) == false) {
        throw new ArgumentError(message);
    }
}

static void isEmail(String input,[String message = DEFAULT_MATCHES_PATTERN_EX]) {
    matchesPattern(input,new RegExp(PATTERN_EMAIL),message);
}

Now I want to use an elegant way to new this class. When using Scala, I can use Try(new AuthUser(...)) and patten-matching it.

And in Dart, first I tried RxDart,

void main() {
  testWidgets('Counter increments smoke test', (WidgetTester tester) async {
    Observable.just(AuthUser("email", "token", "username", "bio", "img"))
        .doOnError((e, s) => print("oh no"))
        .listen((e) => print(e.toString()));
  });
}

Not work, the test failed for the error(which means RxDart doesn't catch errors at all!!!)

And I want to try Future, failed also.

And I want to use dartz, but I am worried because there is just one maintainer...

Any advice?

If you are OK with using Future what's wrong with this advice: Using Future.sync() to wrap your code ? The code will look like this:

void main() {
  var f = Future.sync(() {AuthUser("email", "token", "username", "bio", "img"); });
  f.then((v) =>  print("Value: " + v.toString())).catchError((e) => print("Failure: " +e.toString()));
}

The main trick is that Future.sync effectively enables lazy evaluation of the parameter but you have to pass your parameter wrapped in a function. This is actually the same trick Scala compiler does for Try (ie for call-by-name parameters) but takes adding a few brackets around.

If you only want the basic functionality of returning either type based on whether an exception occurred or not then you can easily create a utility class such as below.

Otherwise I recommend @SergGr's answer about using Future.sync since it gives you more monadic like pipeline.

void main() {
  Try<Error, void> result = Try.it(() => Validate.isEmail("test-example.com"));

  if (result is Success) {
    print("Good");
  } else if (result is Failure) {
    print("Error: " + result.exception().toString());
  }
}

typedef TryExec<V> = V Function();

abstract class Try<E extends Error, V> {

  static Try<E, V> it<E extends Error, V>(TryExec<V> fn) {
    try {
      return Try.success(fn());
    } catch (e) {
      return Try.failure(e);
    }
  }

  static Try<E, V> failure<E extends Error, V>(Error e) {
    return new Failure(e);
  }

  static Try<E, V> success<E extends Error, V>(V v) {
    return new Success(v);
  }
}

class Failure<E extends Error, V> extends Try<E, V> {
  final E _e;
  Failure(this._e);

  E exception() => _e;
}

class Success<E extends Error, V> extends Try<E, V> { 
  final V _v;
  Success(this._v);

  V value() => _v;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM