Skip to content

Commit ebeb86f

Browse files
fix: Handle segments ProviderError (#3852)
1 parent 8267885 commit ebeb86f

File tree

9 files changed

+255
-31
lines changed

9 files changed

+255
-31
lines changed

src/pages/CommitDetailPage/CommitCoverage/routes/FilesChangedTab/shared/CommitFileDiff/CommitFileDiff.test.tsx

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Suspense } from 'react'
1111
import { MemoryRouter, Route } from 'react-router-dom'
1212
import { type MockInstance } from 'vitest'
1313

14-
import { ImpactedFileType } from 'services/comparison/useComparisonForCommitAndParent'
14+
import { ImpactedFileWithSegmentsUnionType } from 'services/comparison/useComparisonForCommitAndParent'
1515

1616
import CommitFileDiff from './CommitFileDiff'
1717

@@ -75,7 +75,7 @@ class ResizeObserverMock {
7575
}
7676
global.window.ResizeObserver = ResizeObserverMock
7777

78-
const baseMock = (impactedFile: ImpactedFileType | null) => {
78+
const baseMock = (impactedFile: ImpactedFileWithSegmentsUnionType | null) => {
7979
if (!impactedFile) {
8080
return { owner: null }
8181
}
@@ -97,7 +97,7 @@ const baseMock = (impactedFile: ImpactedFileType | null) => {
9797
}
9898
}
9999

100-
const mockImpactedFile = {
100+
const mockImpactedFile: ImpactedFileWithSegmentsUnionType = {
101101
headName: 'flag1/file.js',
102102
hashedPath: 'hashedFilePath',
103103
isNewFile: false,
@@ -114,6 +114,7 @@ const mockImpactedFile = {
114114
},
115115
changeCoverage: 0,
116116
segments: {
117+
__typename: 'SegmentComparisons',
117118
results: [
118119
{
119120
header: '-0,0 +1,45',
@@ -226,7 +227,7 @@ afterAll(() => {
226227
})
227228

228229
interface SetupArgs {
229-
impactedFile?: ImpactedFileType | null
230+
impactedFile?: ImpactedFileWithSegmentsUnionType | null
230231
bundleAnalysisEnabled?: boolean
231232
}
232233

@@ -378,6 +379,46 @@ describe('CommitFileDiff', () => {
378379
})
379380
})
380381

382+
describe('when segments union type returned error', () => {
383+
describe('when provider error', () => {
384+
it('renders a error display message', async () => {
385+
const impactedFileWithProviderError = {
386+
...mockImpactedFile,
387+
segments: {
388+
__typename: 'ProviderError',
389+
message: 'Error fetching data from the provider',
390+
},
391+
} as ImpactedFileWithSegmentsUnionType
392+
setup({ impactedFile: impactedFileWithProviderError })
393+
render(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
394+
395+
const errorMessage = await screen.findByText(
396+
/There was a problem getting the source code from your provider. Unable to show line by line coverage/i
397+
)
398+
expect(errorMessage).toBeInTheDocument()
399+
})
400+
})
401+
402+
describe('when path error', () => {
403+
it('renders a error display message for path error', async () => {
404+
const impactedFileWithPathError = {
405+
...mockImpactedFile,
406+
segments: {
407+
__typename: 'UnknownPath',
408+
message: 'Unknown path',
409+
},
410+
} as ImpactedFileWithSegmentsUnionType
411+
setup({ impactedFile: impactedFileWithPathError })
412+
render(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
413+
414+
const errorMessage = await screen.findByText(
415+
/There was a problem getting the source code from your provider by path for/i
416+
)
417+
expect(errorMessage).toBeInTheDocument()
418+
})
419+
})
420+
})
421+
381422
describe('code renderer', () => {
382423
it('renders text area', async () => {
383424
setup({})
@@ -430,7 +471,10 @@ describe('CommitFileDiff', () => {
430471
...mockImpactedFile,
431472

432473
headName: 'flag1/file.js',
433-
segments: { results: [] },
474+
segments: {
475+
__typename: 'SegmentComparisons',
476+
results: [],
477+
} as ImpactedFileWithSegmentsUnionType['segments'],
434478
}
435479

436480
it('does not render information on the code renderer', async () => {

src/pages/CommitDetailPage/CommitCoverage/routes/FilesChangedTab/shared/CommitFileDiff/CommitFileDiff.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,37 @@ function ErrorDisplayMessage() {
147147
)
148148
}
149149

150+
function UnknownPathErrorDisplayMessage({ path }: { path: string }) {
151+
const location = useLocation()
152+
153+
return (
154+
<p className="border border-solid border-ds-gray-tertiary p-4">
155+
There was a problem getting the source code from your provider by path
156+
for: <strong>{path}</strong>. Unable to show line by line coverage.
157+
<br />
158+
<span>
159+
If you continue to experience this issue, please try{' '}
160+
<A
161+
to={{ pageName: 'login', options: { to: location.pathname } }}
162+
hook={undefined}
163+
isExternal={undefined}
164+
>
165+
logging in
166+
</A>{' '}
167+
again to refresh your credentials. Otherwise, please visit our{' '}
168+
<A
169+
to={{ pageName: 'pathFixing', options: { to: location.pathname } }}
170+
hook={undefined}
171+
isExternal={undefined}
172+
>
173+
Path Fixing
174+
</A>{' '}
175+
documentation for troubleshooting tips.
176+
</span>
177+
</p>
178+
)
179+
}
180+
150181
interface URLParams {
151182
provider: string
152183
owner: string
@@ -171,11 +202,28 @@ function CommitFileDiff({ path }: CommitFileDiffProps) {
171202
opts: { enabled: !!path },
172203
})
173204

174-
if (!comparisonData || !comparisonData?.impactedFile || !path) {
205+
const segments = comparisonData?.impactedFile?.segments
206+
207+
if (
208+
!comparisonData ||
209+
!comparisonData?.impactedFile ||
210+
!path ||
211+
!segments ||
212+
segments?.__typename === 'ProviderError'
213+
) {
175214
return <ErrorDisplayMessage />
176215
}
177216

178-
return <DiffRenderer impactedFile={comparisonData.impactedFile} path={path} />
217+
if (segments?.__typename === 'UnknownPath') {
218+
return <UnknownPathErrorDisplayMessage path={path} />
219+
}
220+
221+
const impactedFileWithSegments = {
222+
...comparisonData.impactedFile,
223+
segments,
224+
}
225+
226+
return <DiffRenderer impactedFile={impactedFileWithSegments} path={path} />
179227
}
180228

181229
export default CommitFileDiff

src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const mockImpactedFile = {
115115
},
116116
changeCoverage: 0,
117117
segments: {
118+
__typename: 'SegmentComparisons',
118119
results: [
119120
{
120121
header: '-0,0 +1,45',
@@ -367,6 +368,46 @@ describe('CommitFileDiff', () => {
367368
})
368369
})
369370

371+
describe('when segments union type returned error', () => {
372+
describe('when provider error', () => {
373+
it('renders a error display message', async () => {
374+
const impactedFileWithProviderError = {
375+
...mockImpactedFile,
376+
segments: {
377+
__typename: 'ProviderError',
378+
message: 'Error fetching data from the provider',
379+
},
380+
}
381+
setup({ impactedFile: impactedFileWithProviderError })
382+
render(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
383+
384+
const errorMessage = await screen.findByText(
385+
/There was a problem getting the source code from your provider. Unable to show line by line coverage/i
386+
)
387+
expect(errorMessage).toBeInTheDocument()
388+
})
389+
})
390+
391+
describe('when path error', () => {
392+
it('renders a error display message for path error', async () => {
393+
const impactedFileWithPathError = {
394+
...mockImpactedFile,
395+
segments: {
396+
__typename: 'UnknownPath',
397+
message: 'Unknown path',
398+
},
399+
}
400+
setup({ impactedFile: impactedFileWithPathError })
401+
render(<CommitFileDiff path={'flag1/file.js'} />, { wrapper })
402+
403+
const errorMessage = await screen.findByText(
404+
/There was a problem getting the source code from your provider by path for/i
405+
)
406+
expect(errorMessage).toBeInTheDocument()
407+
})
408+
})
409+
})
410+
370411
describe('code renderer', () => {
371412
it('renders the text area', async () => {
372413
setup({})

src/pages/CommitDetailPage/CommitCoverage/routes/IndirectChangesTab/IndirectChangesTable/CommitFileDiff/CommitFileDiff.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,37 @@ function ErrorDisplayMessage() {
145145
)
146146
}
147147

148+
function UnknownPathErrorDisplayMessage({ path }: { path: string }) {
149+
const location = useLocation()
150+
151+
return (
152+
<p className="border border-solid border-ds-gray-tertiary p-4">
153+
There was a problem getting the source code from your provider by path
154+
for: <strong>{path}</strong>. Unable to show line by line coverage.
155+
<br />
156+
<span>
157+
If you continue to experience this issue, please try{' '}
158+
<A
159+
to={{ pageName: 'login', options: { to: location.pathname } }}
160+
hook={undefined}
161+
isExternal={undefined}
162+
>
163+
logging in
164+
</A>{' '}
165+
again to refresh your credentials. Otherwise, please visit our{' '}
166+
<A
167+
to={{ pageName: 'pathFixing', options: { to: location.pathname } }}
168+
hook={undefined}
169+
isExternal={undefined}
170+
>
171+
Path Fixing
172+
</A>{' '}
173+
documentation for troubleshooting tips.
174+
</span>
175+
</p>
176+
)
177+
}
178+
148179
interface URLParams {
149180
provider: string
150181
owner: string
@@ -170,11 +201,28 @@ function CommitFileDiff({ path }: CommitFileDiffProps) {
170201
},
171202
})
172203

173-
if (!comparisonData || !comparisonData?.impactedFile || !path) {
204+
const segments = comparisonData?.impactedFile?.segments
205+
206+
if (
207+
!comparisonData ||
208+
!comparisonData?.impactedFile ||
209+
!path ||
210+
!segments ||
211+
segments?.__typename === 'ProviderError'
212+
) {
174213
return <ErrorDisplayMessage />
175214
}
176215

177-
return <DiffRenderer impactedFile={comparisonData.impactedFile} path={path} />
216+
if (segments?.__typename === 'UnknownPath') {
217+
return <UnknownPathErrorDisplayMessage path={path} />
218+
}
219+
220+
const impactedFileWithSegments = {
221+
...comparisonData.impactedFile,
222+
segments,
223+
}
224+
225+
return <DiffRenderer impactedFile={impactedFileWithSegments} path={path} />
178226
}
179227

180228
CommitFileDiff.propTypes = {

src/services/comparison/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fragment ComparisonFragment on Comparison {
2121
changeCoverage
2222
segments (filters: $filters) {
2323
... on SegmentComparisons {
24+
__typename
2425
results {
2526
header
2627
hasUnintendedChanges
@@ -37,6 +38,14 @@ fragment ComparisonFragment on Comparison {
3738
}
3839
}
3940
}
41+
... on ProviderError {
42+
__typename
43+
message
44+
}
45+
... on UnknownPath {
46+
__typename
47+
message
48+
}
4049
}
4150
}
4251
}

src/services/comparison/useComparisonForCommitAndParent/useComparisonForCommitAndParent.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const mockImpactedFile = {
2929
},
3030
changeCoverage: 0,
3131
segments: {
32+
__typename: 'SegmentComparisons',
3233
results: [
3334
{
3435
header: '-0,0 +1,45',
@@ -193,6 +194,7 @@ describe('useComparisonForCommitAndParent', () => {
193194
coverage: 100,
194195
},
195196
segments: {
197+
__typename: 'SegmentComparisons',
196198
results: [
197199
{
198200
hasUnintendedChanges: false,

0 commit comments

Comments
 (0)