Skip to content

Commit

Permalink
fix(gatsby-cli): Fine-grained errors with type/category (#37070)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Nov 22, 2022
1 parent 33d1ecc commit 0c2b51c
Show file tree
Hide file tree
Showing 12 changed files with 242 additions and 132 deletions.
Expand Up @@ -83,7 +83,7 @@ describe(`Validate Plugin Options`, () => {
]),
}),
code: `11331`,
type: `PLUGIN`,
type: `API.NODE.VALIDATION`,
}),
}),
}),
Expand Down Expand Up @@ -120,7 +120,7 @@ describe(`Validate Plugin Options`, () => {
]),
}),
code: `11331`,
type: `PLUGIN`,
type: `API.NODE.VALIDATION`,
}),
}),
}),
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-cli/src/reporter/redux/internal-actions.ts
Expand Up @@ -29,8 +29,7 @@ import {
getElapsedTimeMS,
getGlobalStatus,
} from "./utils"
import { IStructuredError } from "../../structured-errors/types"
import { ErrorCategory } from "../../structured-errors/error-map"
import { IStructuredError, ErrorCategory } from "../../structured-errors/types"
import { IRenderPageArgs } from "../types"

const ActivityStatusToLogLevel = {
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-cli/src/reporter/redux/types.ts
@@ -1,6 +1,5 @@
import { Actions, ActivityStatuses, ActivityTypes } from "../constants"
import { IStructuredError } from "../../structured-errors/types"
import { ErrorCategory } from "../../structured-errors/error-map"
import { IStructuredError, ErrorCategory } from "../../structured-errors/types"
import { IRenderPageArgs } from "../types"

export interface IGatsbyCLIState {
Expand Down
9 changes: 6 additions & 3 deletions packages/gatsby-cli/src/reporter/reporter.ts
Expand Up @@ -7,7 +7,10 @@ import * as reduxReporterActions from "./redux/actions"
import { LogLevels, ActivityStatuses } from "./constants"
import { getErrorFormatter } from "./errors"
import constructError from "../structured-errors/construct-error"
import { IErrorMapEntry, ErrorId } from "../structured-errors/error-map"
import {
IErrorMapEntryPublicApi,
ErrorId,
} from "../structured-errors/error-map"
import { prematureEnd } from "./catch-exit-signals"
import { IConstructError, IStructuredError } from "../structured-errors/types"
import { createTimerReporter, ITimerReporter } from "./reporter-timer"
Expand Down Expand Up @@ -53,7 +56,7 @@ class Reporter {
stripIndent = stripIndent
format = chalk

errorMap: Record<ErrorId, IErrorMapEntry> = {}
errorMap: Record<ErrorId, IErrorMapEntryPublicApi> = {}

/**
* Set a custom error map to the reporter. This allows
Expand All @@ -63,7 +66,7 @@ class Reporter {
* https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-cli/src/structured-errors/error-map.ts
*/

setErrorMap = (entry: Record<string, IErrorMapEntry>): void => {
setErrorMap = (entry: Record<string, IErrorMapEntryPublicApi>): void => {
this.errorMap = {
...this.errorMap,
...entry,
Expand Down
15 changes: 12 additions & 3 deletions packages/gatsby-cli/src/structured-errors/construct-error.ts
@@ -1,13 +1,18 @@
import stackTrace from "stack-trace"
import { errorSchema } from "./error-schema"
import { defaultError, ErrorId, errorMap, IErrorMapEntry } from "./error-map"
import {
defaultError,
ErrorId,
errorMap,
IErrorMapEntryPublicApi,
} from "./error-map"
import { sanitizeStructuredStackTrace } from "../reporter/errors"
import { IConstructError, IStructuredError } from "./types"
// Merge partial error details with information from the errorMap
// Validate the constructed object against an error schema
const constructError = (
{ details: { id, ...otherDetails } }: IConstructError,
suppliedErrorMap: Record<ErrorId, IErrorMapEntry>
suppliedErrorMap: Record<ErrorId, IErrorMapEntryPublicApi>
): IStructuredError => {
let errorMapEntry = defaultError

Expand All @@ -16,7 +21,11 @@ const constructError = (
if (errorMap[id]) {
errorMapEntry = errorMap[id]
} else if (suppliedErrorMap[id]) {
errorMapEntry = suppliedErrorMap[id]
errorMapEntry = {
type: `PLUGIN`,
level: `ERROR`,
...suppliedErrorMap[id],
}
}
}

Expand Down

0 comments on commit 0c2b51c

Please sign in to comment.