-
Notifications
You must be signed in to change notification settings - Fork 35
Fix strong mode errors, warnings, and hints. #14
Conversation
Ah, |
… due to better type handling.
Fixed by using All tests pass and no output from |
"jsonrpc": "2.0", | ||
"method": method | ||
}; | ||
var message = <String, dynamic>{"jsonrpc": "2.0", "method": method}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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(() { |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just catch (error, stackTrace)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 && |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
This reverts commit 5028cec.
Part of dart-lang/tools#716.
Added cast to(Removed because fix didn't work -- will investigate better fix).jsonDocument
inStreamChannel<String>.transform(jsonDocument)
.jsonDocument
is aStreamChannelTransformer<Object, String>
which doesn't implicitly cast to the requiredStreamChannelTransformer<String, dynamic>
.StreamChannel.transform(StreamChannelTransformer)
toStreamChannelTransformer.bind(StreamChannel)
which, according to the docs, is identical but has better type handling. (Instead of above initial attempt at fixing use ofjsonDocument
).ignoreFormatExceptions
ofStreamTransformer<Object, Object>
.sendResponse
andsendJsonResponse
tovoid
to match code.syncFuture
as it relies on a deprecated method and, since Dart 1.7, is just the same asnew Future.sync
.