Skip to content

Commit

Permalink
🔨 improve(patch): improve error output
Browse files Browse the repository at this point in the history
  • Loading branch information
kellymears committed Jun 25, 2024
1 parent 1ee7d01 commit 5792fbc
Show file tree
Hide file tree
Showing 45 changed files with 439 additions and 347 deletions.
9 changes: 5 additions & 4 deletions examples/webpack-plugin/WebpackPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Just a normal Webpack plugin
*/
export class WebpackPlugin {
constructor(public log: (...args: any[]) => void) {
}
class WebpackPlugin {
constructor(public log: (...args: any[]) => void) {}

apply(compiler: any) {
apply() {
this.log?.(this.constructor.name, 'applied!')
}
}

export {WebpackPlugin, WebpackPlugin as default}
2 changes: 1 addition & 1 deletion examples/webpack-plugin/bud.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {bud} from '@roots/bud'

const {WebpackPlugin} = require('./WebpackPlugin.js')
import WebpackPlugin from './WebpackPlugin'

/**
* This is an example of how to use a Webpack plugin
Expand Down
14 changes: 11 additions & 3 deletions sources/@repo/markdown-kit/readme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const logger = new Logger({logLevel: `info`})
* Returns props for a template
*/
const getProps = async (signifier: string) => {
const {default: projectConfig} = await import(path(`config`, `monorepo.config.cjs`))
const {default: projectConfig} = await import(
path(`config`, `monorepo.config.cjs`)
)
const json = await Json.read(path(`sources`, signifier, `package.json`))
return {...json, projectConfig}
}
Expand Down Expand Up @@ -124,11 +126,17 @@ const data = {
}
const body = templates.root(data)
logger.log(`repo readme.md is ${body.length} characters`)

const formatted = await format(templates.root(data), {
parser: `markdown`,
}).catch(error => logger.error(`repo readme.md`, error.message))
}).catch(error => logger.error(`repo readme.md: ${error.message}`))
logger.log(formatted)

if (!formatted) {
throw new Error(`Could not format readme`)
}

await fs
.write(outputPath, formatted)
.catch(error => logger.error(`repo readme.md`, error.message))
.catch(error => logger.error(`repo readme.md: ${error.message}`))
.finally(() => logger.log(`Wrote repo readme.md`))
6 changes: 3 additions & 3 deletions sources/@roots/bud-api/src/methods/alias/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Bud} from '@roots/bud-framework'

import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'

import type {Parameters} from './types.js'

Expand Down Expand Up @@ -30,11 +30,11 @@ export const alias: alias = function (this: Bud, ...input) {
}

if (!isSignifier(input[0]))
throw new InputError(
throw new BudError(
`bud.alias received invalid input. param[0] must be a string.`,
)
if (!isValue(input[1])) {
throw new InputError(
throw new BudError(
`bud.alias received invalid input. param[1] must be a string.`,
)
}
Expand Down
6 changes: 3 additions & 3 deletions sources/@roots/bud-api/src/methods/compilePaths/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {Bud, Rules} from '@roots/bud-framework'

import {InputError} from '@roots/bud-support/errors'
import isArray from '@roots/bud-support/isArray'
import isString from '@roots/bud-support/isString'

Expand All @@ -17,7 +16,7 @@ export const compilePaths: compilePaths = function (sources, rules) {

sourcesArray.forEach(source => {
if (!isString(source) && !(source instanceof RegExp)) {
throw new InputError(
this.catch(
`bud.compilePaths: source must be a string or a regular expression.`,
)
}
Expand All @@ -31,7 +30,7 @@ export const compilePaths: compilePaths = function (sources, rules) {
const match = bud.build.getRule(key)

if (!match) {
throw new InputError(
this.catch(
`bud.compilePaths: \`${key}\` is not a valid rule name.`,
)
}
Expand All @@ -40,6 +39,7 @@ export const compilePaths: compilePaths = function (sources, rules) {
})

matches.map(rule => {
if (!rule) return
bud.api.logger.log(`setting compile paths for ${rule.getTest()}`)
rule.setInclude(sourcesArray)
})
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/bud-api/src/methods/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Bud} from '@roots/bud-framework'
import type {Configuration} from '@roots/bud-framework/config'

import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'
import isFunction from '@roots/bud-support/isFunction'

export type Parameters = [
Expand All @@ -16,7 +16,7 @@ export interface config {

export const config: config = function (this: Bud, input): Bud {
if (!input)
throw new InputError(
throw new BudError(
`config input must pass a callback function that returns a webpack configuration`,
{
docs: new URL(`https://bud.js.org/docs/bud.config`),
Expand Down
12 changes: 12 additions & 0 deletions sources/@roots/bud-api/src/methods/copyDir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {Plugin as CopyPlugin} from '@roots/bud-support/copy-webpack-plugin'

import {isAbsolute} from 'node:path'

import {BudError} from '@roots/bud-support/errors'
import isString from '@roots/bud-support/isString'

type FromToTuple = [string, string]
Expand All @@ -26,6 +27,17 @@ export const copyDir: copyDir = async function copyDir(
const makePatternObjectFromString = fromStringFactory(this, overrides)
const makePatternObjectFromTuple = fromTupleFactory(this, overrides)

if (context && !isString(context)) {
throw BudError.normalize(
`bud.copyDir: Parameter 2 must be a string.`,
{
details: `The second parameter, context, must be a string. Received ${typeof context}.`,
docs: new URL(`https://bud.js.org/reference/bud.copyDir`),
thrownBy: import.meta.url,
},
)
}

if (!context) context = this.path(`@src`)
if (!isAbsolute(context)) context = this.path(context)

Expand Down
6 changes: 3 additions & 3 deletions sources/@roots/bud-api/src/methods/hash/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Bud} from '@roots/bud-framework'

import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'

export type Value =
| ((hash?: boolean) => boolean | string)
Expand All @@ -27,7 +27,7 @@ export const hash: hash = function (this: Bud, value = true) {
value = value(this.context.hash)

if (typeof value !== `boolean` && typeof value !== `string`)
throw new InputError(`bud.hash: invalid input`, {
throw new BudError(`bud.hash: invalid input`, {
details: `callbacks supplied to bud.hash should return a boolean or a string value`,
docs: new URL(`https://bud.js.org/reference/bud.hash`),
thrownBy: `@roots/bud-api/methods/hash`,
Expand All @@ -47,7 +47,7 @@ export const hash: hash = function (this: Bud, value = true) {
return setFormat(this, value)
}

throw new InputError(`bud.hash: invalid input`, {
throw new BudError(`bud.hash: invalid input`, {
details: `bud.hash accepts a boolean, string, or callback function as input.`,
docs: new URL(`https://bud.js.org/reference/bud.hash`),
thrownBy: `@roots/bud-api/methods/hash`,
Expand Down
6 changes: 3 additions & 3 deletions sources/@roots/bud-api/src/methods/minimize/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Bud} from '@roots/bud-framework'
import {ConfigError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'

export type Parameters = [
(`css` | `js` | Array<`css` | `js`> | boolean | Bud)?,
Expand Down Expand Up @@ -65,15 +65,15 @@ export const minimize: minimize = function (this: Bud, value = true) {
return this
}

throw ConfigError.normalize(`Error in bud.minimize`, {
throw BudError.normalize(`Error in bud.minimize`, {
details: `Invalid argument passed to bud.minimize. Value must be a boolean, string, or array of strings.`,
docs: new URL(`https://bud.js.org/reference/bud.minimize`),
thrownBy: `@roots/bud-api/methods/minimize`,
})
}

const throwUndefinedMinimizer = (): never => {
throw ConfigError.normalize(`Error in bud.minimize`, {
throw BudError.normalize(`Error in bud.minimize`, {
details: `Invalid argument passed to bud.minimize. Minimizer does not exist.`,
docs: new URL(`https://bud.js.org/reference/bud.minimize`),
thrownBy: `@roots/bud-api/methods/minimize`,
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/bud-api/src/methods/provide/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Bud} from '@roots/bud-framework'

import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'
import isString from '@roots/bud-support/isString'
import isUndefined from '@roots/bud-support/isUndefined'

Expand Down Expand Up @@ -28,7 +28,7 @@ export const provide: provide = async function (this: Bud, ...params) {
const accessors = !Array.isArray(params[1]) ? [params[1]] : params[1]

if (!valid(value) || !valid(accessors)) {
throw new InputError(
throw new BudError(
`bud.provide: when specifying a key and value using multiple parameters, the key should be a string and the value should be a string or array of strings`,
{
docs: new URL(`https://bud.js.org/reference/bud.provide`),
Expand Down
18 changes: 18 additions & 0 deletions sources/@roots/bud-api/src/methods/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type {Optimization} from '@roots/bud-framework/config'

import {Bud} from '@roots/bud-framework'
import {BudError} from '@roots/bud-support/errors'
import isObject from '@roots/bud-support/isObject'

export type Parameters = [
| ((
Expand All @@ -18,6 +20,22 @@ export const runtime: runtime = async function (
this: Bud,
runtime = `single`,
) {
if (
runtime !== `single` &&
runtime !== `multiple` &&
typeof runtime !== `boolean` &&
!(runtime instanceof Bud) &&
!(isObject(runtime) && `name` in runtime)
) {
throw BudError.normalize(
`bud.runtime: invalid value "${runtime}". Must be a boolean, "single", or "multiple".`,
{
docs: new URL(`https://bud.js.org/reference/bud.runtime`),
thrownBy: import.meta.url,
},
)
}

const value =
runtime instanceof Bud || runtime === true ? `single` : runtime

Expand Down
35 changes: 25 additions & 10 deletions sources/@roots/bud-api/test/compilePaths.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'
import {beforeEach, describe, expect, it, vi} from 'vitest'

import {compilePaths as compilePathsFn} from '../src/methods/compilePaths'
Expand All @@ -8,7 +8,7 @@ describe(`@roots/bud-api/methods/compilePaths`, function () {
let compilePaths: compilePathsFn
let ruleGetTest = vi.fn(rule => `mock_getTest`)
let ruleSetInclude = vi.fn(() => `mock_setInclude`)
let error: InputError
let error: BudError

beforeEach(() => {
class MockBud {
Expand All @@ -31,9 +31,10 @@ describe(`@roots/bud-api/methods/compilePaths`, function () {
js: {},
},
}
public catch = vi.fn()
public hooks = {
action: vi.fn(async (event, cb) => {
return await cb(this).catch((errorInstance: InputError) => {
return await cb(this).catch((errorInstance: BudError) => {
error = errorInstance
})
}),
Expand All @@ -54,23 +55,35 @@ describe(`@roots/bud-api/methods/compilePaths`, function () {
})

it(`should throw when input is undefined`, () => {
// @ts-ignore
expect(() => compilePaths()).toThrow()
/// @ts-ignore
compilePaths(1)
expect(mockBud.catch).toHaveBeenCalledWith(
`bud.compilePaths: source must be a string or a regular expression.`,
)
})

it(`should throw when provided a null value`, () => {
// @ts-ignore
expect(() => compilePaths()).toThrow()
compilePaths(1)
expect(mockBud.catch).toHaveBeenCalledWith(
`bud.compilePaths: source must be a string or a regular expression.`,
)
})

it(`should throw when provided a number`, () => {
// @ts-ignore
expect(() => compilePaths(1)).toThrow()
compilePaths(1)
expect(mockBud.catch).toHaveBeenCalledWith(
`bud.compilePaths: source must be a string or a regular expression.`,
)
})

it(`should throw when provided a non array object`, () => {
// @ts-ignore
expect(() => compilePaths({})).toThrow()
compilePaths({})
expect(mockBud.catch).toHaveBeenCalledWith(
`bud.compilePaths: source must be a string or a regular expression.`,
)
})

describe(`when a string is provided`, async () => {
Expand Down Expand Up @@ -257,8 +270,10 @@ describe(`@roots/bud-api/methods/compilePaths`, function () {
compilePaths([`/foo`], [`baz`])
})

it(`should throw`, async () => {
expect(error).toBeInstanceOf(InputError)
it(`should error`, async () => {
expect(mockBud.catch).toHaveBeenCalledWith(
`bud.compilePaths: \`baz\` is not a valid rule name.`,
)
})
})
})
6 changes: 3 additions & 3 deletions sources/@roots/bud-babel/src/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {Bud} from '@roots/bud-framework'
import {Extension} from '@roots/bud-framework/extension'
import {expose} from '@roots/bud-framework/extension/decorators/expose'
import {label} from '@roots/bud-framework/extension/decorators/label'
import {InputError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'
import isString from '@roots/bud-support/isString'
import isUndefined from '@roots/bud-support/isUndefined'

Expand Down Expand Up @@ -179,7 +179,7 @@ class BabelExtension extends Extension {
}

if (Array.isArray(name)) {
throw new InputError(`Invalid plugin name`)
throw new BudError(`Invalid plugin name`)
}

this.plugins[name] = Array.isArray(plugin) ? plugin : [plugin]
Expand Down Expand Up @@ -223,7 +223,7 @@ class BabelExtension extends Extension {
}

if (Array.isArray(name)) {
throw new InputError(`Invalid preset name`)
throw new BudError(`Invalid preset name`)
}

this.presets[name] = Array.isArray(preset) ? preset : [preset]
Expand Down
4 changes: 2 additions & 2 deletions sources/@roots/bud-build/src/config/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Factory} from '@roots/bud-build/config'

import {ConfigError} from '@roots/bud-support/errors'
import {BudError} from '@roots/bud-support/errors'
import isUndefined from '@roots/bud-support/isUndefined'

export const dependencies: Factory<`dependencies`> = async ({
Expand All @@ -14,7 +14,7 @@ export const dependencies: Factory<`dependencies`> = async ({
const defined = !isUndefined(root.children?.[dependency])

if (!defined) {
throw ConfigError.normalize(
throw BudError.normalize(
`${dependency} is not a registered instance of bud.js.`,
{
details: root.children
Expand Down
2 changes: 1 addition & 1 deletion sources/@roots/bud-build/src/item/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Item extends Registrable implements Build.Item {
}

if (!output.loader) {
logger.error(`error in ${this.ident}`, `no loader registered`)
logger.error(`error in ${this.ident}: no loader registered`)
}

return Object.entries(output).reduce(
Expand Down
Loading

0 comments on commit 5792fbc

Please sign in to comment.