Skip to content

Commit

Permalink
fix: print full error and cause with flag --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Sep 9, 2024
1 parent 3c4f208 commit 3cd0084
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function inject(filename, resourceName, resourceData, options) {

try {
await fs.access(filename, constants.R_OK | constants.W_OK);
} catch {
throw new Error("Can't read and write to target executable");
} catch (cause) {
throw new Error("Can't read and write to target executable", { cause });
}

let executable;
Expand All @@ -26,8 +26,8 @@ async function inject(filename, resourceName, resourceData, options) {

try {
executable = await fs.readFile(filename);
} catch {
throw new Error("Couldn't read target executable");
} catch (cause) {
throw new Error("Couldn't read target executable", { cause });
}
const executableFormat = postject.getExecutableFormat(executable);

Expand Down Expand Up @@ -155,8 +155,8 @@ async function inject(filename, resourceName, resourceData, options) {

try {
await fs.writeFile(filename, buffer);
} catch {
throw new Error("Couldn't write executable");
} catch (cause) {
throw new Error("Couldn't write executable", { cause });
}
}

Expand Down
16 changes: 14 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
const program = require("commander");
const { constants, promises: fs } = require("fs");
const path = require("path");
const { format } = require("util");
const { inject } = require("./api.js");

const logger = {
info: (message) => console.log("\x1b[36m%s\x1b[0m", message),
success: (message) => console.log("\x1b[32m%s\x1b[0m", message),
error: (message) => console.log("\x1b[31mError: %s\x1b[0m", message),
verbose: () => {},
};

function setupVerboseLogger() {
logger.error = (message) => console.log("\x1b[31mError: %s\x1b[0m", format(message)),
logger.verbose = (message) => console.log("\x1b[36m%s\x1b[0m", format(message));
}

async function main(filename, resourceName, resource, options) {
if (options.verbose) {
setupVerboseLogger();
}
if (options.outputApiHeader) {
// Handles --output-api-header.
console.log(
Expand All @@ -25,8 +35,9 @@ async function main(filename, resourceName, resource, options) {
try {
await fs.access(resource, constants.R_OK);
resourceData = await fs.readFile(resource);
} catch {
} catch (err) {
logger.error("Can't read resource file");
logger.verbose(err);
process.exit(1);
}

Expand All @@ -41,7 +52,7 @@ async function main(filename, resourceName, resource, options) {
});
logger.success("💉 Injection done!");
} catch (err) {
logger.error(err.message);
logger.error(err);
process.exit(1);
}
}
Expand Down Expand Up @@ -70,6 +81,7 @@ if (require.main === module) {
)
.option("--output-api-header", "Output the API header to stdout")
.option("--overwrite", "Overwrite the resource if it already exists")
.option("--verbose", "Output verbose logs")
.action(main)
.parse(process.argv);
}
4 changes: 4 additions & 0 deletions test/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ describe("postject CLI", () => {
"node",
[
"./dist/cli.js",
"--verbose",
"unknown-filename",
"foobar",
resourceFilename,
Expand All @@ -134,6 +135,9 @@ describe("postject CLI", () => {
expect(stdout).to.have.string(
"Error: Can't read and write to target executable"
);
expect(stdout).to.have.string(
"Error: ENOENT: no such file or directory"
);
expect(stdout).to.not.have.string("Injection done!");
expect(status).to.equal(1);
}
Expand Down

0 comments on commit 3cd0084

Please sign in to comment.