-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port parts of Structured Errors to TS + Add two more errors #20597
Changes from 12 commits
38e3b6a
dffa76f
37546e1
5655304
2bcf5a8
dd609b1
cd1437f
6844972
c2a11bf
e1de81a
9a2a3f6
7cde48f
33e78ee
4f7b7a3
bb22972
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Joi from "@hapi/joi" | ||
import stackTrace from "stack-trace" | ||
import errorSchema from "./error-schema" | ||
import { errorMap, defaultError, IErrorMapEntry } from "./error-map" | ||
import { sanitizeStructuredStackTrace } from "../reporter/errors" | ||
|
||
interface IConstructError { | ||
details: { | ||
id?: keyof typeof errorMap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now allows for auto completion of all error IDs 👍 |
||
context?: Record<string, string> | ||
error?: string | ||
[key: string]: unknown | ||
} | ||
} | ||
|
||
interface ILocationPosition { | ||
line: number | ||
column: number | ||
} | ||
|
||
interface IStructuredError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This schema is more or less copied from the |
||
code?: string | ||
text: string | ||
stack: { | ||
fileName: string | ||
functionName?: string | ||
lineNumber?: number | ||
columnNumber?: number | ||
}[] | ||
filePath?: string | ||
location?: { | ||
start: ILocationPosition | ||
end?: ILocationPosition | ||
} | ||
error?: unknown | ||
group?: string | ||
level: IErrorMapEntry["level"] | ||
type?: IErrorMapEntry["type"] | ||
docsUrl?: string | ||
} | ||
|
||
// Merge partial error details with information from the errorMap | ||
// Validate the constructed object against an error schema | ||
const constructError = ({ | ||
details: { id, ...otherDetails }, | ||
}: IConstructError): IStructuredError => { | ||
const result: IErrorMapEntry = (id && errorMap[id]) || defaultError | ||
|
||
// merge | ||
const structuredError: IStructuredError = { | ||
context: {}, | ||
...otherDetails, | ||
...result, | ||
text: result.text(otherDetails.context), | ||
stack: otherDetails.error | ||
? sanitizeStructuredStackTrace(stackTrace.parse(otherDetails.error)) | ||
: null, | ||
docsUrl: result.docsUrl || `https://gatsby.dev/issue-how-to`, | ||
} | ||
|
||
if (id) { | ||
structuredError.code = id | ||
} | ||
|
||
// validate | ||
const { error } = Joi.validate(structuredError, errorSchema) | ||
if (error !== null) { | ||
console.log(`Failed to validate error`, error) | ||
process.exit(1) | ||
} | ||
|
||
return structuredError | ||
} | ||
|
||
export default constructError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lol what a goofy syntax