Skip to content

Commit

Permalink
add types to api-runner-error-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieudutour committed Mar 13, 2020
1 parent 2c08909 commit 4d109ab
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 21 deletions.
52 changes: 52 additions & 0 deletions packages/gatsby/src/utils/__tests__/api-runner-error-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import errorParser from "../api-runner-error-parser"

describe(`error matching`, () => {
test(`it matches "is not defined" errors`, () => {
const match = errorParser({ err: new Error(`foo is not defined`) })

expect(match).toEqual({
id: `11330`,
context: { sourceMessage: `foo is not defined`, arg: `foo` },
})
})

test(`it has a default when no match are found`, () => {
const match = errorParser({ err: new Error(`unknown error`) })

expect(match).toEqual({
id: `11321`,
context: { sourceMessage: `unknown error` },
error: new Error(`unknown error`),
})
})
})

describe(`unkown error parser`, () => {
test(`it handles Errors`, () => {
const match = errorParser({ err: new Error(`error`) })

expect(match.context.sourceMessage).toEqual(`error`)
expect(match.error).toBeTruthy()
})

test(`it handles Strings`, () => {
const match = errorParser({ err: `error` })

expect(match.context.sourceMessage).toEqual(`error`)
expect(match.error).toBeUndefined()
})

test(`it handles arrays of Error`, () => {
const match = errorParser({ err: [new Error(`error`)] })

expect(match.context.sourceMessage).toEqual(`error`)
expect(match.error).toBeTruthy()
})

test(`it handles anything else by returning an empty string`, () => {
const match = errorParser({ err: new Map() })

expect(match.context.sourceMessage).toEqual(``)
expect(match.error).toBeUndefined()
})
})
48 changes: 27 additions & 21 deletions packages/gatsby/src/utils/api-runner-error-parser.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
import { IMatch } from "../types"

const errorParser = ({ err }): IMatch => {
const errorParser = ({ err }: { err: unknown }): IMatch => {
const handlers = [
{
regex: /(.+) is not defined/m,
cb: (match): IMatch => {
cb: (match: RegExpMatchArray): IMatch => {
return {
id: `11330`,
context: { sourceMessage: match[0], arg: match[1] },
}
},
},
// Match anything with a generic catch-all error handler
{
regex: /[\s\S]*/gm,
cb: (match): IMatch => {
return {
id: `11321`,
context: { sourceMessage: err instanceof Error ? match[0] : err },
error: err instanceof Error ? err : undefined,
}
},
},
]

let structured
let structured: IMatch | undefined
let errorMessage: string | undefined

// try to handle as many type of err as possible.
// the err might come from a plugin so we don't
// know what we are getting
if (Array.isArray(err)) {
err = err[0]
}
if (err instanceof Error) {
errorMessage = err.message
}
if (typeof err === 'string') {
errorMessage = err
}

for (const { regex, cb } of handlers) {
if (Array.isArray(err)) {
err = err[0]
}
if (err.message) {
err = err.message
}
const matched = err?.match(regex)
const matched = errorMessage?.match(regex)
if (matched) {
structured = {
...cb(matched),
Expand All @@ -42,6 +39,15 @@ const errorParser = ({ err }): IMatch => {
}
}

// if we haven't found any known error
if (!structured) {
return {
id: `11321`,
context: { sourceMessage: errorMessage || '' },
error: err instanceof Error ? err : undefined,
}
}

return structured
}

Expand Down

0 comments on commit 4d109ab

Please sign in to comment.