-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw module server errors from runJS (#190)
Co-authored-by: Gerardo Rodriguez <gerardo@cloudfour.com>
- Loading branch information
1 parent
0df14e2
commit 9fb149d
Showing
14 changed files
with
437 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'pleasantest': minor | ||
--- | ||
|
||
Improve error message output for resolution errors and syntax errors/transform errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// This makes it so that errors thrown by the module server get re-thrown inside of runJS/loadJS. | ||
// This is necessary because the network is between the runJS call and the module server, so errors do not propagate | ||
// By tracking which runJS/loadJS initiated the request for each file, the module server can know which runJS/loadJS needs to reject. | ||
// When runJS finishes, it will check to see if the buildStatuses map has any errors corresponding to its buildId. | ||
// If there are any errors, it throws the first one. | ||
|
||
const buildStatuses = new Map<number, Error[]>(); | ||
|
||
let buildIds = 0; | ||
|
||
/** | ||
* Add an error for a specific buildId. | ||
* If the build is already finished (resolved), | ||
* triggers an uncaught rejection, with the hopes that it will fail the test. | ||
*/ | ||
export const rejectBuild = (buildId: number, error: Error) => { | ||
const statusArray = buildStatuses.get(buildId); | ||
if (statusArray) statusArray.push(error); | ||
// Uncaught promise rejection! | ||
// Hope that Jest will catch it and fail the test, otherwise it is just logged by Node | ||
else Promise.reject(error); | ||
}; | ||
|
||
export const createBuildStatusTracker = () => { | ||
const buildId = ++buildIds; | ||
buildStatuses.set(buildId, []); | ||
return { | ||
buildId, | ||
complete() { | ||
const status = buildStatuses.get(buildId); | ||
// This should never happen | ||
if (!status) throw new Error('Build already completed'); | ||
buildStatuses.delete(buildId); | ||
if (status.length > 0) return status; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as colors from 'kolorist'; | ||
import { createCodeFrame } from 'simple-code-frame'; | ||
import { promises as fs } from 'fs'; | ||
|
||
export class ErrorWithLocation extends Error { | ||
filename?: string; | ||
line: number; | ||
column?: number; | ||
constructor({ | ||
message, | ||
filename, | ||
line, | ||
column, | ||
}: { | ||
message: string; | ||
filename?: string; | ||
line: number; | ||
column?: number; | ||
}) { | ||
super(message); | ||
this.filename = filename; | ||
this.line = line; | ||
this.column = column; | ||
} | ||
|
||
async toCodeFrame() { | ||
if (!this.filename) | ||
throw new Error('filename missing in ErrorWithLocation'); | ||
|
||
const originalCode = await fs.readFile(this.filename, 'utf8'); | ||
const frame = createCodeFrame( | ||
originalCode, | ||
this.line - 1, | ||
this.column || 0, | ||
); | ||
const message = `${colors.red(this.message)} | ||
${colors.blue( | ||
`${this.filename}:${this.line}${ | ||
this.column === undefined ? '' : `:${this.column + 1}` | ||
}`, | ||
)} | ||
${frame}`; | ||
const modifiedError = new Error(message); | ||
modifiedError.stack = message; | ||
return modifiedError; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.