1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

fix: if response is not valid json throw error #409

This commit is contained in:
2021-06-21 00:45:37 +05:00
parent 9b976d48ca
commit 93c9a34591
4 changed files with 35 additions and 2 deletions

View File

@@ -8,8 +8,9 @@ import { generateFileUploadForm } from '../file/generateFileUploadForm'
import { generateTableUploadForm } from '../file/generateTableUploadForm'
import { RequestClient } from '../request/RequestClient'
import { SASViyaApiClient } from '../SASViyaApiClient'
import { isRelativePath } from '../utils'
import { isRelativePath, isValidJson } from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
export interface WaitingRequstPromise {
promise: Promise<any> | null
@@ -97,6 +98,19 @@ export class WebJobExecutor extends BaseJobExecutor {
this.appendRequest(res, sasJob, config.debug)
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.'
)
}
console.log('WebJobExecutor')
isValidJson(jsonResponse)
this.appendRequest(res, sasJob, config.debug)
resolve(res.result)
}
isValidJson(res.result as string)
this.appendRequest(res, sasJob, config.debug)
resolve(res.result)
})

View File

@@ -11,6 +11,7 @@ import {
import { parseWeboutResponse } from '../utils/parseWeboutResponse'
import { prefixMessage } from '@sasjs/utils/error'
import { SAS9AuthError } from '../types/errors/SAS9AuthError'
import { isValidJson } from '../utils'
export interface HttpClient {
get<T>(
@@ -419,7 +420,13 @@ export class RequestClient implements HttpClient {
}
} catch {
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.')
}
console.log('RequestClient')
isValidJson(weboutResponse)
parsedResponse = JSON.parse(weboutResponse)
} catch {
parsedResponse = response.data
}

View File

@@ -12,3 +12,4 @@ export * from './serialize'
export * from './splitChunks'
export * from './parseWeboutResponse'
export * from './fetchLogByChunks'
export * from './isValidJson'

11
src/utils/isValidJson.ts Normal file
View 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.')
}
}