mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-09 05:20:05 +00:00
Merge pull request #431 from sasjs/issue-409
fix: if response does not contain valid JSON throw error #409
This commit is contained in:
@@ -8,8 +8,9 @@ import { generateFileUploadForm } from '../file/generateFileUploadForm'
|
|||||||
import { generateTableUploadForm } from '../file/generateTableUploadForm'
|
import { generateTableUploadForm } from '../file/generateTableUploadForm'
|
||||||
import { RequestClient } from '../request/RequestClient'
|
import { RequestClient } from '../request/RequestClient'
|
||||||
import { SASViyaApiClient } from '../SASViyaApiClient'
|
import { SASViyaApiClient } from '../SASViyaApiClient'
|
||||||
import { isRelativePath } from '../utils'
|
import { isRelativePath, isValidJson } from '../utils'
|
||||||
import { BaseJobExecutor } from './JobExecutor'
|
import { BaseJobExecutor } from './JobExecutor'
|
||||||
|
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||||
|
|
||||||
export interface WaitingRequstPromise {
|
export interface WaitingRequstPromise {
|
||||||
promise: Promise<any> | null
|
promise: Promise<any> | null
|
||||||
@@ -100,6 +101,19 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
this.appendRequest(res, sasJob, config.debug)
|
this.appendRequest(res, sasJob, config.debug)
|
||||||
resolve(jsonResponse)
|
resolve(jsonResponse)
|
||||||
}
|
}
|
||||||
|
if (this.serverType === ServerType.Sas9 && config.debug) {
|
||||||
|
const jsonResponse = parseWeboutResponse(res.result as string)
|
||||||
|
if (jsonResponse === '') {
|
||||||
|
throw new Error(
|
||||||
|
'Valid JSON could not be extracted from response.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
isValidJson(jsonResponse)
|
||||||
|
this.appendRequest(res, sasJob, config.debug)
|
||||||
|
resolve(res.result)
|
||||||
|
}
|
||||||
|
isValidJson(res.result as string)
|
||||||
this.appendRequest(res, sasJob, config.debug)
|
this.appendRequest(res, sasJob, config.debug)
|
||||||
resolve(res.result)
|
resolve(res.result)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
|
||||||
import { prefixMessage } from '@sasjs/utils/error'
|
import { prefixMessage } from '@sasjs/utils/error'
|
||||||
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
|
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
|
||||||
|
import { isValidJson } from '../utils'
|
||||||
|
|
||||||
export interface HttpClient {
|
export interface HttpClient {
|
||||||
get<T>(
|
get<T>(
|
||||||
@@ -423,7 +424,13 @@ export class RequestClient implements HttpClient {
|
|||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
try {
|
try {
|
||||||
parsedResponse = JSON.parse(parseWeboutResponse(response.data))
|
const weboutResponse = parseWeboutResponse(response.data)
|
||||||
|
if (weboutResponse === '') {
|
||||||
|
throw new Error('Valid JSON could not be extracted from response.')
|
||||||
|
}
|
||||||
|
|
||||||
|
isValidJson(weboutResponse)
|
||||||
|
parsedResponse = JSON.parse(weboutResponse)
|
||||||
} catch {
|
} catch {
|
||||||
parsedResponse = response.data
|
parsedResponse = response.data
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ export * from './serialize'
|
|||||||
export * from './splitChunks'
|
export * from './splitChunks'
|
||||||
export * from './parseWeboutResponse'
|
export * from './parseWeboutResponse'
|
||||||
export * from './fetchLogByChunks'
|
export * from './fetchLogByChunks'
|
||||||
|
export * from './isValidJson'
|
||||||
|
|||||||
11
src/utils/isValidJson.ts
Normal file
11
src/utils/isValidJson.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* Checks if string is in valid JSON format else throw error.
|
||||||
|
* @param str - string to check.
|
||||||
|
*/
|
||||||
|
export const isValidJson = (str: string) => {
|
||||||
|
try {
|
||||||
|
JSON.parse(str)
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('Invalid JSON response.')
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user