Skip to content

Commit

Permalink
chore(build): add types to errors in build (#5374)
Browse files Browse the repository at this point in the history
* chore(build): add types to errors in build

* chore(build): s/type/types

* chore(build): fixing some types

* chore(build): just moving some things around

* chore(build): error types JSDoc

* chore(build): further restrict errorProps type

* chore(build): simplify plugin parse types

* chore(build): fix compilation errors
  • Loading branch information
JGAntunes authored Nov 9, 2023
1 parent fd75258 commit d7a432c
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 220 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { getBuildCommandDescription, getPluginOrigin } from '../../log/description.js'
import type {
BuildCommandLocation,
FunctionsBundlingLocation,
CoreStepLocation,
PluginLocation,
APILocation,
} from '../types.js'

// Retrieve an error's location to print in logs.
// Each error type has its own logic (or none if there's no location to print).
Expand All @@ -17,39 +24,39 @@ export const getLocationInfo = function ({ stack, location, locationType }) {
return [locationString, stack].filter(Boolean).join('\n')
}

const getBuildCommandLocation = function ({ buildCommand, buildCommandOrigin }) {
const getBuildCommandLocation = function ({ buildCommand, buildCommandOrigin }: BuildCommandLocation) {
const description = getBuildCommandDescription(buildCommandOrigin)
return `In ${description}:
${buildCommand}`
}

const getFunctionsBundlingLocation = function ({ functionName, functionType }) {
const getFunctionsBundlingLocation = function ({ functionName, functionType }: FunctionsBundlingLocation) {
if (functionType === 'edge') {
return 'While bundling edge function'
}

return `While bundling function "${functionName}"`
}

const getCoreStepLocation = function ({ coreStepName }) {
const getCoreStepLocation = function ({ coreStepName }: CoreStepLocation) {
return `During ${coreStepName}`
}

const getBuildFailLocation = function ({ event, packageName, loadedFrom, origin }) {
const getBuildFailLocation = function ({ event, packageName, loadedFrom, origin }: PluginLocation) {
const eventMessage = getEventMessage(event)
const pluginOrigin = getPluginOrigin(loadedFrom, origin)
return `${eventMessage} "${packageName}" ${pluginOrigin}`
}

const getEventMessage = function (event) {
const getEventMessage = function (event: string) {
if (event === 'load') {
return `While loading`
}

return `In "${event}" event in`
}

const getApiLocation = function ({ endpoint, parameters }) {
const getApiLocation = function ({ endpoint, parameters }: APILocation) {
return `While calling the Netlify API endpoint '${endpoint}' with:\n${JSON.stringify(parameters, null, 2)}`
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { serializeObject } from '../../log/serialize.js'
import { getErrorInfo } from '../info.js'
import { getTypeInfo } from '../type.js'
import type { BuildError, BasicErrorInfo, ErrorInfo, TitleFunction } from '../types.js'
import { getTypeInfo } from '../types.js'

import { getLocationInfo } from './location.js'
import { normalizeError } from './normalize.js'
Expand All @@ -9,14 +10,13 @@ import { getErrorProps } from './properties.js'
import { getStackInfo } from './stack.js'

// Add additional type-specific error information
export const getFullErrorInfo = function ({ error, colors, debug }) {
export const getFullErrorInfo = function ({ error, colors, debug }): BuildError {
const basicErrorInfo = parseErrorInfo(error)
const {
message,
stack,
errorProps,
errorInfo,
errorInfo: { location = {}, plugin = {}, tsConfig },
severity,
title,
stackType,
Expand All @@ -26,11 +26,15 @@ export const getFullErrorInfo = function ({ error, colors, debug }) {
errorMetadata,
} = basicErrorInfo

const { location = {}, plugin = {}, tsConfig } = errorInfo

const titleA = getTitle(title, errorInfo)

const { message: messageA, stack: stackA } = getStackInfo({ message, stack, stackType, rawStack, severity, debug })

const pluginInfo = getPluginInfo(plugin, location)
// FIXME here location should be PluginLocation type, but I'm affraid to mess up the current
// getPluginInfo behaviour by running a type check
const pluginInfo = getPluginInfo(plugin, location as any)
const tsConfigInfo = getTsConfigInfo(tsConfig)
const locationInfo = getLocationInfo({ stack: stackA, location, locationType })
const errorPropsA = getErrorProps({ errorProps, showErrorProps, colors })
Expand All @@ -48,7 +52,7 @@ export const getFullErrorInfo = function ({ error, colors, debug }) {
}

// Serialize the `tsConfig` error information
const getTsConfigInfo = function (tsConfig) {
const getTsConfigInfo = function (tsConfig: any) {
if (tsConfig === undefined) {
return
}
Expand All @@ -57,7 +61,7 @@ const getTsConfigInfo = function (tsConfig) {
}

// Parse error instance into all the basic properties containing information
export const parseErrorInfo = function (error) {
export const parseErrorInfo = function (error: Error): BasicErrorInfo {
const { message, stack, ...errorProps } = normalizeError(error)
const [errorInfo, errorPropsA] = getErrorInfo(errorProps)
const { errorMetadata } = errorInfo
Expand Down Expand Up @@ -92,7 +96,7 @@ export const parseErrorInfo = function (error) {
}

// Retrieve title to print in logs
const getTitle = function (title, errorInfo) {
const getTitle = function (title: TitleFunction | string, errorInfo: ErrorInfo) {
if (typeof title !== 'function') {
return title
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const serializeField = function ({ name, getField, pluginPackageJson, packageNam

const NAME_PADDING = 16

const getPackage = function (pluginPackageJson, { packageName }) {
const getPackage = function (_, { packageName }) {
return packageName
}

Expand All @@ -36,27 +36,32 @@ const getVersion = function ({ version }) {
return version
}

export const getHomepage = function (pluginPackageJson = {}, { loadedFrom } = {}) {
type pkgJSONData = { name?: string; bugs?: { url?: string }; repository?: { url?: string } }

export const getHomepage = function (
pluginPackageJson: pkgJSONData = {},
{ loadedFrom }: { loadedFrom?: string } = {},
) {
return (
getRepository(pluginPackageJson) ||
getNpmLink(pluginPackageJson, { loadedFrom }) ||
getIssuesLink(pluginPackageJson)
)
}

const getRepository = function ({ repository: { url } = {} }) {
const getRepository = function ({ repository: { url } = {} }: pkgJSONData) {
return url
}

const getNpmLink = function ({ name }, { loadedFrom }) {
const getNpmLink = function ({ name }: pkgJSONData, { loadedFrom }: { loadedFrom?: string }) {
if (!name || loadedFrom === 'local') {
return
}

return `https://www.npmjs.com/package/${name}`
}

const getIssuesLink = function ({ bugs: { url } = {} }) {
const getIssuesLink = function ({ bugs: { url } = {} }: pkgJSONData) {
return url
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { THEME } from '../../log/theme.js'
import type { BuildError } from '../types.js'

// Serialize an error object into a title|body string to print in logs
export const serializeLogError = function ({
fullErrorInfo: { title, severity, message, pluginInfo, locationInfo, tsConfigInfo, errorProps },
}: {
fullErrorInfo: BuildError
}) {
const body = getBody({ message, pluginInfo, locationInfo, tsConfigInfo, errorProps, severity })
return { title, body }
Expand Down
202 changes: 0 additions & 202 deletions packages/build/src/error/type.js

This file was deleted.

Loading

0 comments on commit d7a432c

Please sign in to comment.