Skip to content

Commit

Permalink
prepare error for serialization & passing the thread boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Apr 17, 2019
1 parent 3640a91 commit f1a4d2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RpcProvider } from 'worker-rpc';
import { NormalizedMessage } from './NormalizedMessage';
import { Message } from './Message';
import { RunPayload, RunResult, RUN } from './RpcTypes';
import { prepareErrorForSerialization } from './util';

// fork workers...
const division = parseInt(process.env.WORK_DIVISION || '', 10);
Expand Down Expand Up @@ -51,7 +52,9 @@ parentRpc.registerRpcHandler<RunPayload, RunResult>(RUN, async message => {
workerRpcs.map(workerRpc =>
workerRpc.rpc<RunPayload, RunResult>(RUN, message)
)
);
).catch(e => {
throw prepareErrorForSerialization(e);
});

function workerFinished(
workerResult: (Message | undefined)[]
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ class ForkTsCheckerWebpackPlugin {
this.logger.error(
this.colors.red(
'Error during checking: ' +
(error ? error.toString() : 'Unknown error')
(error
? error.message || error.toString()
: 'Unknown error')
)
);
}
Expand Down Expand Up @@ -477,7 +479,9 @@ class ForkTsCheckerWebpackPlugin {
this.logger.error(
this.colors.red(
'Error during checking: ' +
(error ? error.toString() : 'Unknown error')
(error
? error.message || error.toString()
: 'Unknown error')
)
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from './NormalizedMessageFactories';
import { RpcProvider } from 'worker-rpc';
import { RunPayload, RunResult, RUN } from './RpcTypes';
import { prepareErrorForSerialization } from './util';

const rpc = new RpcProvider(message => {
try {
Expand Down Expand Up @@ -91,7 +92,9 @@ async function run(cancellationToken: CancellationToken) {

rpc.registerRpcHandler<RunPayload, RunResult>(RUN, message =>
typeof message !== 'undefined'
? run(CancellationToken.createFromJSON(typescript, message!))
? run(CancellationToken.createFromJSON(typescript, message!)).catch(e => {
throw prepareErrorForSerialization(e);
})
: undefined
);

Expand Down
23 changes: 23 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function prepareErrorForSerialization(e: any) {
const prepared = {
message: 'unkonwn error',
stack: 'no stack trace available',
fileName: 'unknown',
lineNumber: 'unknown',
columnNumber: 'unknown'
};

if (e) {
if (typeof e === 'string') {
prepared.message = e;
} else {
for (const key of Object.keys(prepared)) {
if (typeof e[key] !== 'undefined') {
prepared[key] = String(e[key]);
}
}
}
}

return prepared;
}

0 comments on commit f1a4d2a

Please sign in to comment.