Skip to content

Commit

Permalink
add stack traces when AZLE_VERBOSE=true
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Oct 29, 2024
1 parent ddae198 commit 94600b1
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 44 deletions.
87 changes: 58 additions & 29 deletions src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,41 @@ import { getCanisterConfig } from './stable/utils/get_canister_config';
import { AZLE_PACKAGE_PATH } from './stable/utils/global_paths';
import { CanisterConfig, Command } from './stable/utils/types';

process.on('uncaughtException', (error: Error) => {
const prefix = 'Azle BuildError';

const message =
process.env.AZLE_VERBOSE === 'true'
? `${prefix}: ${error.stack}`
: `${prefix}: ${error}`;

console.error(message);

process.exit(1);
});

process.on('unhandledRejection', (reason: any) => {
const prefix = 'Azle BuildError';

const message =
process.env.AZLE_VERBOSE === 'true' && reason instanceof Error
? `${prefix}: ${reason.stack}`
: `${prefix}: ${reason}`;

console.error(message);

process.exit(1);
});

build();

async function build(): Promise<void> {
const command = process.argv[2] as Command | undefined;

if (command === undefined) {
throw `No command found when running azle. Running azle should start like this: azle [commandName]`;
throw new Error(
`No command found when running azle. Running azle should start like this: azle [commandName]`
);
}

const ioType = process.env.AZLE_VERBOSE === 'true' ? 'inherit' : 'pipe';
Expand Down Expand Up @@ -81,7 +109,9 @@ async function build(): Promise<void> {
return;
}

throw `Invalid command found when running azle. Running azle ${command} is not valid`;
throw new Error(
`Invalid command found when running azle. Running azle ${command} is not valid`
);
}

function handleInstallDfxExtensionCommand(ioType: IOType): void {
Expand All @@ -96,7 +126,9 @@ async function handleUploadAssetsCommand(): Promise<void> {

if (experimental === false) {
if (canisterConfig.custom?.assets !== undefined) {
throw experimentalMessageDfxJson('the upload-assets command');
throw new Error(
experimentalMessageDfxJson('the upload-assets command')
);
}
} else {
await runUploadAssetsCommand();
Expand Down Expand Up @@ -156,7 +188,7 @@ async function handleNewCommand(): Promise<void> {

if (experimental === false) {
if (httpServer === true) {
throw experimentalMessageCli('the --http-server option');
throw new Error(experimentalMessageCli('the --http-server option'));
}

const templatePath = join(AZLE_PACKAGE_PATH, 'examples', 'hello_world');
Expand All @@ -176,51 +208,48 @@ function checkForExperimentalDfxJsonFields(
canisterConfig: CanisterConfig
): void {
if (canisterConfig.custom?.assets !== undefined) {
throw experimentalMessageDfxJson(
'the assets field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson('the assets field in your dfx.json file')
);
}

if (canisterConfig.custom?.build_assets !== undefined) {
throw experimentalMessageDfxJson(
'the build_assets field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson(
'the build_assets field in your dfx.json file'
)
);
}

if (canisterConfig.custom?.candid_gen === 'http') {
throw experimentalMessageDfxJson(
'the "candid_gen": "http" field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson(
'the "candid_gen": "http" field in your dfx.json file'
)
);
}

if (canisterConfig.custom?.esm_aliases !== undefined) {
throw experimentalMessageDfxJson(
'the esm_aliases field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson(
'the esm_aliases field in your dfx.json file'
)
);
}

if (canisterConfig.custom?.esm_externals !== undefined) {
throw experimentalMessageDfxJson(
'the esm_externals field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson(
'the esm_externals field in your dfx.json file'
)
);
}

if (canisterConfig.custom?.openValueSharing !== undefined) {
throw experimentalMessageDfxJson(
'the openValueSharing field in your dfx.json file'
throw new Error(
experimentalMessageDfxJson(
'the openValueSharing field in your dfx.json file'
)
);
}
}

// TODO do we want stack traces?
// TODO maybe when verbose is set only?
// TODO stack traces with verbose sounds pretty good
process.on('uncaughtException', (error: Error) => {
console.error(`Azle BuildError: ${error}`);
process.exit(1);
});

process.on('unhandledRejection', (reason: any) => {
console.error(`Azle BuildError: ${reason}`);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getCandidAndMethodMeta(
handleHttp();
}

throw `dfx.json: "candid_gen": "${candidGen}" is not supported`;
throw new Error(`dfx.json: "candid_gen": "${candidGen}" is not supported`);
}

async function handleAutomaticAndCustom(
Expand All @@ -54,5 +54,7 @@ async function handleAutomaticAndCustom(
}

function handleHttp(): never {
throw `dfx.json: "candid_gen": "http" is only available in experimental mode`;
throw new Error(
`dfx.json: "candid_gen": "http" is only available in experimental mode`
);
}
10 changes: 7 additions & 3 deletions src/build/stable/commands/compile/get_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ export function getContext(
const main = canisterConfig?.main;

if (main === undefined) {
throw `Your dfx.json canister configuration object must have a "main" property pointing to your canister's entrypoint .ts or .js file`;
throw new Error(
`Your dfx.json canister configuration object must have a "main" property pointing to your canister's entrypoint .ts or .js file`
);
}

const canisterPath = join('.azle', canisterName);

const candidPath = process.env.CANISTER_CANDID_PATH;

if (candidPath === undefined) {
throw `CANISTER_CANDID_PATH is not defined`;
throw new Error(`CANISTER_CANDID_PATH is not defined`);
}

const wasmBinaryPath = join(canisterPath, `${canisterName}.wasm`);
Expand Down Expand Up @@ -51,7 +53,9 @@ function getEnvVars(canisterConfig: CanisterConfig): EnvVars {
const envVarValue = process.env[envVarName];

if (envVarValue === undefined) {
throw `Environment variable ${envVarName} must be undefined`;
throw new Error(
`Environment variable ${envVarName} must be undefined`
);
}

return [envVarName, envVarValue];
Expand Down
4 changes: 3 additions & 1 deletion src/build/stable/commands/compile/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export async function bundle(buildOptions: BuildOptions): Promise<string> {
const buildResult = await build(buildOptions);

if (buildResult.outputFiles === undefined) {
throw `Build process failed to produce JavaScript output files`;
throw new Error(
`Build process failed to produce JavaScript output files`
);
}

const bundleArray = buildResult.outputFiles[0].contents;
Expand Down
2 changes: 1 addition & 1 deletion src/build/stable/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function runCommand(
templatePath: string
): Promise<void> {
if (process.argv[3] === undefined) {
throw `You must provide a name for your Azle project`;
throw new Error(`You must provide a name for your Azle project`);
}

const projectName = process.argv[3];
Expand Down
2 changes: 1 addition & 1 deletion src/build/stable/utils/exec_sync_pretty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function execSyncPretty(
return execSync(command, { stdio });
} catch (error) {
if (hint !== undefined) {
throw `${hint}: ${error}`;
throw new Error(`${hint}: ${error}`);
} else {
throw error;
}
Expand Down
12 changes: 9 additions & 3 deletions src/build/stable/utils/get_canister_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export async function getCanisterConfig(
const dfxJsonExample = getDfxJsonExample(canisterName);

if (!existsSync(`dfx.json`)) {
throw `Create a dfx.json file in the current directory with the following format:\n\n${dfxJsonExample}`;
throw new Error(
`Create a dfx.json file in the current directory with the following format:\n\n${dfxJsonExample}`
);
}

const dfxJson: DfxJson = JSON.parse(
Expand All @@ -18,11 +20,15 @@ export async function getCanisterConfig(
const canisterConfig = dfxJson.canisters?.[canisterName];

if (canisterConfig === undefined) {
throw `Make sure your dfx.json contains a property for "${canisterName}". For example:\n\n${dfxJsonExample}`;
throw new Error(
`Make sure your dfx.json contains a property for "${canisterName}". For example:\n\n${dfxJsonExample}`
);
}

if (canisterConfig.main === undefined) {
throw `Make sure your dfx.json contains a property for "main". For example:\n\n${dfxJsonExample}`;
throw new Error(
`Make sure your dfx.json contains a property for "main". For example:\n\n${dfxJsonExample}`
);
}

return canisterConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/dfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export function getLocalDfxVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Return the version number
} else {
throw 'Could not parse the dfx version';
throw new Error(`Could not parse the dfx version`);
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export function getLocalNodeVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Returns the version number (e.g., "16.13.0")
} else {
throw 'Could not parse node version';
throw new Error(`Could not parse node version`);
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export function getLocalRustVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Returns the version number
} else {
throw 'Could not parse rustc version';
throw new Error(`Could not parse rustc version`);
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/wasi2ic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ function getCargoVersion(packageName: string): string {
return match[1]; // Return the version number if no link is found
}
} else {
throw `Could not parse ${packageName} version`;
throw new Error(`Could not parse ${packageName} version`);
}
}

0 comments on commit 94600b1

Please sign in to comment.