Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(build): add types to errors in build #5374

Merged
merged 9 commits into from
Nov 9, 2023
Merged

Conversation

JGAntunes
Copy link
Contributor

@JGAntunes JGAntunes commented Nov 8, 2023

🎉 Thanks for submitting a pull request! 🎉

Summary

This is related with COM-129 - https://linear.app/netlify/issue/COM-129/expose-location-information-for-errors-in-netlifybuild - where we want to capture particular error data in our traces. We continuously struggle to understand our error types and make sense of what properties we have in place when going through the many mutations they go through. This is a first stab at adding types to our errors in @netlify/build. There's almost certainly things we can improve but the idea here is to start somewhere.


For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures
    we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures
    your code follows our style guide and passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

@JGAntunes JGAntunes self-assigned this Nov 9, 2023
Comment on lines +52 to +84
export type BuildCommandLocation = {
buildCommand: string
buildCommandOrigin: string
}

export type FunctionsBundlingLocation = {
functionName: string
functionType: string
}

export type CoreStepLocation = {
coreStepName: string
}

export type PluginLocation = {
event: string
packageName: string
loadedFrom: string
origin: string
input?: string
}

export type APILocation = {
endpoint: string
parameters?: any
}

export type ErrorLocation =
| BuildCommandLocation
| FunctionsBundlingLocation
| CoreStepLocation
| PluginLocation
| APILocation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came from analysing:

  • // 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).
    export const getLocationInfo = function ({ stack, location, locationType }) {
    // No location to print
    if (locationType === undefined && stack === undefined) {
    return
    }
    // The location is only the stack trace
    if (locationType === undefined) {
    return stack
    }
    const locationString = LOCATIONS[locationType](location)
    return [locationString, stack].filter(Boolean).join('\n')
    }
    const getBuildCommandLocation = function ({ buildCommand, buildCommandOrigin }) {
    const description = getBuildCommandDescription(buildCommandOrigin)
    return `In ${description}:
    ${buildCommand}`
    }
    const getFunctionsBundlingLocation = function ({ functionName, functionType }) {
    if (functionType === 'edge') {
    return 'While bundling edge function'
    }
    return `While bundling function "${functionName}"`
    }
    const getCoreStepLocation = function ({ coreStepName }) {
    return `During ${coreStepName}`
    }
    const getBuildFailLocation = function ({ event, packageName, loadedFrom, origin }) {
    const eventMessage = getEventMessage(event)
    const pluginOrigin = getPluginOrigin(loadedFrom, origin)
    return `${eventMessage} "${packageName}" ${pluginOrigin}`
    }
    const getEventMessage = function (event) {
    if (event === 'load') {
    return `While loading`
    }
    return `In "${event}" event in`
    }
    const getApiLocation = function ({ endpoint, parameters }) {
    return `While calling the Netlify API endpoint '${endpoint}' with:\n${JSON.stringify(parameters, null, 2)}`
    }
    const LOCATIONS = {
    buildCommand: getBuildCommandLocation,
    functionsBundling: getFunctionsBundlingLocation,
    coreStep: getCoreStepLocation,
    buildFail: getBuildFailLocation,
    api: getApiLocation,


type ErrorTypes = keyof typeof ErrorTypeMap

const TYPES: { [T in ErrorTypes]: ErrorType } = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing there's probably better ways to deal with this in the future if we break down this large object and create separate types for these 🤔

@JGAntunes JGAntunes marked this pull request as ready for review November 9, 2023 12:40
@JGAntunes JGAntunes requested review from a team as code owners November 9, 2023 12:40
@JGAntunes JGAntunes requested review from kitop and 4xposed November 9, 2023 13:15
Copy link
Contributor

@kitop kitop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but would love the eyes of somebody more familiar with the TS type system.

packages/build/src/error/parse/plugin.ts Show resolved Hide resolved
eduardoboucas
eduardoboucas previously approved these changes Nov 9, 2023
Copy link
Member

@eduardoboucas eduardoboucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

packages/build/src/error/types.ts Show resolved Hide resolved
packages/build/src/error/types.ts Outdated Show resolved Hide resolved
packages/build/src/error/parse/plugin.ts Outdated Show resolved Hide resolved
eduardoboucas
eduardoboucas previously approved these changes Nov 9, 2023
@kodiakhq kodiakhq bot merged commit d7a432c into main Nov 9, 2023
35 checks passed
@kodiakhq kodiakhq bot deleted the chore/build-error-types branch November 9, 2023 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants