Skip to content

Commit

Permalink
Plat-13978 display error (#1110)
Browse files Browse the repository at this point in the history
* PLAT-13978: Adding error display

* PLAT-13978: Making more generic response handler

* PLAT-13978: Updating error handling of publish action

* PLAT-13978: Updating handling of publish
  • Loading branch information
taylor-cb authored Mar 6, 2024
1 parent 33b9699 commit 15bdfd0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
44 changes: 24 additions & 20 deletions scripts/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import inquirer from "inquirer";
import { invalid, section, valid } from "../utils.js";
import ora from "ora";
import { apiClient } from "./utils/apiClient.js";
import { printServerFieldValidationErrors } from "./utils/response.js";

const POLL_INTERVAL = 2000;

const IN_PROGRESS_STATUSES = ["PENDING", "STARTED"];
const IN_PROGRESS_STATUSES = ["CREATED", "STARTED"];

const waitForTaskToComplete = async (taskId) => {
const waitingSpinner = ora(
Expand All @@ -30,15 +31,6 @@ const waitForTaskToComplete = async (taskId) => {
if (!processingResult.ok) {
waitingSpinner.stop();

// TODO - ask the BE to supply a 400 when there's a regular failure?
if (processingResult.status === 500) {
const result = await processingResult.text();
invalid(`
Crowdbotics CLI has encountered an error while waiting for the publish command to complete. Publishing may still finish successfully, however you may contact support with reference id: '${taskId}'
Error details: ${result}
`);
}

invalid(
`Crowdbotics CLI has encountered an error while waiting for the publish command to complete. Publishing may still finish successfully, however you may contact support with reference id: '${taskId}'`
);
Expand All @@ -47,14 +39,6 @@ Error details: ${result}

latestProcessingResultBody = await processingResult.json();

// There were some instances while testing where status was coming back as undefined. This may not be an issue anymore.
if (!latestProcessingResultBody.status) {
waitingSpinner.stop();
invalid(
`Crowdbotics CLI has encountered an error while waiting for the publish command to complete. Publishing may still finish successfully, however you may contact support with reference id: '${taskId}'`
);
}

// If we've reached an end state for the polling, end the polling loop.
if (!IN_PROGRESS_STATUSES.includes(latestProcessingResultBody.status)) {
break;
Expand All @@ -66,7 +50,7 @@ Error details: ${result}
if (latestProcessingResultBody.status === "FAILURE") {
invalid(`
Crowdbotics CLI has encountered an error while waiting for the publish command to complete. Publishing may still finish successfully, however you may contact support with reference id: '${taskId}'
Error details: ${JSON.stringify(latestProcessingResultBody)}
Error details: ${latestProcessingResultBody.reason}
`);
}

Expand Down Expand Up @@ -153,7 +137,27 @@ export const publish = async () => {
publishSpinner.stop();

if (!createResult.ok) {
invalid("Unable to publish module to catalog.");
const errorActionString = "Unable to publish module to catalog.";

let errorBody;
try {
errorBody = await createResult.json();
} catch {
invalid(`${errorActionString} An unexpected error has occurred.`);
return;
}

if (createResult.status === 400) {
await printServerFieldValidationErrors(errorBody, errorActionString);
} else if (createResult.status === 403) {
invalid(
`${errorActionString} Current user is unauthorized to publish modules.`
);
} else if (createResult.status === 404 && errorBody.message) {
invalid(`${errorActionString} ${errorBody.message}`);
} else {
invalid(`${errorActionString} An unexpected error has occurred.`);
}
}

const taskResult = await createResult.json();
Expand Down
20 changes: 20 additions & 0 deletions scripts/utils/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { invalid } from "../../utils.js";

export async function printServerFieldValidationErrors(
response,
actionMessage
) {
let errorString = `${actionMessage}\n`;

errorString = "Server returned the following errors:\n";

if (response && response.errors) {
response.errors.forEach((error) => {
errorString += `\t- ${error.field}: ${error.message}\n`;
});
} else {
errorString += "\t- Unexpected error occurred\n";
}

invalid(errorString);
}

0 comments on commit 15bdfd0

Please sign in to comment.