-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port parts of Structured Errors to TS + Add two more errors (#20597)
* Convert first error files over to TS + add Joi typings * Type more stuff * Error 85926 * Rename tests * Error 85927 * Fix ts type errors for test * WIP: Warn on using .gif in image sharp * Some more typing stuff * Remove WIP .gif stuff * Convert another test to TS * Update snapshot, add describe block * Update snapshot * Fix missing return type * Fix type error and explicitly type error map Co-authored-by: Matt Kane <matt@gatsbyjs.com>
- Loading branch information
Showing
17 changed files
with
366 additions
and
216 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
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
11 changes: 7 additions & 4 deletions
11
...tured-errors/__tests__/construct-error.js → ...tured-errors/__tests__/construct-error.ts
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
2 changes: 1 addition & 1 deletion
2
.../structured-errors/__tests__/error-map.js → .../structured-errors/__tests__/error-map.ts
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
2 changes: 1 addition & 1 deletion
2
...ructured-errors/__tests__/error-schema.js → ...ructured-errors/__tests__/error-schema.ts
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
39 changes: 0 additions & 39 deletions
39
packages/gatsby-cli/src/structured-errors/construct-error.js
This file was deleted.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
packages/gatsby-cli/src/structured-errors/construct-error.ts
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,75 @@ | ||
import Joi from "@hapi/joi" | ||
import stackTrace from "stack-trace" | ||
import errorSchema from "./error-schema" | ||
import { errorMap, defaultError, IErrorMapEntry, ErrorId } from "./error-map" | ||
import { sanitizeStructuredStackTrace } from "../reporter/errors" | ||
|
||
interface IConstructError { | ||
details: { | ||
id?: ErrorId | ||
context?: Record<string, string> | ||
error?: string | ||
[key: string]: unknown | ||
} | ||
} | ||
|
||
interface ILocationPosition { | ||
line: number | ||
column: number | ||
} | ||
|
||
interface IStructuredError { | ||
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 |
Oops, something went wrong.