Skip to content

fix: print full error and cause with flag --verbose #100

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
Loading