Skip to content

Commit f4cd7b9

Browse files
committed
Return error response in submitBom
When an error occurs, there is no possibility for the calling code to know the details. In my case, I'd like to be able to log error response body, in particular to be able to know which validation is KO when uploading a SBOM to DependencyTrack (that recently enabled JsonSchema validation on upload). Signed-off-by: Maxime Robert <[email protected]> Signed-off-by: Maxime Robert <[email protected]>
1 parent df888c3 commit f4cd7b9

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

bin/cdxgen.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,9 @@ const checkPermissions = (filePath) => {
676676
if (options.serverUrl && options.serverUrl != true && options.apiKey) {
677677
try {
678678
const dbody = await submitBom(options, bomNSData.bomJson);
679-
console.log("Response from server", dbody);
679+
if (!dbody?.body?.errors) {
680+
console.log("Response from server", dbody);
681+
}
680682
} catch (err) {
681683
console.log(err);
682684
}

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6194,6 +6194,7 @@ export async function createBom(path, options) {
61946194
*
61956195
* @param {Object} args CLI args
61966196
* @param {Object} bomContents BOM Json
6197+
* @return {Promise<{ token: string } | { body: { errors: string[] } } | undefined>} a promise with a token (if request was successful), a body with errors (if request failed) or undefined (in case of invalid arguments)
61976198
*/
61986199
export async function submitBom(args, bomContents) {
61996200
const serverUrl = `${args.serverUrl.replace(/\/$/, "")}/api/v1/bom`;
@@ -6277,11 +6278,11 @@ export async function submitBom(args, bomContents) {
62776278
console.log(
62786279
"Unable to submit the SBOM to the Dependency-Track server using POST method",
62796280
);
6280-
console.log(error);
62816281
}
62826282
} else {
62836283
console.log("Unable to submit the SBOM to the Dependency-Track server");
6284-
console.log(error);
62856284
}
6285+
console.log(error.response?.body);
6286+
return error.response;
62866287
}
62876288
}

server.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@ const start = (options) => {
131131
if (!filePath) {
132132
res.writeHead(500, { "Content-Type": "application/json" });
133133
return res.end(
134-
"{'error': 'true', 'message': 'path or url is required.'}\n",
134+
JSON.stringify({
135+
error: true,
136+
message: "path or url is required.",
137+
}),
135138
);
136139
}
137-
res.writeHead(200, { "Content-Type": "application/json" });
138140
let srcDir = filePath;
139141
if (filePath.startsWith("http") || filePath.startsWith("git")) {
140142
srcDir = gitClone(filePath, reqOptions.gitBranch);
@@ -145,6 +147,22 @@ const start = (options) => {
145147
if (reqOptions.requiredOnly || reqOptions["filter"] || reqOptions["only"]) {
146148
bomNSData = postProcess(bomNSData, reqOptions);
147149
}
150+
if (reqOptions.serverUrl && reqOptions.apiKey) {
151+
console.log("Publishing SBOM to Dependency Track");
152+
const response = await submitBom(reqOptions, bomNSData.bomJson);
153+
const errorMessages = response?.body?.errors;
154+
if (errorMessages) {
155+
res.writeHead(500, { "Content-Type": "application/json" });
156+
return res.end(
157+
JSON.stringify({
158+
error: true,
159+
message: "Unable to submit the SBOM to the Dependency-Track server",
160+
detail: errorMessages,
161+
}),
162+
);
163+
}
164+
}
165+
res.writeHead(200, { "Content-Type": "application/json" });
148166
if (bomNSData.bomJson) {
149167
if (
150168
typeof bomNSData.bomJson === "string" ||
@@ -155,10 +173,6 @@ const start = (options) => {
155173
res.write(JSON.stringify(bomNSData.bomJson, null, null));
156174
}
157175
}
158-
if (reqOptions.serverUrl && reqOptions.apiKey) {
159-
console.log("Publishing SBOM to Dependency Track");
160-
submitBom(reqOptions, bomNSData.bomJson);
161-
}
162176
res.end("\n");
163177
if (cleanup && srcDir && srcDir.startsWith(os.tmpdir()) && fs.rmSync) {
164178
console.log(`Cleaning up ${srcDir}`);

types/index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,13 @@ export function createBom(path: string, options: any): any;
260260
*
261261
* @param {Object} args CLI args
262262
* @param {Object} bomContents BOM Json
263+
* @return {Promise<{ token: string } | { body: { errors: string[] } } | undefined>} a promise with a token (if request was successful), a body with errors (if request failed) or undefined (in case of invalid arguments)
263264
*/
264-
export function submitBom(args: any, bomContents: any): Promise<any>;
265+
export function submitBom(args: any, bomContents: any): Promise<{
266+
token: string;
267+
} | {
268+
body: {
269+
errors: string[];
270+
};
271+
} | undefined>;
265272
//# sourceMappingURL=index.d.ts.map

types/index.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/server.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)