Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Fix strong mode errors, warnings, and hints. #14

Merged
merged 14 commits into from
Oct 28, 2016
Merged

Fix strong mode errors, warnings, and hints. #14

merged 14 commits into from
Oct 28, 2016

Conversation

har79
Copy link
Contributor

@har79 har79 commented Oct 20, 2016

Part of dart-lang/tools#716.

  • Added cast to jsonDocument in StreamChannel<String>.transform(jsonDocument). jsonDocument is a StreamChannelTransformer<Object, String> which doesn't implicitly cast to the required StreamChannelTransformer<String, dynamic>. (Removed because fix didn't work -- will investigate better fix).
  • Change StreamChannel.transform(StreamChannelTransformer) to StreamChannelTransformer.bind(StreamChannel) which, according to the docs, is identical but has better type handling. (Instead of above initial attempt at fixing use of jsonDocument).
  • Added types to ignoreFormatExceptions of StreamTransformer<Object, Object>.
  • Changed declared return type of sendResponse and sendJsonResponse to void to match code.
  • Removed syncFuture as it relies on a deprecated method and, since Dart 1.7, is just the same as new Future.sync.
  • Some dartfmt churn.

@har79
Copy link
Contributor Author

har79 commented Oct 20, 2016

Ah, jsonDocument as StreamChannelTransformer<String, String> fails the tests.

@har79
Copy link
Contributor Author

har79 commented Oct 20, 2016

Fixed by using jsonDocument.bind(channel) instead of channel.transform(jsonDocument).

All tests pass and no output from dartanalyzer --strong.

"jsonrpc": "2.0",
"method": method
};
var message = <String, dynamic>{"jsonrpc": "2.0", "method": method};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these incidental reformattings. This package doesn't use dartfmt, and they make it harder to see the real changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

if (!isClosed) _manager.add(response);
}

/// Handles an individual parsed request.
Future _handleSingleRequest(request) {
return syncFuture(() {
return new Future.sync(() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just change this method to use async/await.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -170,7 +173,8 @@ class Server {
'result': result,
'id': request['id']
};
}).catchError((error, stackTrace) {
} catch (e, stackTrace) {
var error = e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just catch (error, stackTrace)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't work. Then error is final and the assignment in line 179 fails.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I'd just return the serialized exception directly in the is! RpcException case rather than re-assigning here. You could also pull out if (!request.containsKey('id')) return null to the beginning of the catch block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. After pulling the return null branch out; it seemed simpler to use a ternary for the two cases and assign to a temporary variable.

});
}

} catch (error, stackTrace) {
if (error.code != error_code.INVALID_REQUEST &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error.code may not be defined here, since we don't know anything about the type of error yet. I meant just pulling the request.containsKey() check here, and leaving the error.code check where it was.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pulling the request.containsKey() check would change the logic since it's part of an &&. Although, actually my change also changes the logic as the created RpcException could pass this check.

There doesn't seem to be any way to easily simplify this; returning in the if (error is! RpcException would result in quite a bit of duplication.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see the duplication... here's what I'd write:

} catch (error, stackTrace) {
  if (!request.containsKey("id")) return null;
  if (error is RpcException) return error.serialize(request);

  var chain = new Chain.forTrace(stackTrace);
  return new RpcException(error_code.SERVER_ERROR, getErrorMessage(error),
          data: {'full': error.toString(), 'stack': chain.toString()})
      .serialize(request);
}

Copy link
Contributor Author

@har79 har79 Oct 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However that drops the error.code != error_code.INVALID_REQUEST. Before, if error were an RpcException with code INVALID_REQUEST, it would be serialised and returned; now null may be returned instead. To preserve that logic, you would have to change the first check to:

if ((error is RpcException && error.code != error_code.INVALID_REQUEST || error is! RpcException) &&
      !request.containsKey('id')) {
    return null;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. What about:

} catch (error, stackTrace) {
  if (error is RpcException) {
    if (error.code != error_code.INVALID_REQUEST &&
        !request.containsKey("id")) {
      return null;
    } else {
      return error.serialize(request);
    }
  } else if (!request.containsKey("id")) {
    return null;
  }

  var chain = new Chain.forTrace(stackTrace);
  return new RpcException(error_code.SERVER_ERROR, getErrorMessage(error),
          data: {'full': error.toString(), 'stack': chain.toString()})
      .serialize(request);
}

There's still a little duplication, but it avoids allocating a RpcException just to return null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Gone for that, although with the booleans swapped around to use the positive conditions. Thanks!

}
});

var e = error is RpcException
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: try to avoid single-letter names.

'stack': '$chain',
}).serialize(request);
} else {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last nit: I prefer to avoid indentation by short-circuiting more than I prefer to use positive conditions, so I'd move the return null up earlier so you don't have to indent the RpcException creation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@nex3 nex3 merged commit d852cbc into dart-archive:master Oct 28, 2016
@har79 har79 deleted the strong branch October 28, 2016 22:19
mosuem pushed a commit to dart-lang/tools that referenced this pull request Oct 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

3 participants