From a19de50e67d7a477881dc4edeaab7fef84a50d8d Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 9 May 2023 16:59:18 +0300 Subject: [PATCH] fix(create-folder): improved error message --- src/SASViyaApiClient.spec.ts | 22 ++++++++++++++++++++++ src/SASViyaApiClient.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/SASViyaApiClient.spec.ts b/src/SASViyaApiClient.spec.ts index 516c5bb..13c5199 100644 --- a/src/SASViyaApiClient.spec.ts +++ b/src/SASViyaApiClient.spec.ts @@ -29,11 +29,33 @@ describe('SASViyaApiClient', () => { jest .spyOn(requestClient, 'get') .mockImplementation(() => Promise.reject('Not Found')) + const error = await sasViyaApiClient .createFolder('test', '/foo') .catch((e: any) => e) + expect(error).toBeInstanceOf(RootFolderNotFoundError) }) + + it('should throw an error when ', async () => { + const testMessage1 = 'test message 1' + const testMessage2 = 'test message 2.' + + jest.spyOn(requestClient, 'post').mockImplementation(() => { + return Promise.reject({ + message: testMessage1, + response: { data: { message: testMessage2 }, status: 409 } + }) + }) + + const error = await sasViyaApiClient + .createFolder('test', '/foo') + .catch((e: any) => e) + + const expectedError = `${testMessage1}. ${testMessage2} To override, please set "isForced" to "true".` + + expect(error).toEqual(expectedError) + }) }) const setupMocks = () => { diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 04dd276..306223b 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -434,8 +434,8 @@ export class SASViyaApiClient { } } - const { result: createFolderResponse } = - await this.requestClient.post( + const { result: createFolderResponse } = await this.requestClient + .post( `/folders/folders?parentFolderUri=${parentFolderUri}`, { name: folderName, @@ -443,12 +443,34 @@ export class SASViyaApiClient { }, accessToken ) + .catch((err) => { + const { message, response } = err + + if (message && response && response.data && response.data.message) { + const { status } = response + const { message: responseMessage } = response.data + const messages = [message, responseMessage].map((mes: string) => + /\.$/.test(mes) ? mes : `${mes}.` + ) + + if (!isForced && status === 409) { + messages.push(`To override, please set "isForced" to "true".`) + } + + const errMessage = messages.join(' ') + + throw errMessage + } + + throw err + }) // update folder map with newly created folder. await this.populateFolderMap( `${parentFolderPath}/${folderName}`, accessToken ) + return createFolderResponse }