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

✨ improve(patch): Improve error handling and logging #2494

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions config/eslint.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
],
parser: `@typescript-eslint/parser`,
parserOptions: {
ecmaFeatures: {jsx: true},
ecmaFeatures: { jsx: true },
ecmaVersion: 2021,
sourceType: `module`,
},
Expand All @@ -58,11 +58,22 @@ module.exports = {
`sort-class-members`,
],
rules: {
[`@typescript-eslint/ban-types`]: [
ERROR,
{
types: {
Buffer: {
message: `Use Uint8Array instead.`,
suggest: [`Uint8Array`],
},
},
},
],
[`@typescript-eslint/explicit-member-accessibility`]: ERROR,
[`@typescript-eslint/quotes`]: [
ERROR,
`backtick`,
{avoidEscape: true},
{ avoidEscape: true },
],
[`arrow-body-style`]: OFF,
[`comma-dangle`]: [
Expand Down Expand Up @@ -106,11 +117,18 @@ module.exports = {
],
[`n/no-unsupported-features/es-syntax`]: [
ERROR,
{ignores: [`modules`], version: `>=16.0.0`},
{ ignores: [`modules`], version: `>=16.0.0` },
],
[`n/shebang`]: OFF,
[`no-console`]: ERROR,
[`no-extra-semi`]: OFF,
[`no-restricted-globals`]: [
ERROR,
{
message: `Use Uint8Array instead.`,
name: `Buffer`,
},
],
[`perfectionist/sort-classes`]: OFF,
[`perfectionist/sort-imports`]: [
ERROR,
Expand Down Expand Up @@ -162,6 +180,6 @@ module.exports = {
`.mjs`,
],
},
react: {version: `detect`},
react: { version: `detect` },
},
}
8 changes: 4 additions & 4 deletions sources/@roots/bud-build/src/config/infrastructureLogging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export const infrastructureLogging: Factory<
console: bud.hooks.filter(`build.infrastructureLogging.console`, {
...console,
error: (...args: any[]) => {
logger.scope(bud.label, `webpack`).error(...args)
logger.scope(bud.label).error(...args)
},
info: (...args: any[]) => {
logger.scope(bud.label, `webpack`).info(...args)
logger.scope(bud.label).info(...args)
},
log: (...args: any[]) => {
logger.scope(bud.label, `webpack`).log(...args)
logger.scope(bud.label).log(...args)
},
warn: (...args: any[]) => {
logger.scope(bud.label, `webpack`).info(...args)
logger.scope(bud.label).info(...args)
},
}),
level: bud.hooks.filter(`build.infrastructureLogging.level`, `log`),
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-build/src/rules/svg.inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const inlineSvg: Factory = async ({filter, makeRule, path}) =>
.setGenerator({dataUrl})
.setType(`asset/inline`)

const dataUrl = (data: Buffer) => dataUri(data.toString())
const dataUrl = (data: Uint8Array) => dataUri(data.toString())

export {dataUrl, inlineSvg as default}
54 changes: 33 additions & 21 deletions sources/@roots/bud-build/src/service/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {Records} from '@roots/bud-build/config'
import type {Bud, Build as BudBuild} from '@roots/bud-framework'
import type {Items, Loaders, Rules} from '@roots/bud-framework'
import type {Configuration} from '@roots/bud-framework/config'
Expand Down Expand Up @@ -92,28 +91,41 @@ class Build extends Service implements BudBuild {
this.logger.log(`bud.build.make called`)
await this.app.hooks.fire(`build.before`, this.app)

await import(`@roots/bud-build/config`).then(
async (records: Records) =>
await Promise.all(
Object.entries(records).map(async ([prop, factory]) => {
try {
const value = await factory(this.app)
if (isUndefined(value)) return

this.config[prop] = value
this.logger.log(`built`, prop)
} catch (error) {
throw error
}
}),
),
)

this.logger.log(`configuration successfully built`)
await import(`@roots/bud-build/config`)
.then(
async records =>
await Promise.all(
Object.entries(records).map(async ([prop, factory]) => {
const value = await factory(this.app).catch(this.catch)
if (isUndefined(value)) {
this.logger.success(`omitting:`, prop, `(undefined)`)
return
}

Object.defineProperty(this.config, prop, {
configurable: true,
enumerable: true,
value,
writable: true,
})

this.logger
.success(`defined:`, prop, `(${typeof this.config[prop]})`)
.info(prop, `info:`, this.config[prop])
}),
),
)
.catch(this.catch)

this.logger.success(`configuration built`)
this.logger.info(this.config)
await this.app.hooks.fire(`build.after`, this.app)

return this.config
await this.app.hooks.fire(`build.after`, this.app).catch(this.catch)

return Object.entries(this.config).reduce((a, [k, v]) => {
if (isUndefined(v)) return a
return {...a, [k]: v}
}, {})
}

/**
Expand Down
104 changes: 56 additions & 48 deletions sources/@roots/bud-dashboard/src/components/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,78 @@ import {BudError} from '@roots/bud-support/errors'
import figures from '@roots/bud-support/figures'
import {Box, type ReactNode, Static, Text} from '@roots/bud-support/ink'

const basePath =
global.process.env.PROJECT_CWD ??
global.process.env.INIT_CWD ??
global.process.cwd()

export const Error = ({error}: {error: unknown}): ReactNode => {
let normalError: BudError
type RawError = BudError | Error | string | undefined

const cleanErrorObject = (error: RawError): BudError => {
if (!error) {
error = BudError.normalize(`Unknown error`)
error = new BudError(`Unknown error`)
}

if (typeof error === `string`) {
error = new BudError(error)
}
normalError =
error instanceof BudError ? error : BudError.normalize(error)

return error instanceof BudError ? error : BudError.normalize(error)
}

export const Error = ({error: input}: {error: RawError}): ReactNode => {
const error = cleanErrorObject(input)

return (
<Static items={[0]}>
{(_, key) => (
<Box flexDirection="column" key={key} paddingTop={1}>
<Text backgroundColor="red" color="white">
{` ${normalError.name} `}
</Text>

<Box flexDirection="row" gap={1} marginTop={1}>
<Text color="red">{figures.cross}</Text>
<Text>{normalError.message}</Text>
</Box>

{normalError.details &&
!normalError.details.startsWith(`resolve`) && (
<Box marginTop={1}>
<Text>
<Text color="blue">
{figures.ellipsis}
{` `}Details{` `}
</Text>
{error.name && (
<Box flexDirection="row" gap={1}>
<Text color="red">{figures.cross}</Text>
<Text backgroundColor="red" color="white">
{error.name}
</Text>
</Box>
)}

<Text>{normalError.details.replace(basePath, `.`)}</Text>
{error.message && (
<Box flexDirection="row" gap={1} marginTop={1}>
<Text>{error.message}</Text>
</Box>
)}

{error.details && !error.details.startsWith(`resolve`) && (
<Box marginTop={1}>
<Text>
<Text color="blue">
{figures.ellipsis}
{` `}Details{` `}
</Text>
</Box>
)}

{normalError.thrownBy && (
<Text>{error.details}</Text>
</Text>
</Box>
)}

{error.thrownBy && (
<Box flexDirection="row" gap={1} marginTop={1}>
<Text color="blue">
{figures.ellipsis}
{` `}Thrown by{` `}
</Text>
<Text>{normalError.thrownBy}</Text>
<Text>{error.thrownBy}</Text>
</Box>
)}

{normalError.docs && (
{error.docs && (
<Box marginTop={1}>
<Text>
<Text color="blue">
{figures.arrowRight}
{` `}Documentation{` `}
</Text>
<Text>{normalError.docs.href}</Text>
<Text>{error.docs.href}</Text>
</Text>
</Box>
)}

{normalError.issues && (
{error.issues && (
<Box marginTop={1}>
<Text>
<Text color="blue">
Expand All @@ -75,24 +83,24 @@ export const Error = ({error}: {error: unknown}): ReactNode => {
Issues
</Text>
{` `}
<Text>{normalError.issues.href}</Text>
<Text>{error.issues.href}</Text>
</Text>
</Box>
)}

{normalError.file && (
{error.file && (
<Box marginTop={1}>
<Text color="blue">
{figures.info}
{` `}See file{` `}
</Text>
<Text>{normalError.file.path}</Text>
<Text>{error.file.path}</Text>
</Box>
)}

{normalError.origin &&
!(normalError.origin instanceof BudError) &&
normalError.stack && (
{error.origin &&
!(error.origin instanceof BudError) &&
error.stack && (
<Box flexDirection="column" marginTop={1}>
<Text color="blue">
{figures.home}
Expand All @@ -108,14 +116,14 @@ export const Error = ({error}: {error: unknown}): ReactNode => {
borderTop={false}
paddingLeft={1}
>
<Text>{normalError.stack}</Text>
<Text>{error.stack}</Text>
</Box>
</Box>
)}

{normalError.origin &&
normalError.origin instanceof BudError &&
normalError.stack && (
{error.origin &&
error.origin instanceof BudError &&
error.stack && (
<Box flexDirection="column" marginTop={1}>
<Text color="blue">
{figures.home}
Expand All @@ -133,11 +141,11 @@ export const Error = ({error}: {error: unknown}): ReactNode => {
paddingLeft={1}
>
<Text>
{normalError.origin.message}
{error.origin.message}
{`\n`}
</Text>
{normalError.origin.stack && (
<Text>{normalError.origin.stack}</Text>
{error.origin.stack && (
<Text dimColor>{error.origin.stack}</Text>
)}
</Box>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-emotion/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
*/
@label(`@roots/bud-emotion`)
@dependsOnOptional([`@roots/bud-babel`, `@roots/bud-swc`])
export class BudEmotion extends Extension<{}, null> {
export class BudEmotion extends Extension<NonNullable<unknown>, null> {
/**
* {@link Extension.boot}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type {Bud} from '@roots/bud-framework'

import {Extension} from '@roots/bud-framework/extension'
import {
label,
plugin,
production,
} from '@roots/bud-framework/extension/decorators'
import {label, plugin} from '@roots/bud-framework/extension/decorators'

import FixStyleOnlyEntrypoints from './plugin.js'

Expand All @@ -12,5 +10,27 @@ import FixStyleOnlyEntrypoints from './plugin.js'
*/
@label(`@roots/bud-extensions/fix-style-only-entrypoints`)
@plugin(FixStyleOnlyEntrypoints)
@production
export default class BudFixStyleOnlyEntrypoints extends Extension {}
export default class BudFixStyleOnlyEntrypoints extends Extension {
/**
* When
*/
public override when(bud: Bud): boolean {
if (this.enabled === true) return true

if (bud.isDevelopment) return false

const entrypoints = bud.hooks.filter(`build.entry`, undefined)

if (!entrypoints) return false

if (
!Object.values(entrypoints).every(value =>
value.import.every(entry => entry.endsWith(`.css`)),
)
) {
return false
}

return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
@label(`@roots/bud-extensions/webpack-hot-module-replacement-plugin`)
@plugin(Webpack.HotModuleReplacementPlugin)
export default class BudHMR extends Extension<
{},
NonNullable<unknown>,
HotModuleReplacementPlugin
> {
/**
Expand Down
Loading
Loading