Skip to content

Commit

Permalink
Merge pull request #328 from sasjs/issue-322
Browse files Browse the repository at this point in the history
fix: provide logging for network requests
  • Loading branch information
allanbowe authored Apr 27, 2022
2 parents 01d797c + 5bf939e commit f6b0863
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
27 changes: 21 additions & 6 deletions src/commands/execute-code/ExecuteCodeCommand.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -150,6 +163,7 @@ const createAndOpenLogFile = async (
)

outputChannel.appendLine(`Log content: ${log}`)
outputChannel.show()

await createFile(resultsPath, log)
const document = await workspace.openTextDocument(resultsPath)
Expand All @@ -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) {
Expand Down
20 changes: 13 additions & 7 deletions src/commands/select-target/selectTargetCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}
}
4 changes: 3 additions & 1 deletion src/utils/createTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand All @@ -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.')
}
}

Expand Down

0 comments on commit f6b0863

Please sign in to comment.