From 47fe7686cbc2a2e712df1c16d8531d621a2a1b90 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 00:01:28 +0500 Subject: [PATCH 1/7] chore: introduced new error types: InvalidJsonError, JsonParseArrayError, WeboutResponseError --- src/types/errors/InvalidJsonError.ts | 7 +++++++ src/types/errors/JsonParseArrayError.ts | 7 +++++++ src/types/errors/WeboutResponseError.ts | 7 +++++++ src/types/errors/index.ts | 3 +++ 4 files changed, 24 insertions(+) create mode 100644 src/types/errors/InvalidJsonError.ts create mode 100644 src/types/errors/JsonParseArrayError.ts create mode 100644 src/types/errors/WeboutResponseError.ts diff --git a/src/types/errors/InvalidJsonError.ts b/src/types/errors/InvalidJsonError.ts new file mode 100644 index 0000000..f59339a --- /dev/null +++ b/src/types/errors/InvalidJsonError.ts @@ -0,0 +1,7 @@ +export class InvalidJsonError extends Error { + constructor() { + super('Error: invalid Json string') + this.name = 'InvalidJsonError' + Object.setPrototypeOf(this, InvalidJsonError.prototype) + } +} diff --git a/src/types/errors/JsonParseArrayError.ts b/src/types/errors/JsonParseArrayError.ts new file mode 100644 index 0000000..9006f64 --- /dev/null +++ b/src/types/errors/JsonParseArrayError.ts @@ -0,0 +1,7 @@ +export class JsonParseArrayError extends Error { + constructor(public message: string) { + super(message) + this.name = 'JsonParseArrayError' + Object.setPrototypeOf(this, JsonParseArrayError.prototype) + } +} diff --git a/src/types/errors/WeboutResponseError.ts b/src/types/errors/WeboutResponseError.ts new file mode 100644 index 0000000..9ca1e80 --- /dev/null +++ b/src/types/errors/WeboutResponseError.ts @@ -0,0 +1,7 @@ +export class WeboutResponseError extends Error { + constructor(public url: string) { + super(`Error: error while parsing response from ${url}`) + this.name = 'WeboutResponseError' + Object.setPrototypeOf(this, WeboutResponseError.prototype) + } +} diff --git a/src/types/errors/index.ts b/src/types/errors/index.ts index cca1a97..e2b0766 100644 --- a/src/types/errors/index.ts +++ b/src/types/errors/index.ts @@ -8,3 +8,6 @@ export * from './NotFoundError' export * from './ErrorResponse' export * from './NoSessionStateError' export * from './RootFolderNotFoundError' +export * from './JsonParseArrayError' +export * from './WeboutResponseError' +export * from './InvalidJsonError' From 0bc69401e5a2e3b4015058b64cbcf60e30dbde73 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 00:02:48 +0500 Subject: [PATCH 2/7] chore: refactor code for getValidJson function --- src/test/utils/getValidJson.spec.ts | 15 +++++---------- src/utils/getValidJson.ts | 11 +++++++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/test/utils/getValidJson.spec.ts b/src/test/utils/getValidJson.spec.ts index e7fbf66..a84eb04 100644 --- a/src/test/utils/getValidJson.spec.ts +++ b/src/test/utils/getValidJson.spec.ts @@ -1,4 +1,5 @@ import { getValidJson } from '../../utils' +import { JsonParseArrayError, InvalidJsonError } from '../../types/errors' describe('jsonValidator', () => { it('should not throw an error with a valid json', () => { @@ -19,23 +20,17 @@ describe('jsonValidator', () => { it('should throw an error with an invalid json', () => { const json = `{\"test\":\"test\"\"test2\":\"test\"}` - let errorThrown = false - try { + const test = () => { getValidJson(json) - } catch (error) { - errorThrown = true } - expect(errorThrown).toBe(true) + expect(test).toThrowError(InvalidJsonError) }) it('should throw an error when an array is passed', () => { const array = ['hello', 'world'] - let errorThrown = false - try { + const test = () => { getValidJson(array) - } catch (error) { - errorThrown = true } - expect(errorThrown).toBe(true) + expect(test).toThrow(JsonParseArrayError) }) }) diff --git a/src/utils/getValidJson.ts b/src/utils/getValidJson.ts index 0313157..565ee98 100644 --- a/src/utils/getValidJson.ts +++ b/src/utils/getValidJson.ts @@ -1,16 +1,19 @@ +import { JsonParseArrayError, InvalidJsonError } from '../types/errors' + /** * if string passed then parse the string to json else if throw error for all other types unless it is not a valid json object. * @param str - string to check. */ export const getValidJson = (str: string | object) => { try { - if (Array.isArray(str)) { - throw new Error('Can not parse array object to json.') - } + const arrayErrorMessage = 'Can not parse array object to json.' + if (Array.isArray(str)) throw new JsonParseArrayError(arrayErrorMessage) + if (typeof str === 'object') return str return JSON.parse(str) } catch (e) { - throw new Error('Invalid JSON response.') + if (e instanceof JsonParseArrayError) throw e + throw new InvalidJsonError() } } From 8464e506e0fafe96e9123342b729f3a76adf9e47 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 00:04:30 +0500 Subject: [PATCH 3/7] fix: check for valid json while parsing sas viya debug response --- src/FileUploader.ts | 4 +--- src/utils/parseViyaDebugResponse.ts | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/FileUploader.ts b/src/FileUploader.ts index 4e117df..38d2f52 100644 --- a/src/FileUploader.ts +++ b/src/FileUploader.ts @@ -75,9 +75,7 @@ export class FileUploader { this.requestClient, this.sasjsConfig.serverUrl ) - return typeof jsonResponse === 'string' - ? getValidJson(jsonResponse) - : jsonResponse + return jsonResponse } return typeof res.result === 'string' diff --git a/src/utils/parseViyaDebugResponse.ts b/src/utils/parseViyaDebugResponse.ts index 3137995..b0227f2 100644 --- a/src/utils/parseViyaDebugResponse.ts +++ b/src/utils/parseViyaDebugResponse.ts @@ -1,4 +1,5 @@ import { RequestClient } from '../request/RequestClient' +import { getValidJson } from '../utils' /** * When querying a Viya job using the Web approach (as opposed to using the APIs) with _DEBUG enabled, @@ -25,5 +26,5 @@ export const parseSasViyaDebugResponse = async ( return requestClient .get(serverUrl + jsonUrl, undefined) - .then((res) => res.result) + .then((res: any) => getValidJson(res.result)) } From 63e220c5be2a64acb13343a82a79e5a6ccebf628 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 00:05:52 +0500 Subject: [PATCH 4/7] fix: double parsing issue in sas9 debug mode fixed --- src/job-execution/WebJobExecutor.ts | 21 ++++++++++++--------- src/request/RequestClient.ts | 8 +------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index c75a06e..4058ef7 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -2,7 +2,8 @@ import { ServerType } from '@sasjs/utils/types' import { ErrorResponse, JobExecutionError, - LoginRequiredError + LoginRequiredError, + WeboutResponseError } from '../types/errors' import { generateFileUploadForm } from '../file/generateFileUploadForm' import { generateTableUploadForm } from '../file/generateTableUploadForm' @@ -102,10 +103,11 @@ export class WebJobExecutor extends BaseJobExecutor { const requestPromise = new Promise((resolve, reject) => { this.requestClient!.post(apiUrl, formData, undefined) - .then(async (res) => { + .then(async (res: any) => { + console.log(106, res) if (this.serverType === ServerType.SasViya && config.debug) { const jsonResponse = await parseSasViyaDebugResponse( - res.result as string, + res.result, this.requestClient, this.serverUrl ) @@ -113,18 +115,19 @@ export class WebJobExecutor extends BaseJobExecutor { 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.' - ) + let jsonResponse + if (typeof res.result === 'string') { + jsonResponse = parseWeboutResponse(res.result) + if (jsonResponse === '') throw new WeboutResponseError(apiUrl) + } else { + jsonResponse = res.result } getValidJson(jsonResponse) this.appendRequest(res, sasJob, config.debug) resolve(res.result) } - getValidJson(res.result as string) + getValidJson(res.result) this.appendRequest(res, sasJob, config.debug) resolve(res.result) }) diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index e44a8c4..28c5512 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -429,13 +429,7 @@ export class RequestClient implements HttpClient { } } catch { try { - const weboutResponse = parseWeboutResponse(response.data) - if (weboutResponse === '') { - throw new Error('Valid JSON could not be extracted from response.') - } - - const jsonResponse = getValidJson(weboutResponse) - parsedResponse = jsonResponse + parsedResponse = JSON.parse(parseWeboutResponse(response.data)) } catch { parsedResponse = response.data } From 78149e6c546d55f061d9239382b7531d0e29d049 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 00:27:10 +0500 Subject: [PATCH 5/7] chore: remove console log statement --- src/job-execution/WebJobExecutor.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index 4058ef7..595dea1 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -104,7 +104,6 @@ export class WebJobExecutor extends BaseJobExecutor { const requestPromise = new Promise((resolve, reject) => { this.requestClient!.post(apiUrl, formData, undefined) .then(async (res: any) => { - console.log(106, res) if (this.serverType === ServerType.SasViya && config.debug) { const jsonResponse = await parseSasViyaDebugResponse( res.result, From e72195ca5d0774a58bf710842a9fcdadb8412481 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 16:09:51 +0500 Subject: [PATCH 6/7] fix: predefine jsonParseArrayError message --- src/types/errors/JsonParseArrayError.ts | 4 ++-- src/utils/getValidJson.ts | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/types/errors/JsonParseArrayError.ts b/src/types/errors/JsonParseArrayError.ts index 9006f64..ad08ecd 100644 --- a/src/types/errors/JsonParseArrayError.ts +++ b/src/types/errors/JsonParseArrayError.ts @@ -1,6 +1,6 @@ export class JsonParseArrayError extends Error { - constructor(public message: string) { - super(message) + constructor() { + super('Can not parse array object to json.') this.name = 'JsonParseArrayError' Object.setPrototypeOf(this, JsonParseArrayError.prototype) } diff --git a/src/utils/getValidJson.ts b/src/utils/getValidJson.ts index 565ee98..17f9cbb 100644 --- a/src/utils/getValidJson.ts +++ b/src/utils/getValidJson.ts @@ -6,8 +6,7 @@ import { JsonParseArrayError, InvalidJsonError } from '../types/errors' */ export const getValidJson = (str: string | object) => { try { - const arrayErrorMessage = 'Can not parse array object to json.' - if (Array.isArray(str)) throw new JsonParseArrayError(arrayErrorMessage) + if (Array.isArray(str)) throw new JsonParseArrayError() if (typeof str === 'object') return str From 3c9f133374b1c02cab436493286422c906e033d8 Mon Sep 17 00:00:00 2001 From: sabhas Date: Wed, 18 Aug 2021 16:33:26 +0500 Subject: [PATCH 7/7] fix: throw error from parseWeboutResponse function if unable to find webout response --- src/job-execution/WebJobExecutor.ts | 10 +++------- src/utils/parseWeboutResponse.ts | 5 ++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index 595dea1..560b1bf 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -114,13 +114,9 @@ export class WebJobExecutor extends BaseJobExecutor { resolve(jsonResponse) } if (this.serverType === ServerType.Sas9 && config.debug) { - let jsonResponse - if (typeof res.result === 'string') { - jsonResponse = parseWeboutResponse(res.result) - if (jsonResponse === '') throw new WeboutResponseError(apiUrl) - } else { - jsonResponse = res.result - } + let jsonResponse = res.result + if (typeof res.result === 'string') + jsonResponse = parseWeboutResponse(res.result, apiUrl) getValidJson(jsonResponse) this.appendRequest(res, sasJob, config.debug) diff --git a/src/utils/parseWeboutResponse.ts b/src/utils/parseWeboutResponse.ts index f9bc4db..30d91b6 100644 --- a/src/utils/parseWeboutResponse.ts +++ b/src/utils/parseWeboutResponse.ts @@ -1,4 +1,6 @@ -export const parseWeboutResponse = (response: string) => { +import { WeboutResponseError } from '../types/errors' + +export const parseWeboutResponse = (response: string, url?: string) => { let sasResponse = '' if (response.includes('>>weboutBEGIN<<')) { @@ -7,6 +9,7 @@ export const parseWeboutResponse = (response: string) => { .split('>>weboutBEGIN<<')[1] .split('>>weboutEND<<')[0] } catch (e) { + if (url) throw new WeboutResponseError(url) sasResponse = '' console.error(e) }