Skip to content

Commit

Permalink
fix: Use getErrorLocale to display the right error to the user
Browse files Browse the repository at this point in the history
Since we can have error that have no translations yet or since the
error is technical, we should fallback to the genric error.

This is what is done by the getErrorLocale method and we should
always use it to display error coming from the konnectors.
  • Loading branch information
Crash-- committed Jun 2, 2023
1 parent 7299eea commit b689dda
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/cozy-harvest-lib/src/helpers/konnectorBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import get from 'lodash/get'
import { triggers } from 'cozy-client/dist/models/trigger'
import { generateUniversalLink } from 'cozy-ui/transpiled/react/AppLinker'

import { getErrorLocale } from './konnectors'
import { hasNewVersionAvailable } from './konnectors'
import {
buildKonnectorQuery,
Expand Down Expand Up @@ -157,7 +158,7 @@ const statusToFormatOptions = {
},
iconStatus: 'disabled',
message: {
text: t(`error.job.${error.message}.title`),
text: getErrorLocale(error, konnector, t, 'title'),
color
}
})
Expand Down
49 changes: 43 additions & 6 deletions packages/cozy-harvest-lib/src/helpers/konnectorBlock.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMockClient } from 'cozy-client'
import { I18n } from 'cozy-ui/react/I18n'

import konnectorBlock from './konnectorBlock'

Expand All @@ -8,10 +9,18 @@ client.fetchJSON = jest.fn()
client.query = jest.fn()
client.getQueryFromState = jest.fn()

const I18nComponent = new I18n({
lang: 'en',
dictRequire: lang => require(`../locales/${lang}`)
})

const mockT = I18nComponent.getChildContext().t

const setup = async ({
isAccountConnected = true,
isInMaintenance = false,
isInError = { error: null }
isInError = { error: null },
t = x => x
} = {}) => {
jest
.spyOn(konnectorBlock, 'isAccountConnected')
Expand All @@ -23,7 +32,7 @@ const setup = async ({

return await konnectorBlock.fetchKonnectorData({
client,
t: jest.fn(x => x),
t: t,
slug: 'pajemploi',
sourceAccount: '012345'
})
Expand Down Expand Up @@ -136,7 +145,9 @@ describe('fetchKonnectorData', () => {

it('should return appropriate response if konnector has actionable error', async () => {
const res = await setup({
isInError: { error: { message: 'errorMessage', isActionable: true } }
isInError: {
error: { message: 'error.job.UNKNOWN_ERROR.title', isActionable: true }
}
})

expect(res).toMatchObject({
Expand All @@ -148,13 +159,15 @@ describe('fetchKonnectorData', () => {
target: '_blank'
},
iconStatus: 'disabled',
message: { text: 'error.job.errorMessage.title', color: 'error' }
message: { text: 'error.job.UNKNOWN_ERROR.title', color: 'error' }
})
})

it('should return appropriate response if konnector has not actionable error', async () => {
const res = await setup({
isInError: { error: { message: 'errorMessage', isActionable: false } }
isInError: {
error: { message: 'error.job.UNKNOWN_ERROR.title', isActionable: false }
}
})

expect(res).toMatchObject({
Expand All @@ -166,7 +179,31 @@ describe('fetchKonnectorData', () => {
target: '_blank'
},
iconStatus: 'disabled',
message: { text: 'error.job.errorMessage.title', color: 'textSecondary' }
message: { text: 'error.job.UNKNOWN_ERROR.title', color: 'textSecondary' }
})
})

it('should return generic error if the error doesnt exist', async () => {
const res = await setup({
isInError: {
error: { message: 'error.job.IDONTEXIST.title', isActionable: false }
},
t: mockT
})

expect(res).toMatchObject({
name: 'Pajemploi',
link: 'https://links.mycozy.cloud/home/connected/pajemploi?fallback=http%3A%2F%2Fcozy-home.tools%3A8080%2F%23%2Fconnected%2Fpajemploi',
vendorLink: {
component: 'a',
href: 'https://www.pajemploi.urssaf.fr/',
target: '_blank'
},
iconStatus: 'disabled',
message: {
text: mockT('error.job.UNKNOWN_ERROR.title'),
color: 'textSecondary'
}
})
})

Expand Down

0 comments on commit b689dda

Please sign in to comment.