Skip to content

Commit

Permalink
Use 0xffffffff as the fallback error ID
Browse files Browse the repository at this point in the history
  • Loading branch information
joshpeterson30489 committed Jan 7, 2021
1 parent 8c82c14 commit 172e8a9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/src/dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ class Dispatcher {
"Unknown message type: ${message.toDebugString()}");
}
} on ProtocolError catch (error) {
error.id = _inboundId(message) ?? -1;
error.id = _inboundId(message) ?? errorId;
stderr.write("Host caused ${error.type.name.toLowerCase()} error");
if (error.id != -1) stderr.write(" with request ${error.id}");
if (error.id != errorId) stderr.write(" with request ${error.id}");
stderr.writeln(": ${error.message}");
sendError(error);
// PROTOCOL error from https://bit.ly/2poTt90
Expand All @@ -99,7 +99,7 @@ class Dispatcher {
stderr.write("Internal compiler error: $errorMessage");
sendError(ProtocolError()
..type = ProtocolError_ErrorType.INTERNAL
..id = _inboundId(message) ?? -1
..id = _inboundId(message) ?? errorId
..message = errorMessage);
_channel.sink.close();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/host_callable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ sass.Callable hostCallable(Dispatcher dispatcher, FunctionRegistry functions,
// dart-lang/sdk#38790
throw "Unknown FunctionCallResponse.result $response.";
} on ProtocolError catch (error) {
error.id = -1;
error.id = errorId;
stderr.writeln("Host caused ${error.type.name.toLowerCase()} error: "
"${error.message}");
dispatcher.sendError(error);
Expand Down
11 changes: 7 additions & 4 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import 'package:source_span/source_span.dart';
import 'embedded_sass.pb.dart' as proto;
import 'embedded_sass.pb.dart' hide SourceSpan;

/// The special ID that indicates an error that's not associated with a specific
/// inbound request ID.
const errorId = 0xffffffff;

/// Returns a [ProtocolError] indicating that a mandatory field with the given
/// [fieldName] was missing.
ProtocolError mandatoryError(String fieldName) =>
Expand All @@ -16,10 +20,9 @@ ProtocolError mandatoryError(String fieldName) =>
/// Returns a [ProtocolError] indicating that the parameters for an inbound
/// message were invalid.
ProtocolError paramsError(String message) => ProtocolError()
// Set the ID to -1 by default, because that's the required value for errors
// that aren't associated with a specific inbound request ID. This will be
// overwritten by the dispatcher if a request ID is available.
..id = -1
// Set the ID to [errorId] by default. This will be overwritten by the
// dispatcher if a request ID is available.
..id = errorId
..type = ProtocolError_ErrorType.PARAMS
..message = message;

Expand Down
31 changes: 18 additions & 13 deletions test/function_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:test/test.dart';

import 'package:sass_embedded/src/embedded_sass.pb.dart';
import 'package:sass_embedded/src/utils.dart';

import 'embedded_process.dart';
import 'utils.dart';
Expand Down Expand Up @@ -391,7 +392,8 @@ void main() {
group("without alpha:", () {
group("hue", () {
test("0", () async {
expect(await _protofy('hsl(0, 50%, 50%)'), _rgb(191, 64, 64, 1.0));
expect(
await _protofy('hsl(0, 50%, 50%)'), _rgb(191, 64, 64, 1.0));
});

test("360", () async {
Expand All @@ -400,32 +402,34 @@ void main() {
});

test("below 0", () async {
expect(
await _protofy('hsl(-100, 50%, 50%)'), _rgb(106, 64, 191, 1.0));
expect(await _protofy('hsl(-100, 50%, 50%)'),
_rgb(106, 64, 191, 1.0));
});

test("between 0 and 360", () async {
expect(
await _protofy('hsl(100, 50%, 50%)'), _rgb(106, 191, 64, 1.0));
expect(await _protofy('hsl(100, 50%, 50%)'),
_rgb(106, 191, 64, 1.0));
});

test("above 360", () async {
expect(
await _protofy('hsl(560, 50%, 50%)'), _rgb(64, 149, 191, 1.0));
expect(await _protofy('hsl(560, 50%, 50%)'),
_rgb(64, 149, 191, 1.0));
});
});

group("saturation", () {
test("0", () async {
expect(await _protofy('hsl(0, 0%, 50%)'), _rgb(128, 128, 128, 1.0));
expect(
await _protofy('hsl(0, 0%, 50%)'), _rgb(128, 128, 128, 1.0));
});

test("100", () async {
expect(await _protofy('hsl(0, 100%, 50%)'), _rgb(255, 0, 0, 1.0));
});

test("in the middle", () async {
expect(await _protofy('hsl(0, 42%, 50%)'), _rgb(181, 74, 74, 1.0));
expect(
await _protofy('hsl(0, 42%, 50%)'), _rgb(181, 74, 74, 1.0));
});
});

Expand All @@ -435,12 +439,13 @@ void main() {
});

test("100", () async {
expect(
await _protofy('hsl(0, 50%, 100%)'), _rgb(255, 255, 255, 1.0));
expect(await _protofy('hsl(0, 50%, 100%)'),
_rgb(255, 255, 255, 1.0));
});

test("in the middle", () async {
expect(await _protofy('hsl(0, 50%, 42%)'), _rgb(161, 54, 54, 1.0));
expect(
await _protofy('hsl(0, 50%, 42%)'), _rgb(161, 54, 54, 1.0));
});
});
});
Expand Down Expand Up @@ -1242,7 +1247,7 @@ Future<void> _expectDeprotofyError(Value value, message) async {
..id = request.id
..success = value));

await expectParamsError(_process, -1, message);
await expectParamsError(_process, errorId, message);
await _process.kill();
}

Expand Down
7 changes: 4 additions & 3 deletions test/importer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;

import 'package:sass_embedded/src/embedded_sass.pb.dart';
import 'package:sass_embedded/src/utils.dart';

import 'embedded_process.dart';
import 'utils.dart';
Expand All @@ -30,7 +31,7 @@ void main() {

await expectParamsError(
process,
-1,
errorId,
"Response ID ${request.id + 1} doesn't match any outstanding "
"requests.");
await process.kill();
Expand All @@ -47,7 +48,7 @@ void main() {

await expectParamsError(
process,
-1,
errorId,
"Request ID ${request.id} doesn't match response type "
"InboundMessage_ImportResponse.");
await process.kill();
Expand Down Expand Up @@ -505,7 +506,7 @@ Future<void> _canonicalize(EmbeddedProcess process) async {
/// [message] on its protobuf stream and causes the compilation to fail.
Future<void> _expectImportParamsError(EmbeddedProcess process, message) async {
await expectLater(process.outbound,
emits(isProtocolError(-1, ProtocolError_ErrorType.PARAMS, message)));
emits(isProtocolError(errorId, ProtocolError_ErrorType.PARAMS, message)));

var failure = getCompileFailure(await process.outbound.next);
expect(failure.message, equals('Protocol error: $message'));
Expand Down
5 changes: 3 additions & 2 deletions test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'package:sass_embedded/src/embedded_sass.pb.dart';
import 'package:sass_embedded/src/utils.dart';

import 'embedded_process.dart';

Expand Down Expand Up @@ -39,7 +40,7 @@ InboundMessage compileString(String css,
/// [message] on its protobuf stream and prints a notice on stderr.
Future<void> expectParseError(EmbeddedProcess process, message) async {
await expectLater(process.outbound,
emits(isProtocolError(-1, ProtocolError_ErrorType.PARSE, message)));
emits(isProtocolError(errorId, ProtocolError_ErrorType.PARSE, message)));

var stderrPrefix = "Host caused parse error: ";
await expectLater(
Expand All @@ -56,7 +57,7 @@ Future<void> expectParamsError(EmbeddedProcess process, int id, message) async {
emits(isProtocolError(id, ProtocolError_ErrorType.PARAMS, message)));

var stderrPrefix = "Host caused params error"
"${id == -1 ? '' : " with request $id"}: ";
"${id == errorId ? '' : " with request $id"}: ";
await expectLater(
process.stderr,
message is String
Expand Down

0 comments on commit 172e8a9

Please sign in to comment.