Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/adapters/REST/endpoints/ui-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ export const get: RestEndpoint<'UIConfig', 'get'> = (
return raw.get<UIConfigProps>(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<typeof rawData, 'sys'> = copy(rawData)
delete data.sys
return raw.put<UIConfigProps>(http, getUrl(params), data, {
headers: {
'X-Contentful-Version': rawData.sys.version ?? 0,
},
})
try {
return await raw.put<UIConfigProps>(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',
}
}
}
21 changes: 21 additions & 0 deletions test/unit/adapters/REST/endpoints/ui-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
}
})
})