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

Remove cli stacktrace from cli error output #303

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 12 additions & 6 deletions packages/cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ const path = require('path')
const { exit } = require('process')

// remove the `npx cli` prefix
const index = process.argv.findIndex((arg) => arg.includes('@alephium/cli') || arg.includes('cli.js') || arg.includes('cli'))
const index = process.argv.findIndex(
(arg) => arg.includes('@alephium/cli') || arg.includes('cli.js') || arg.includes('cli')
)
if (index === -1) {
console.log('Please run "npx @alephium/cli@latest <command>"')
exit(-1)
Expand All @@ -31,8 +33,12 @@ const argString = process.argv.slice(index + 1).join(' ')
const cliRootPath = path.resolve(__dirname)
const cliInternalPath = path.join(cliRootPath, 'cli_internal.ts')
const command = `npx --yes ts-node --transpile-only ${cliInternalPath} ${argString}`
execSync(command, {
stdio: 'inherit',
cwd: process.cwd(),
env: process.env
})
try {
execSync(command, {
Dismissed Show dismissed Hide dismissed
stdio: 'inherit',
cwd: process.cwd(),
env: process.env
})
} catch (err) {
exit(err.status)
}
29 changes: 19 additions & 10 deletions packages/cli/cli_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
import { generateImagesWithOpenAI, uploadImagesAndMetadataToIPFS } from './scripts/pre-designed-nft'
import { codegen, getConfigFile, isNetworkLive, loadConfig } from './src'

function getConfig(options: any): Configuration {

Check warning on line 29 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (16)

Unexpected any. Specify a different type

Check warning on line 29 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected any. Specify a different type
const configFile = options.config ? (options.config as string) : getConfigFile()
console.log(`Loading alephium config file: ${configFile}`)
const config = loadConfig(configFile)
if (config.enableDebugMode) enableDebugMode()
if (config.enableDebugMode || options.debug) enableDebugMode()
return config
}

Expand All @@ -44,6 +44,11 @@
return networkId as NetworkId
}

function buildErrorOutput(error: Error, isDebug: boolean): string {
const debugMsg = error.stack ?? error.toString()
return isDebug ? debugMsg : error.message
}

const templateTypes = ['base', 'react', 'nextjs']

program
Expand Down Expand Up @@ -73,14 +78,14 @@
.option('-c, --config <config-file>', 'project config file (default: alephium.config.{ts|js})')
.option('-n, --network <network-type>', 'network type')
.option('--skipGenerate', 'skip generate typescript code by contract artifacts')
.option('--debug', 'show detailed debug information such as error stack traces')
.action(async (options) => {
try {
const config = getConfig(options)
const networkId = checkAndGetNetworkId(options.network)
const nodeUrl = config.networks[networkId].nodeUrl

Check warning on line 86 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (16)

Generic Object Injection Sink

Check warning on line 86 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (18)

Generic Object Injection Sink
if (!(await isNetworkLive(nodeUrl))) {
console.log(`${networkId} is not live`)
process.exit(1)
throw new Error(`${networkId} is not live`)
}
web3.setCurrentNodeProvider(nodeUrl)
const fullNodeVersion = (await web3.getCurrentNodeProvider().infos.getInfosVersion()).version
Expand All @@ -91,11 +96,11 @@
if (options.skipGenerate) {
return
}
const artifactDir = config.artifactDir! // there is a default value always

Check warning on line 99 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (16)

Forbidden non-null assertion

Check warning on line 99 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (18)

Forbidden non-null assertion
codegen(artifactDir)
console.log('✅ Codegen completed!')
} catch (error) {
program.error(`Failed to compile, error: ${(error as Error).stack}`)
program.error(`Failed to compile, error: ${buildErrorOutput(error, options.debug)}`)
}
})

Expand Down Expand Up @@ -160,6 +165,7 @@
'-t, --to <number>',
'run scripts to a specific index(inclusive), the number refers to the prefix of the script file'
)
.option('--debug', 'show detailed debug information such as error stack traces')
.action(async (options) => {
try {
const config = getConfig(options)
Expand All @@ -168,7 +174,7 @@
const toIndex = tryGetScriptIndex(options.to)
await deployAndSaveProgress(config, networkId, fromIndex, toIndex)
} catch (error) {
program.error(`Failed to deploy contracts, error: ${(error as Error).stack}`)
program.error(`Failed to deploy contracts, error: ${buildErrorOutput(error, options.debug)}`)
Copy link
Member

Choose a reason for hiding this comment

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

If debug is enabled via the config file, should we also show the debug info? It looks good to keep the two debug flag consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If debug is enabled via the config file, should we also show the debug info? It looks good to keep the two debug flag consistent.

Yeah good point, updated

}
})

Expand All @@ -182,13 +188,14 @@
.option('-d, --dir <directory-of-stored-images>', 'directory where to store the images')
.option('-n, --number <number-of-images>', 'number of images to generate', '1')
.option('-s, --size <size-of-image>', 'size of the image to generate', '512x512')
.option('--debug', 'show detailed debug information such as error stack traces')
.action(async (options, args) => {
try {
const config = getConfig(options)
const networkId = checkAndGetNetworkId(options.network)
const openaiAPIKey = config.networks[networkId].settings.openaiAPIKey

Check warning on line 196 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (16)

Generic Object Injection Sink

Check warning on line 196 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (18)

Generic Object Injection Sink
if (!openaiAPIKey) {
program.error('OpenAI API key not specified')
throw new Error('OpenAI API key not specified')
}
const numberOfImages = Number(options.number)
const imageSize = options.size as CreateImageRequestSizeEnum
Expand All @@ -197,7 +204,7 @@

await generateImagesWithOpenAI(openaiAPIKey, prompt, numberOfImages, imageSize, storedDir)
} catch (error) {
program.error(`Failed to generate images, error: ${(error as Error).stack}`)
program.error(`Failed to generate images, error: ${buildErrorOutput(error, options.debug)}`)
}
})

Expand All @@ -209,6 +216,7 @@
.option('-d, --localDir <directory-of-local-images>', 'directory of local images to be uploaded')
.option('-i, --ipfsDir <ipfs-directory-of-uploaded-images>', 'IPFS directory to upload the images')
.option('-m, --metadataFile <metadata-file>', 'file to store the metadata of the uploaded images')
.option('--debug', 'show detailed debug information such as error stack traces')
ross-weir marked this conversation as resolved.
Show resolved Hide resolved
.action(async (options) => {
try {
const localDir = options.localDir as string
Expand All @@ -216,18 +224,18 @@
const metadataFile = options.metadataFile as string
const config = getConfig(options)
const networkId = checkAndGetNetworkId(options.network)
const settings = config.networks[networkId].settings

Check warning on line 227 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (16)

Generic Object Injection Sink

Check warning on line 227 in packages/cli/cli_internal.ts

View workflow job for this annotation

GitHub Actions / build (18)

Generic Object Injection Sink
const projectId = settings.ipfs.infura.projectId
const projectSecret = settings.ipfs.infura.projectSecret
if (!projectId || !projectSecret) {
program.error('Infura project id or secret not specified')
throw new Error('Infura project id or secret not specified')
}

const result = await uploadImagesAndMetadataToIPFS(localDir, ipfsDir, metadataFile, projectId, projectSecret)
console.log('NFTBaseUri:')
console.log(result)
} catch (error) {
program.error(`Failed to upload images, error: ${(error as Error).stack}`)
program.error(`Failed to upload images, error: ${buildErrorOutput(error, options.debug)}`)
}
})

Expand All @@ -236,6 +244,7 @@
.description('validate nft base uri for pre-designed collection')
.option('-n, --nftBaseUri <nft-base-uri>', 'NFT base uri')
.option('-m, --maxSupply <max-supply-of-the-pre-designed-collection>', 'max supply of the NFT collection')
.option('--debug', 'show detailed debug information such as error stack traces')
.action(async (options) => {
try {
const nftBaseUri = options.nftBaseUri as string
Expand All @@ -244,7 +253,7 @@
console.log('Token Metadataz:')
console.log(result)
} catch (error) {
program.error(`Failed to upload images metadata, error: ${(error as Error).stack} `)
program.error(`Failed to upload images metadata, error: ${buildErrorOutput(error, options.debug)}`)
}
})

Expand Down
Loading