Skip to content

Commit 61af9ee

Browse files
authored
Use a common error handler for embedded protocol errors (#2027)
1 parent 658eb70 commit 61af9ee

File tree

4 files changed

+31
-47
lines changed

4 files changed

+31
-47
lines changed

lib/src/embedded/dispatcher.dart

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'dart:typed_data';
1010
import 'package:path/path.dart' as p;
1111
import 'package:protobuf/protobuf.dart';
1212
import 'package:sass/sass.dart' as sass;
13-
import 'package:stack_trace/stack_trace.dart';
1413
import 'package:stream_channel/stream_channel.dart';
1514

1615
import 'embedded_sass.pb.dart';
@@ -116,24 +115,8 @@ class Dispatcher {
116115
throw parseError(
117116
"Unknown message type: ${message.toDebugString()}");
118117
}
119-
} on ProtocolError catch (error) {
120-
// Always set the ID to [errorId] because we're only ever reporting
121-
// errors for responses or for [CompileRequest] which has no ID.
122-
error.id = errorId;
123-
stderr.write("Host caused ${error.type.name.toLowerCase()} error");
124-
if (error.id != errorId) stderr.write(" with request ${error.id}");
125-
stderr.writeln(": ${error.message}");
126-
sendError(error);
127-
// PROTOCOL error from https://bit.ly/2poTt90
128-
exitCode = 76;
129-
_channel.sink.close();
130-
} catch (error, stackTrace) {
131-
var errorMessage = "$error\n${Chain.forTrace(stackTrace)}";
132-
stderr.write("Internal compiler error: $errorMessage");
133-
sendError(ProtocolError()
134-
..type = ProtocolErrorType.INTERNAL
135-
..id = errorId
136-
..message = errorMessage);
118+
} on ProtocolError catch (error, stackTrace) {
119+
sendError(handleError(error, stackTrace));
137120
_channel.sink.close();
138121
}
139122
}).asFuture<void>();

lib/src/embedded/host_callable.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
// ignore: deprecated_member_use
66
import 'dart:cli';
7-
import 'dart:io';
87

98
import '../callable.dart';
109
import '../exception.dart';
@@ -51,11 +50,8 @@ Callable hostCallable(
5150
case InboundMessage_FunctionCallResponse_Result.notSet:
5251
throw mandatoryError('FunctionCallResponse.result');
5352
}
54-
} on ProtocolError catch (error) {
55-
error.id = errorId;
56-
stderr.writeln("Host caused ${error.type.name.toLowerCase()} error: "
57-
"${error.message}");
58-
dispatcher.sendError(error);
53+
} on ProtocolError catch (error, stackTrace) {
54+
dispatcher.sendError(handleError(error, stackTrace));
5955
throw error.message;
6056
}
6157
});

lib/src/embedded/isolate_dispatcher.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
import 'dart:async';
66
import 'dart:ffi';
7-
import 'dart:io';
87
import 'dart:isolate';
98
import 'dart:typed_data';
109

1110
import 'package:pool/pool.dart';
1211
import 'package:protobuf/protobuf.dart';
13-
import 'package:stack_trace/stack_trace.dart';
1412
import 'package:stream_channel/isolate_channel.dart';
1513
import 'package:stream_channel/stream_channel.dart';
1614

@@ -199,26 +197,9 @@ class IsolateDispatcher {
199197
/// responded to, if available.
200198
void _handleError(Object error, StackTrace stackTrace,
201199
{int? compilationId, int? messageId}) {
202-
if (error is ProtocolError) {
203-
error.id = messageId ?? errorId;
204-
stderr.write("Host caused ${error.type.name.toLowerCase()} error");
205-
if (error.id != errorId) stderr.write(" with request ${error.id}");
206-
stderr.writeln(": ${error.message}");
207-
sendError(compilationId ?? errorId, error);
208-
// PROTOCOL error from https://bit.ly/2poTt90
209-
exitCode = 76;
210-
_channel.sink.close();
211-
} else {
212-
var errorMessage = "$error\n${Chain.forTrace(stackTrace)}";
213-
stderr.write("Internal compiler error: $errorMessage");
214-
sendError(
215-
compilationId ?? errorId,
216-
ProtocolError()
217-
..type = ProtocolErrorType.INTERNAL
218-
..id = messageId ?? errorId
219-
..message = errorMessage);
220-
_channel.sink.close();
221-
}
200+
sendError(compilationId ?? errorId,
201+
handleError(error, stackTrace, messageId: messageId));
202+
_channel.sink.close();
222203
}
223204

224205
/// Sends [message] to the host.

lib/src/embedded/utils.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// MIT-style license that can be found in the LICENSE file or at
33
// https://opensource.org/licenses/MIT.
44

5+
import 'dart:io';
56
import 'dart:typed_data';
67

78
import 'package:protobuf/protobuf.dart';
89
import 'package:source_span/source_span.dart';
10+
import 'package:stack_trace/stack_trace.dart';
911
import 'package:term_glyph/term_glyph.dart' as term_glyph;
1012

1113
import '../syntax.dart';
@@ -134,3 +136,25 @@ final _compilationIdBuilder = VarintBuilder(32, 'compilation ID');
134136
_compilationIdBuilder.reset();
135137
}
136138
}
139+
140+
/// Wraps error object into ProtocolError, writes error to stderr, and returns the ProtocolError.
141+
ProtocolError handleError(Object error, StackTrace stackTrace,
142+
{int? messageId}) {
143+
if (error is ProtocolError) {
144+
error.id = messageId ?? errorId;
145+
stderr.write("Host caused ${error.type.name.toLowerCase()} error");
146+
if (error.id != errorId) stderr.write(" with request ${error.id}");
147+
stderr.writeln(": ${error.message}");
148+
// PROTOCOL error from https://bit.ly/2poTt90
149+
exitCode = 76; // EX_PROTOCOL
150+
return error;
151+
} else {
152+
var errorMessage = "$error\n${Chain.forTrace(stackTrace)}";
153+
stderr.write("Internal compiler error: $errorMessage");
154+
exitCode = 70; // EX_SOFTWARE
155+
return ProtocolError()
156+
..type = ProtocolErrorType.INTERNAL
157+
..id = messageId ?? errorId
158+
..message = errorMessage;
159+
}
160+
}

0 commit comments

Comments
 (0)