From 5bf939e8c93688edfae833f9bcfdd1c62671dbda Mon Sep 17 00:00:00 2001
From: Sabir Hassan <sabirbk06@gmail.com>
Date: Wed, 27 Apr 2022 18:07:00 +0500
Subject: [PATCH] fix: provide logging for network requests

---
 .../execute-code/ExecuteCodeCommand.ts        | 27 ++++++++++++++-----
 .../select-target/selectTargetCommand.ts      | 20 +++++++++-----
 src/utils/createTarget.ts                     |  4 ++-
 3 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/src/commands/execute-code/ExecuteCodeCommand.ts b/src/commands/execute-code/ExecuteCodeCommand.ts
index 14543f1..857de00 100644
--- a/src/commands/execute-code/ExecuteCodeCommand.ts
+++ b/src/commands/execute-code/ExecuteCodeCommand.ts
@@ -1,5 +1,5 @@
 import SASjs from '@sasjs/adapter/node'
-import { ServerType } from '@sasjs/utils'
+import { ServerType, Target } from '@sasjs/utils'
 import * as os from 'os'
 import * as path from 'path'
 import {
@@ -33,8 +33,21 @@ export class ExecuteCodeCommand {
 
   private executeCode = async () => {
     this.outputChannel.appendLine('Initialising SASjs.')
-    const target = await selectTarget(this.outputChannel)
+    let target: Target | undefined
+    try {
+      target = await selectTarget(this.outputChannel)
+    } catch (error: any) {
+      this.outputChannel.appendLine('SASjs: Error selecting target: ')
+      this.outputChannel.appendLine(error)
+      this.outputChannel.appendLine(error.message)
+      this.outputChannel.appendLine(JSON.stringify(error, null, 2))
+      this.outputChannel.show()
+    }
+
     if (!target) {
+      window.showErrorMessage(
+        'An unexpected error occurred while selecting target.'
+      )
       return
     }
 
@@ -150,6 +163,7 @@ const createAndOpenLogFile = async (
   )
 
   outputChannel.appendLine(`Log content: ${log}`)
+  outputChannel.show()
 
   await createFile(resultsPath, log)
   const document = await workspace.openTextDocument(resultsPath)
@@ -159,10 +173,11 @@ const createAndOpenLogFile = async (
 }
 
 const handleErrorResponse = async (e: any, outputChannel: OutputChannel) => {
-  outputChannel.append('SASjs: Error executing code: ')
-  outputChannel.append(e)
-  outputChannel.append(e.message)
-  outputChannel.append(JSON.stringify(e, null, 2))
+  outputChannel.appendLine('SASjs: Error executing code: ')
+  outputChannel.appendLine(e)
+  outputChannel.appendLine(e.message)
+  outputChannel.appendLine(JSON.stringify(e, null, 2))
+  outputChannel.show()
 
   const { log } = e
   if (log) {
diff --git a/src/commands/select-target/selectTargetCommand.ts b/src/commands/select-target/selectTargetCommand.ts
index dde2491..8f25629 100644
--- a/src/commands/select-target/selectTargetCommand.ts
+++ b/src/commands/select-target/selectTargetCommand.ts
@@ -17,12 +17,18 @@ export class SelectTargetCommand {
   }
 
   private selectTarget = async () => {
-    configureTarget(this.outputChannel).then((target) => {
-      if (target) {
-        window.showInformationMessage(`Selected Target: ${target.name}`)
-      } else {
-        window.showInformationMessage('No target selected!')
-      }
-    })
+    configureTarget(this.outputChannel)
+      .then((target) => {
+        if (target) {
+          window.showInformationMessage(`Selected Target: ${target.name}`)
+        } else {
+          window.showErrorMessage('No target selected!')
+        }
+      })
+      .catch((err) => {
+        window.showErrorMessage('No target selected!')
+        this.outputChannel.appendLine(err.message)
+        this.outputChannel.show()
+      })
   }
 }
diff --git a/src/utils/createTarget.ts b/src/utils/createTarget.ts
index f3ee669..5b415dd 100644
--- a/src/utils/createTarget.ts
+++ b/src/utils/createTarget.ts
@@ -58,7 +58,7 @@ export const createTarget = async (outputChannel: OutputChannel) => {
     targetJson.authConfigSas9 = { userName, password: encodeToBase64(password) }
   } else if (serverType === ServerType.Sasjs) {
     const res = await axios.get(`${serverUrl}/SASjsApi/info`)
-    if (res.data.mode === 'server') {
+    if (res.data?.mode === 'server') {
       const clientId = await getClientId()
       env.openExternal(Uri.parse(getAuthUrl(serverType, serverUrl, clientId)))
 
@@ -80,6 +80,8 @@ export const createTarget = async (outputChannel: OutputChannel) => {
       )
 
       targetJson.authConfig = authResponse
+    } else if (res.data?.mode !== 'desktop') {
+      throw new Error('An unexpected error occurred while creating target.')
     }
   }