Skip to content

Commit

Permalink
renumber errors; extract err-handler for brewkit
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Mar 14, 2023
1 parent 2d70743 commit 8488a58
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
14 changes: 9 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { usePrefix, useExec, useVirtualEnv, useVersion, useSync } from "hooks"
import err_handler from "./app.err-handler.ts"
import { usePrefix, useExec, useVirtualEnv, useVersion, useSync, useErrorHandler } from "hooks"
import * as logger from "hooks/useLogger.ts"
import useFlags, { Args, useArgs } from "hooks/useFlags.ts"
import dump from "./app.dump.ts"
import help from "./app.help.ts"
import provides from "./app.provides.ts";
import magic from "./app.magic.ts"
import exec, { repl } from "./app.exec.ts"
import { print, pkg as pkgutils, flatmap } from "utils"
import { print, pkg as pkgutils, flatmap, UsageError } from "utils"
import Path from "path"
import { Verbosity } from "./types.ts"
import * as semver from "semver"
Expand Down Expand Up @@ -103,8 +102,13 @@ try {
await print(magic(new Path(Deno.execPath()), args.mode[1]))
}
} catch (err) {
const code = await err_handler(err)
Deno.exit(code)
if (err instanceof UsageError) {
if (!useFlags().silent) await help()
Deno.exit(64)
} else {
const code = await useErrorHandler(err)
Deno.exit(code)
}
}

function announce() {
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import usePackageYAML, { usePackageYAMLFrontMatter } from "./usePackageYAML.ts"
import useSync from "./useSync.ts"
import useVersion from "./useVersion.ts"
import useMoustaches from "./useMoustaches.ts"
import useErrorHandler from "./useErrorHandler.ts"

// but we can sort these alphabetically
export {
useCache,
useCellar,
useDownload,
useErrorHandler,
useExec,
useFlags,
useGitHubAPI,
Expand Down
19 changes: 8 additions & 11 deletions src/app.err-handler.ts → src/hooks/useErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as logger from "./hooks/useLogger.ts"
import { usePantry, useFlags, usePrefix } from "hooks"
import { chuzzle, TeaError, undent, UsageError } from "utils"
import help from "./app.help.ts"
import * as logger from "./useLogger.ts"
import { usePantry, useFlags, usePrefix, useInventory } from "hooks"
import { chuzzle, TeaError, undent } from "utils"
import Path from "path"
import useInventory from "./hooks/useInventory.ts"

async function suggestions(err: TeaError) {
switch (err.id) {
Expand All @@ -25,10 +23,7 @@ async function suggestions(err: TeaError) {
export default async function(err: Error) {
const { silent, debug } = useFlags()

if (err instanceof UsageError) {
if (!silent) await help()
return 1
} else if (err instanceof TeaError) {
if (err instanceof TeaError) {
if (!silent) {
const suggestion = await suggestions(err).swallow()
console.error(`${logger.red('error')}: ${err.title()} (${logger.gray(err.code())})`)
Expand All @@ -42,7 +37,7 @@ export default async function(err: Error) {
}
const code = chuzzle(parseInt(err.code().match(/\d+$/)?.[0] ?? '1')) ?? 1
return code
} else {
} else if (!silent) {
const { stack, message } = err ?? {}

const title = encodeURIComponent(`panic:${message ?? "null"}`).replaceAll('%20', '+')
Expand All @@ -61,6 +56,8 @@ export default async function(err: Error) {

// this way: deno will show the backtrace
if (err instanceof Error == false) throw err
return 128
} else {
return 1
}
}
Expand All @@ -72,7 +69,7 @@ function msg(err: TeaError): string {
const { ctx } = err

switch (err.code()) {
case 'spilt-tea-102':
case 'spilt-tea-009':
if (ctx.filename instanceof Path && !ctx.filename.in(usePrefix())) {
// this yaml is being worked on by the user
msg = `${ctx.filename.prettyLocalString()}: ${ctx.cause.message}`
Expand Down
26 changes: 14 additions & 12 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ export default class TeaError extends Error {
ctx: PlainObject

code() {
// starting at 3 ∵ https://tldp.org/LDP/abs/html/exitcodes.html
// https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/refs/heads/tools_r20/sysroot/usr/include/sysexits.h
switch (this.id) {
case 'not-found: tea -X: arg0': return 'spilt-tea-001'
case 'not-found: exe/md: default target': return 'spilt-tea-002'
case 'not-found: exe/md: region': return 'spilt-tea-003'
case 'not-found: pkg.version': return 'spilt-tea-004'
case 'not-found: pantry: package.yml': return 'spilt-tea-005'
case 'not-found: dev-env': return 'spilt-tea-006'
case 'not-found: pantry': return 'spilt-tea-007'
case 'not-found: arg': return 'spilt-tea-008'
case 'parser: pantry: package.yml': return 'spilt-tea-102'
case '#helpwanted': return 'spilt-tea-411'
case 'http': return 'spilt-tea-500'
case 'confused: interpreter': return 'spilt-tea-103'
case 'not-found: tea -X: arg0': return 'spilt-tea-003'
case 'not-found: exe/md: default target': return 'spilt-tea-004'
case 'not-found: exe/md: region': return 'spilt-tea-005'
case 'not-found: pkg.version': return 'spilt-tea-006'
case 'not-found: pantry: package.yml': return 'spilt-tea-007'
case 'not-found: dev-env': return 'spilt-tea-008'
case 'not-found: pantry': return 'spilt-tea-009'
case 'not-found: arg': return 'spilt-tea-010'
case 'parser: pantry: package.yml': return 'spilt-tea-011'
case '#helpwanted': return 'spilt-tea-012'
case 'http': return 'spilt-tea-013'
case 'confused: interpreter': return 'spilt-tea-14'
default: {
const exhaustiveness_check: never = this.id
throw new Error(`unhandled id: ${exhaustiveness_check}`)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/tea.scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ it(suite, "tea script that tea doesn’t know what to do with errors cleanly", a
`
}).chmod(0o500)
const out = await this.run({args: [fixture.string], throws: false})
assertEquals(out, 103)
assertEquals(out, 14)
})

it(suite, "tea shebang but executed via tea interprets shebang", async function() {
Expand Down

0 comments on commit 8488a58

Please sign in to comment.