Skip to content

Commit

Permalink
Port parts of Structured Errors to TS + Add two more errors (#20597)
Browse files Browse the repository at this point in the history
* 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
LekoArts and ascorbic authored Mar 13, 2020
1 parent 3af4b3e commit ca3abed
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 216 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@types/fs-extra": "^8.0.1",
"@types/got": "^9.6.9",
"@types/jest": "^24.0.23",
"@types/joi": "^14.3.4",
"@types/lodash": "^4.14.149",
"@types/node": "^12.12.11",
"@types/webpack": "^4.41.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const tracer = require(`opentracing`).globalTracer()

const { getErrorFormatter } = require(`./errors`)
const { getStore } = require(`./redux`)
const constructError = require(`../structured-errors/construct-error`)
import constructError from "../structured-errors/construct-error"

const errorFormatter = getErrorFormatter()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const constructError = require(`../construct-error`)
import constructError from "../construct-error"

let log
let processExit
beforeEach(() => {
log = jest.spyOn(console, `log`).mockImplementation(() => {})
processExit = jest.spyOn(process, `exit`).mockImplementation(() => {})
processExit = ((jest.spyOn(
process,
`exit`
) as unknown) as jest.Mock).mockImplementation(() => {})

log.mockReset()
processExit.mockReset()
})

afterAll(() => {
console.log.mockClear()
process.exit.mockClear()
;(console.log as jest.Mock).mockClear()
;((process.exit as unknown) as jest.Mock).mockClear()
})

test(`it exits on invalid error schema`, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { errorMap, defaultError } = require(`../error-map`)
import { errorMap, defaultError } from "../error-map"

test(`it defaults to generic error`, () => {
expect(defaultError).toEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const schema = require(`../error-schema`)
import schema from "../error-schema"

test(`throws invalid on an invalid error`, () => {
expect(schema.validate({ lol: `true` })).rejects.toBeDefined()
Expand Down
39 changes: 0 additions & 39 deletions packages/gatsby-cli/src/structured-errors/construct-error.js

This file was deleted.

75 changes: 75 additions & 0 deletions packages/gatsby-cli/src/structured-errors/construct-error.ts
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
Loading

0 comments on commit ca3abed

Please sign in to comment.