Skip to content

Commit c1d25d6

Browse files
committed
gracefully handle error thrown from the service
1 parent 0cc9d8c commit c1d25d6

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fork-ts-checker-webpack-plugin",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Runs typescript type checker and linter on separate process.",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/NormalizedMessage.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type ErrorType = 'diagnostic' | 'lint';
1+
export type ErrorType = 'diagnostic' | 'lint' | 'internal';
22
export type Severity = 'error' | 'warning';
33

44
interface NormalizedMessageJson {
@@ -9,11 +9,13 @@ interface NormalizedMessageJson {
99
file?: string;
1010
line?: number;
1111
character?: number;
12+
stack?: string;
1213
}
1314

1415
export class NormalizedMessage {
1516
public static readonly TYPE_DIAGNOSTIC: ErrorType = 'diagnostic';
1617
public static readonly TYPE_LINT: ErrorType = 'lint';
18+
public static readonly TYPE_INTERNAL: ErrorType = 'internal';
1719

1820
// severity types
1921
public static readonly SEVERITY_ERROR: Severity = 'error';
@@ -26,6 +28,7 @@ export class NormalizedMessage {
2628
public readonly file?: string;
2729
public readonly line?: number;
2830
public readonly character?: number;
31+
public readonly stack?: string;
2932

3033
constructor(data: NormalizedMessageJson) {
3134
this.type = data.type;
@@ -35,6 +38,7 @@ export class NormalizedMessage {
3538
this.file = data.file;
3639
this.line = data.line;
3740
this.character = data.character;
41+
this.stack = data.stack;
3842
}
3943

4044
public static createFromJSON(json: NormalizedMessageJson) {
@@ -73,6 +77,10 @@ export class NormalizedMessage {
7377
messageA.content,
7478
messageB.content
7579
) ||
80+
NormalizedMessage.compareOptionalStrings(
81+
messageA.stack,
82+
messageB.stack
83+
) ||
7684
0 /* EqualTo */
7785
);
7886
}
@@ -149,7 +157,8 @@ export class NormalizedMessage {
149157
content: this.content,
150158
file: this.file,
151159
line: this.line,
152-
character: this.character
160+
character: this.character,
161+
stack: this.stack
153162
} as NormalizedMessageJson;
154163
}
155164

@@ -161,6 +170,10 @@ export class NormalizedMessage {
161170
return NormalizedMessage.TYPE_LINT === this.type;
162171
}
163172

173+
public isInternalType() {
174+
return NormalizedMessage.TYPE_INTERNAL === this.type;
175+
}
176+
164177
public getFormattedCode() {
165178
return this.isDiagnosticType() ? 'TS' + this.code : this.code;
166179
}

src/formatter/codeframeFormatter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export function createCodeframeFormatter(options: any) {
2222
: colors.bold.red;
2323
const positionColor = colors.dim;
2424

25+
if (message.isInternalType()) {
26+
return (
27+
messageColor(`INTERNAL ${message.severity.toUpperCase()}: `) +
28+
message.content +
29+
(message.stack
30+
? os.EOL + 'stack trace:' + os.EOL + colors.gray(message.stack)
31+
: '')
32+
);
33+
}
34+
2535
const file = message.file;
2636
const source =
2737
file && FsHelper.existsSync(file) && fs.readFileSync(file, 'utf-8');

src/formatter/defaultFormatter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export function createDefaultFormatter() {
1919
const fileAndNumberColor = colors.bold.cyan;
2020
const codeColor = colors.grey;
2121

22+
if (message.isInternalType()) {
23+
return (
24+
messageColor(`INTERNAL ${message.severity.toUpperCase()}: `) +
25+
message.content +
26+
(message.stack
27+
? os.EOL + 'stack trace:' + os.EOL + colors.gray(message.stack)
28+
: '')
29+
);
30+
}
31+
2232
return [
2333
messageColor(`${message.severity.toUpperCase()} in `) +
2434
fileAndNumberColor(

src/service.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ async function run(cancellationToken: CancellationToken) {
6464
let diagnostics: NormalizedMessage[] = [];
6565
let lints: NormalizedMessage[] = [];
6666

67-
checker.nextIteration();
68-
6967
try {
68+
checker.nextIteration();
69+
7070
diagnostics = await checker.getDiagnostics(cancellationToken);
7171
if (checker.hasLinter()) {
7272
lints = checker.getLints(cancellationToken);
@@ -76,7 +76,26 @@ async function run(cancellationToken: CancellationToken) {
7676
return undefined;
7777
}
7878

79-
throw error;
79+
diagnostics.push(
80+
new NormalizedMessage(
81+
error instanceof Error
82+
? {
83+
type: NormalizedMessage.TYPE_INTERNAL,
84+
severity: NormalizedMessage.SEVERITY_ERROR,
85+
code: '',
86+
content: error.message,
87+
stack: error.stack,
88+
file: '[internal]'
89+
}
90+
: {
91+
type: NormalizedMessage.TYPE_INTERNAL,
92+
severity: NormalizedMessage.SEVERITY_ERROR,
93+
code: '',
94+
content: error.toString(),
95+
file: '[internal]'
96+
}
97+
)
98+
);
8099
}
81100

82101
if (cancellationToken.isCancellationRequested()) {

0 commit comments

Comments
 (0)