Skip to content

Commit

Permalink
Implement UserError class for errors that don't require a stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
niklashigi committed Jun 26, 2021
1 parent d0202a8 commit 5cb5d9e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { patchXapkBundle, patchApksBundle } from './patch-app-bundle'
import Apktool from './tools/apktool'
import UberApkSigner from './tools/uber-apk-signer'
import Tool from './tools/tool'
import UserError from './utils/user-error'

export type TaskOptions = {
inputPath: string
Expand Down Expand Up @@ -131,7 +132,12 @@ async function main() {
}

function getErrorMessage(error: PatchingError, { tmpDir }: { tmpDir: string }) {
// User errors can be shown without a stack trace
if (error instanceof UserError) return error.message

// Errors from commands can also be shown without a stack trace
if (error.all) return formatCommandError(error.all, { tmpDir })

return error.stack
}

Expand Down
5 changes: 3 additions & 2 deletions src/tasks/check-prerequisites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Listr = require('listr')

import { TaskOptions } from '../cli'
import getJavaVersion from '../utils/get-java-version'
import UserError from '../utils/user-error'
import downloadTools from './download-tools'

const MIN_NODE_VERSION = 14
Expand Down Expand Up @@ -49,14 +50,14 @@ async function ensureZipUlitiesAvailable() {
await execa('unzip', ['-v'])
await execa('zip', ['-v'])
} catch {
throw new Error(
throw new UserError(
'apk-mitm requires the commands "unzip" and "zip" to be installed when patching App Bundles.' +
" Make sure they're both installed and try again!",
)
}
}

class VersionError extends Error {
class VersionError extends UserError {
constructor(tool: string, minVersion: number, currentVersion: number) {
super(
`apk-mitm requires at least ${tool} ${minVersion} to work and you seem to be using ${tool} ${currentVersion}.` +
Expand Down
5 changes: 3 additions & 2 deletions src/utils/get-java-version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import execa = require('execa')
import UserError from './user-error'

/** Returns the major version of the system's default Java installation. */
export default async function getJavaVersion() {
Expand All @@ -7,13 +8,13 @@ export default async function getJavaVersion() {
const majorVersionString = stderr.match(JAVA_VERSION_PATTERN)?.groups?.major
if (!majorVersionString) {
const message = `Could not extract Java major version from "java -version" output!\n${stderr}`
throw new Error(message)
throw new UserError(message)
}

return parseInt(majorVersionString)
} catch (error) {
if (error.code === 'ENOENT')
throw new Error(
throw new UserError(
'No "java" executable could be found!' +
' Make sure that Java is installed and available in your PATH.',
)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/user-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Class for custom errors that can be shown directly to users of the CLI
* without displaying the entire stack trace.
*/
export default class UserError extends Error {
constructor(message: string) {
super(message)
this.name = UserError.name
}
}

0 comments on commit 5cb5d9e

Please sign in to comment.