Skip to content

Commit

Permalink
[UXE-4871] feat: change events name and parse error message correctly…
Browse files Browse the repository at this point in the history
… in the services (#1762)
  • Loading branch information
aloisio-m-bastian authored Oct 3, 2024
1 parent cdd831e commit 09271a5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ const adapt = (payload) => {
}
}

/**
* @param {Object} body - The response body.
* @returns {string} The result message based on the status code.
*/
const extractApiError = (body) => {
for (const keyError of Object.keys(body)) {
const errorValue = Array.isArray(body[keyError]) ? body[keyError][0] : body[keyError]
if (typeof errorValue === 'string') return errorValue
if (typeof errorValue === 'object' && errorValue.message) return errorValue.message[0]
}
return ''
}

/**
* @param {Object} httpResponse - The HTTP response object.
* @param {Object} httpResponse.body - The response body.
Expand All @@ -36,7 +49,7 @@ const parseHttpResponse = (httpResponse) => {
urlToEditView: `/network-lists/edit/${httpResponse.body.results.id}`
}
case 400:
const apiError = httpResponse.body.results[0]
const apiError = extractApiError(httpResponse.body)
throw new Error(apiError).message
case 401:
throw new Errors.InvalidApiTokenError().message
Expand Down
15 changes: 14 additions & 1 deletion src/services/network-lists-services/edit-network-list-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const adapt = (payload) => {
}
}

/**
* @param {Object} body - The response body.
* @returns {string} The result message based on the status code.
*/
const extractApiError = (body) => {
for (const keyError of Object.keys(body)) {
const errorValue = Array.isArray(body[keyError]) ? body[keyError][0] : body[keyError]
if (typeof errorValue === 'string') return errorValue
if (typeof errorValue === 'object' && errorValue.message) return errorValue.message[0]
}
return ''
}

/**
* @param {Object} httpResponse - The HTTP response object.
* @param {Object} httpResponse.body - The response body.
Expand All @@ -39,7 +52,7 @@ const parseHttpResponse = (httpResponse) => {
case 202:
return 'Your network list has been edited'
case 400:
const apiError = httpResponse.body.results[0]
const apiError = extractApiError(httpResponse.body)
throw new Error(apiError).message
case 401:
throw new Errors.InvalidApiTokenError().message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('NetworkListsServices', () => {
expect(data.feedback).toBe('Your network list has been created')
})

it('Should return an API error for an 40 error status', async () => {
it('Should return an API error for an 400 error status with a array of errors', async () => {
const apiErrorMock = 'Network name "IP" is already in use on this account'
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({
statusCode: 400,
Expand All @@ -75,6 +75,21 @@ describe('NetworkListsServices', () => {
expect(feedbackMessage).rejects.toThrow(apiErrorMock)
})

it('Should return an API error for an 400 error status with a single string error', async () => {
const apiErrorMock = 'Network name "IP" is already in use on this account'
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({
statusCode: 400,
body: {
error: apiErrorMock
}
})
const { sut } = makeSut()

const feedbackMessage = sut(fixtures.networkMock)

expect(feedbackMessage).rejects.toThrow(apiErrorMock)
})

it.each([
{
statusCode: 401,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('NetworkListsServices', () => {
expect(feedbackMessage).toBe('Your network list has been edited')
})

it('Should return an API error for an 400 error status', async () => {
it('Should return an API error for an 400 error status with a array of errors', async () => {
const apiErrorMock = 'Network name "IP" is already in use on this account'
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({
statusCode: 400,
Expand All @@ -66,6 +66,21 @@ describe('NetworkListsServices', () => {
expect(feedbackMessage).rejects.toThrow(apiErrorMock)
})

it('Should return an API error for an 400 error status with a single string error', async () => {
const apiErrorMock = 'Network name "IP" is already in use on this account'
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({
statusCode: 400,
body: {
error: apiErrorMock
}
})
const { sut } = makeSut()

const feedbackMessage = sut(fixtures.networkMock)

expect(feedbackMessage).rejects.toThrow(apiErrorMock)
})

it.each([
{
statusCode: 401,
Expand Down
4 changes: 2 additions & 2 deletions src/views/NetworkLists/CreateView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@
const handleTrackCreation = () => {
tracker.product.productCreated({
productName: 'Network Lists'
productName: 'Network List'
})
}
const handleTrackFailedCreation = (error) => {
const { fieldName, message } = handleTrackerError(error)
tracker.product
.failedToCreate({
productName: 'Network Lists',
productName: 'Network List',
errorType: 'api',
fieldName: fieldName.trim(),
errorMessage: message
Expand Down
4 changes: 2 additions & 2 deletions src/views/NetworkLists/EditView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@
const handleTrackSuccessEdit = () => {
tracker.product
.productEdited({
productName: 'Network Lists'
productName: 'Network List'
})
.track()
}
const handleTrackFailEdit = (error) => {
const { fieldName, message } = handleTrackerError(error)
tracker.product
.failedToEdit({
productName: 'Network Lists',
productName: 'Network List',
errorType: 'api',
fieldName: fieldName.trim(),
errorMessage: message
Expand Down
4 changes: 2 additions & 2 deletions src/views/NetworkLists/ListView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@
const handleCreateTrackEvent = () => {
tracker.product.clickToCreate({
productName: 'Network Lists'
productName: 'Network List'
})
}
const handleTrackEditEvent = () => {
tracker.product.clickToEdit({
productName: 'Network Lists'
productName: 'Network List'
})
}
Expand Down

0 comments on commit 09271a5

Please sign in to comment.