1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-08 13:00:05 +00:00

fix: create a utility throwError and add test case for it

This commit is contained in:
2021-07-26 15:30:19 +05:00
parent 26f008d527
commit 710056bded
4 changed files with 37 additions and 19 deletions

View File

@@ -1,4 +1,4 @@
import { isRelativePath, isUri, isUrl } from './utils'
import { isRelativePath, isUri, isUrl, rootFolderNotFound } from './utils'
import * as NodeFormData from 'form-data'
import {
Job,
@@ -14,13 +14,7 @@ import {
import { JobExecutionError } from './types/errors'
import { SessionManager } from './SessionManager'
import { ContextManager } from './ContextManager'
import { decodeToken } from '@sasjs/utils/auth'
import {
SasAuthResponse,
MacroVar,
AuthConfig,
DecodedToken
} from '@sasjs/utils/types'
import { SasAuthResponse, MacroVar, AuthConfig } from '@sasjs/utils/types'
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
import { RequestClient } from './request/RequestClient'
import { prefixMessage } from '@sasjs/utils/error'
@@ -387,17 +381,7 @@ export class SASViyaApiClient {
)
const newFolderName = `${parentFolderPath.split('/').pop()}`
if (newParentFolderPath === '') {
let error: string = `Root folder ${parentFolderPath} was not found\nPlease check ${this.serverUrl}/SASDrive\nIf folder DOES exist then it is likely a permission problem\n`
if (accessToken) {
const decodedToken: DecodedToken = decodeToken(accessToken)
const scope = decodedToken.scope
error =
error + `The following scopes are contained in access token:\n`
scope.forEach((element: any) => {
error = error + `* ${element}\n`
})
}
throw new Error(error)
rootFolderNotFound(parentFolderPath, this.serverUrl, accessToken)
}
logger.info(
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`

View File

@@ -0,0 +1,16 @@
import { rootFolderNotFound } from '../../utils'
describe('root folder not found', () => {
it('when access token is provided, error message should contain scope of accessToken', () => {
const token =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJzY29wZS0xIiwic2NvcGUtMiJdfQ.ktqPL2ulln-8Asa2jSV9QCfDYmQuNk4tNKopxJR5xZs'
let error
try {
rootFolderNotFound('/myProject', 'https://analytium.co.uk', token)
} catch (err) {
error = err.message
}
expect(error).toContain('scope-1')
expect(error).toContain('scope-2')
})
})

View File

@@ -15,3 +15,4 @@ export * from './parseWeboutResponse'
export * from './fetchLogByChunks'
export * from './getValidJson'
export * from './parseViyaDebugResponse'
export * from './throwError'

17
src/utils/throwError.ts Normal file
View File

@@ -0,0 +1,17 @@
import { DecodedToken, decodeToken } from '@sasjs/utils'
export const rootFolderNotFound = (
parentFolderPath: string,
serverUrl: string,
accessToken?: string
) => {
let error: string = `Root folder ${parentFolderPath} was not found\nPlease check ${serverUrl}/SASDrive\nIf folder DOES exist then it is likely a permission problem\n`
if (accessToken) {
const decodedToken: DecodedToken = decodeToken(accessToken)
let scope = decodedToken.scope
scope = scope.map((element) => '* ' + element)
error +=
`The following scopes are contained in access token:\n` + scope.join('\n')
}
throw new Error(error)
}