2

コンプリータからエラーをキャッチしようとしています。

ここで、トークンをデコードする私の方法

  Future<Map> decode(String token) {
    var completer = new Completer();

    new Future(() {
      List<String> parts = token.split(".");
      Map result = {};

      try {
        result["header"] = JSON.decode(new String.fromCharCodes(crypto.CryptoUtils.base64StringToBytes(parts[0])));
        result["payload"] = JSON.decode(new String.fromCharCodes(crypto.CryptoUtils.base64StringToBytes(parts[1])));
      } catch(e) {
        completer.completeError("Bad token");
        return;
      }
      encode(result["payload"]).then((v_token) {
        if (v_token == token) {
          completer.complete(result);
        } else {
          completer.completeError("Bad signature");
        }
      });
    });
    return completer.future;
  }
}

呼び出し:

  var test = new JsonWebToken("topsecret");

  test.encode({"field": "ok"}).then((token) {
    print(token);
    test.decode("bad.jwt.here")
      ..then((n_tok) => print(n_tok))
      ..catchError((e) => print(e));
  });

そして、これが出力です

dart server.dart
eyJ0eXAiOiJKV1QiLCJhbGciOiJTSEEyNTYifQ==.eyJsdSI6Im9rIn0=.E3TjGiPGSJOIVZFFECJ0OSr0jAWojIfF7MqFNTbFPmI=
Bad token
Unhandled exception:
Uncaught Error: Bad token
#0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:820)
#1      _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2      _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:126)

印刷中にエラーがキャッチされないと私に言う理由がわかりません...

4

2 に答える 2

3

の代わりに .. を誤用したと思います。連鎖する未来のために。https://www.dartlang.org/docs/tutorials/futures/#handling-errorsを参照してください

それ以外の

test.decode("bad.jwt.here")
  ..then((n_tok) => print(n_tok))
  ..catchError((e) => print(e));

試してみませんか

test.decode("bad.jwt.here")
  .then((n_tok) => print(n_tok))
  .catchError((e) => print(e));
于 2014-10-14T11:50:30.423 に答える