mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-18 01:20:05 +00:00
fix: create a utility throwError and add test case for it
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { isRelativePath, isUri, isUrl } from './utils'
|
import { isRelativePath, isUri, isUrl, rootFolderNotFound } from './utils'
|
||||||
import * as NodeFormData from 'form-data'
|
import * as NodeFormData from 'form-data'
|
||||||
import {
|
import {
|
||||||
Job,
|
Job,
|
||||||
@@ -14,13 +14,7 @@ import {
|
|||||||
import { JobExecutionError } from './types/errors'
|
import { JobExecutionError } from './types/errors'
|
||||||
import { SessionManager } from './SessionManager'
|
import { SessionManager } from './SessionManager'
|
||||||
import { ContextManager } from './ContextManager'
|
import { ContextManager } from './ContextManager'
|
||||||
import { decodeToken } from '@sasjs/utils/auth'
|
import { SasAuthResponse, MacroVar, AuthConfig } from '@sasjs/utils/types'
|
||||||
import {
|
|
||||||
SasAuthResponse,
|
|
||||||
MacroVar,
|
|
||||||
AuthConfig,
|
|
||||||
DecodedToken
|
|
||||||
} from '@sasjs/utils/types'
|
|
||||||
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
|
import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired'
|
||||||
import { RequestClient } from './request/RequestClient'
|
import { RequestClient } from './request/RequestClient'
|
||||||
import { prefixMessage } from '@sasjs/utils/error'
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
@@ -387,17 +381,7 @@ export class SASViyaApiClient {
|
|||||||
)
|
)
|
||||||
const newFolderName = `${parentFolderPath.split('/').pop()}`
|
const newFolderName = `${parentFolderPath.split('/').pop()}`
|
||||||
if (newParentFolderPath === '') {
|
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`
|
rootFolderNotFound(parentFolderPath, this.serverUrl, accessToken)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
logger.info(
|
logger.info(
|
||||||
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`
|
`Creating parent folder:\n'${newFolderName}' in '${newParentFolderPath}'`
|
||||||
|
|||||||
16
src/test/utils/throwError.spec.ts
Normal file
16
src/test/utils/throwError.spec.ts
Normal 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')
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -15,3 +15,4 @@ export * from './parseWeboutResponse'
|
|||||||
export * from './fetchLogByChunks'
|
export * from './fetchLogByChunks'
|
||||||
export * from './getValidJson'
|
export * from './getValidJson'
|
||||||
export * from './parseViyaDebugResponse'
|
export * from './parseViyaDebugResponse'
|
||||||
|
export * from './throwError'
|
||||||
|
|||||||
17
src/utils/throwError.ts
Normal file
17
src/utils/throwError.ts
Normal 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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user