From ffdd9554a5ed7dc5bfbdd9e4247f6f0f5d0b0abe Mon Sep 17 00:00:00 2001 From: Kathrin Holzmann Date: Mon, 3 Mar 2025 16:15:39 +0100 Subject: [PATCH 1/2] experiment: return custom error object with message code --- lib/adapters/REST/endpoints/ui-config.ts | 25 ++++++++++++++----- .../adapters/REST/endpoints/ui-config.test.ts | 23 ++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/adapters/REST/endpoints/ui-config.ts b/lib/adapters/REST/endpoints/ui-config.ts index c882abab74..601d906d08 100644 --- a/lib/adapters/REST/endpoints/ui-config.ts +++ b/lib/adapters/REST/endpoints/ui-config.ts @@ -16,16 +16,29 @@ export const get: RestEndpoint<'UIConfig', 'get'> = ( return raw.get(http, getUrl(params)) } -export const update: RestEndpoint<'UIConfig', 'update'> = ( +export const update: RestEndpoint<'UIConfig', 'update'> = async( http: AxiosInstance, params: GetUIConfigParams, rawData: UIConfigProps ) => { const data: SetOptional = copy(rawData) delete data.sys - return raw.put(http, getUrl(params), data, { - headers: { - 'X-Contentful-Version': rawData.sys.version ?? 0, - }, - }) + try{ + return await raw.put(http, getUrl(params), data, { + headers: { + 'X-Contentful-Version': rawData.sys.version ?? 0, + }, + }) + } catch(error){ + throw { + sys: { + type: 'Error', + id: 'Failed', + }, + message: 'Update has failed', + details: 'Saved view could not be updated', + message_code: 'savedViews.update.Failed', + } + } } + diff --git a/test/unit/adapters/REST/endpoints/ui-config.test.ts b/test/unit/adapters/REST/endpoints/ui-config.test.ts index 424284d784..24cdc70096 100644 --- a/test/unit/adapters/REST/endpoints/ui-config.test.ts +++ b/test/unit/adapters/REST/endpoints/ui-config.test.ts @@ -32,4 +32,25 @@ describe('Rest UIConfig', () => { } }) }) -}) + + test('UIConfig update fails with custom error message', async () => { + const errorMessage = 'Network error'; + const { adapterMock } = setup(Promise.reject(new Error(errorMessage))); + const entityMock = cloneMock('uiConfig'); + const entity = wrapUIConfig((...args) => adapterMock.makeRequest(...args), entityMock); + + try { + await entity.update(); + } catch (error) { + expect(error).toEqual({ + sys: { + type: 'Error', + id: 'Failed', + }, + message: 'Update has failed', + details: 'Saved view could not be updated', + message_code: 'savedViews.update.Failed', + }); + } + }); +}); From c126dc49f48b0e255caf4433428a832eb1ed5f9e Mon Sep 17 00:00:00 2001 From: Kathrin Holzmann Date: Mon, 3 Mar 2025 16:30:06 +0100 Subject: [PATCH 2/2] chore: prettier --- lib/adapters/REST/endpoints/ui-config.ts | 7 +++---- .../adapters/REST/endpoints/ui-config.test.ts | 16 ++++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/adapters/REST/endpoints/ui-config.ts b/lib/adapters/REST/endpoints/ui-config.ts index 601d906d08..6746f6ef7c 100644 --- a/lib/adapters/REST/endpoints/ui-config.ts +++ b/lib/adapters/REST/endpoints/ui-config.ts @@ -16,20 +16,20 @@ export const get: RestEndpoint<'UIConfig', 'get'> = ( return raw.get(http, getUrl(params)) } -export const update: RestEndpoint<'UIConfig', 'update'> = async( +export const update: RestEndpoint<'UIConfig', 'update'> = async ( http: AxiosInstance, params: GetUIConfigParams, rawData: UIConfigProps ) => { const data: SetOptional = copy(rawData) delete data.sys - try{ + try { return await raw.put(http, getUrl(params), data, { headers: { 'X-Contentful-Version': rawData.sys.version ?? 0, }, }) - } catch(error){ + } catch (error) { throw { sys: { type: 'Error', @@ -41,4 +41,3 @@ export const update: RestEndpoint<'UIConfig', 'update'> = async( } } } - diff --git a/test/unit/adapters/REST/endpoints/ui-config.test.ts b/test/unit/adapters/REST/endpoints/ui-config.test.ts index 24cdc70096..d9dd153cd8 100644 --- a/test/unit/adapters/REST/endpoints/ui-config.test.ts +++ b/test/unit/adapters/REST/endpoints/ui-config.test.ts @@ -34,13 +34,13 @@ describe('Rest UIConfig', () => { }) test('UIConfig update fails with custom error message', async () => { - const errorMessage = 'Network error'; - const { adapterMock } = setup(Promise.reject(new Error(errorMessage))); - const entityMock = cloneMock('uiConfig'); - const entity = wrapUIConfig((...args) => adapterMock.makeRequest(...args), entityMock); + const errorMessage = 'Network error' + const { adapterMock } = setup(Promise.reject(new Error(errorMessage))) + const entityMock = cloneMock('uiConfig') + const entity = wrapUIConfig((...args) => adapterMock.makeRequest(...args), entityMock) try { - await entity.update(); + await entity.update() } catch (error) { expect(error).toEqual({ sys: { @@ -50,7 +50,7 @@ describe('Rest UIConfig', () => { message: 'Update has failed', details: 'Saved view could not be updated', message_code: 'savedViews.update.Failed', - }); + }) } - }); -}); + }) +})