diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 8657b82..dcd9d92 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -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}'` diff --git a/src/test/utils/throwError.spec.ts b/src/test/utils/throwError.spec.ts new file mode 100644 index 0000000..363ebfb --- /dev/null +++ b/src/test/utils/throwError.spec.ts @@ -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') + }) +}) diff --git a/src/utils/index.ts b/src/utils/index.ts index 2a05d63..6501f54 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -15,3 +15,4 @@ export * from './parseWeboutResponse' export * from './fetchLogByChunks' export * from './getValidJson' export * from './parseViyaDebugResponse' +export * from './throwError' diff --git a/src/utils/throwError.ts b/src/utils/throwError.ts new file mode 100644 index 0000000..c054e10 --- /dev/null +++ b/src/utils/throwError.ts @@ -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) +}