Skip to content

Commit

Permalink
Don't use JSON.stringify for logging purposes (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
kzantow committed Apr 5, 2022
1 parent ced7eec commit f6c3d0f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 38 deletions.
23 changes: 15 additions & 8 deletions dist/attachReleaseAssets/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions dist/downloadSyft/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions dist/runSyftAction/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions src/github/GithubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ export function dashWrap(str: string): string {
}

/**
* Logs all objects passed in debug outputting strings directly and
* calling JSON.stringify on other elements in a group with the given label
* Attempts to intelligently log all objects passed in when debug is enabled
*/
export function debugLog(label: string, ...args: unknown[]): void {
if (core.isDebug()) {
Expand All @@ -107,9 +106,9 @@ export function debugLog(label: string, ...args: unknown[]): void {
core.debug(arg);
} else if (arg instanceof Error) {
core.debug(arg.message);
core.debug(JSON.stringify(arg.stack));
console.log(arg.stack);
} else {
core.debug(JSON.stringify(arg));
console.log(arg);
}
}
});
Expand Down Expand Up @@ -432,16 +431,14 @@ export class GithubClient {
);

if (response.status >= 400) {
core.warning(
`Dependency snapshot upload failed: ${JSON.stringify(response)}`
);
core.warning(`Dependency snapshot upload failed:`);
console.log(response);
} else {
debugLog(`Dependency snapshot upload successful:`, response);
}
} catch (e) {
core.warning(
`Error uploading depdendency snapshot: ${JSON.stringify(e)}`
);
core.warning(`Error uploading depdendency snapshot:`);
console.log(e);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/github/SyftGithubAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export async function runSyftAction(): Promise<void> {
core.exportVariable(PRIOR_ARTIFACT_ENV_VAR, getArtifactName());
}
} else {
throw new Error(`No Syft output: ${JSON.stringify(output)}`);
throw new Error(`No Syft output`);
}
}

Expand Down Expand Up @@ -517,7 +517,12 @@ export async function runAndFailBuildOnException<T>(
if (e instanceof Error) {
core.setFailed(e.message);
} else if (e instanceof Object) {
core.setFailed(JSON.stringify(e));
try {
core.setFailed(JSON.stringify(e));
} catch (e) {
core.setFailed("Action failed");
console.error(e);
}
} else {
core.setFailed(`An unknown error occurred: ${e}`);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/GithubClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ describe("Github Client", () => {
}
});

debugLog("the_label", { the: "obj" });
debugLog("the_label", "string");

expect(data.debug.log.length).toBe(1);
expect(data.debug.log[0]).toBe("{\"the\":\"obj\"}");
expect(data.debug.log[0]).toBe("string");
});

it("finds a draft release", async () => {
Expand Down

0 comments on commit f6c3d0f

Please sign in to comment.