From d1c89b79cba7573feb16e1ac5ea677472d4a80cc Mon Sep 17 00:00:00 2001 From: Joe Morris Date: Tue, 17 Oct 2023 10:37:09 -0400 Subject: [PATCH] fix: better error handling --- client/src/commands/run.ts | 43 ++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/client/src/commands/run.ts b/client/src/commands/run.ts index 3df354883..ba0e48b84 100644 --- a/client/src/commands/run.ts +++ b/client/src/commands/run.ts @@ -170,10 +170,7 @@ const _run = async (selected = false, uri?: Uri) => { await runCode(selected, uri) .catch((err) => { - console.dir(err); - window.showErrorMessage( - err.response?.data ? JSON.stringify(err.response.data) : err.message, - ); + onRunError(err); }) .finally(() => { running = false; @@ -235,3 +232,41 @@ export async function runTask( messageEmitter.fire(`${l10n.t("SAS code running...")}\r\n`); return cancelled ? undefined : session.run(code); } + +const isErrorRep = (err: unknown): err is SessionError => { + if ( + err && + typeof err === "object" && + "message" in err && + "details" in err && + "errorCode" in err + ) { + return true; + } + return false; +}; + +const onRunError = (err) => { + console.dir(err); + + if (err.response) { + // The request was made and we got a status code that falls out side of the 2xx range + const errorData = err.response.data; + + if (isErrorRep(errorData)) { + //errorData is an error representation, extract out the details to show a better message + const details = errorData.details; + const options = { + modal: true, + detail: details.join("\n"), + }; + window.showErrorMessage(errorData.message, options); + } else { + window.showErrorMessage(err.message); + } + } else { + // Either the request was made but no response was received, or + // there was an issue in the request setup itself, just show the message + window.showErrorMessage(err.message); + } +};