From 6cbd916cca26372759db7553c5164199002e82d7 Mon Sep 17 00:00:00 2001 From: thoreyjona Date: Wed, 22 Jan 2025 11:22:53 +0000 Subject: [PATCH 1/4] feat: add bad session error --- apps/native/app/src/messages/en.ts | 2 ++ apps/native/app/src/messages/is.ts | 2 ++ .../app/src/screens/wallet-pass/wallet-pass.tsx | 2 +- apps/native/app/src/ui/lib/card/license-card.tsx | 15 +++++++++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/apps/native/app/src/messages/en.ts b/apps/native/app/src/messages/en.ts index fb99a50f24e9..4ec4a0d61ec2 100644 --- a/apps/native/app/src/messages/en.ts +++ b/apps/native/app/src/messages/en.ts @@ -270,6 +270,8 @@ export const en: TranslatedMessages = { 'walletPass.barcodeErrorNotConnected': 'Not possible to scan barcode if the device is not connected to the internet.', 'walletPass.barcodeErrorFailedToFetch': 'Could not fetch barcode', + 'walletPass.barcodeErrorBadSession': + 'Too soon since the license was accessed on another device', 'walletPass.validLicense': 'Valid', 'walletPass.expiredLicense': 'Expired', 'walletPass.passportNumber': 'Passport number: {licenseNumber}', diff --git a/apps/native/app/src/messages/is.ts b/apps/native/app/src/messages/is.ts index 85f6c6f64af1..a54817ba64b0 100644 --- a/apps/native/app/src/messages/is.ts +++ b/apps/native/app/src/messages/is.ts @@ -406,6 +406,8 @@ export const is = { 'walletPass.barcodeErrorNotConnected': 'Ekki er hægt að skanna skírteini nema að tækið sé nettengt.', 'walletPass.barcodeErrorFailedToFetch': 'Ekki tókst að sækja barkóða', + 'walletPass.barcodeErrorBadSession': + 'Of stutt síðan skírteini var sótt á öðru tæki', 'walletPass.validLicense': 'Í gildi', 'walletPass.expiredLicense': 'Útrunnið', 'walletPass.passportNumber': 'Númer vegabréfs: {licenseNumber}', diff --git a/apps/native/app/src/screens/wallet-pass/wallet-pass.tsx b/apps/native/app/src/screens/wallet-pass/wallet-pass.tsx index 87c8758de9a0..5609fa3e653d 100644 --- a/apps/native/app/src/screens/wallet-pass/wallet-pass.tsx +++ b/apps/native/app/src/screens/wallet-pass/wallet-pass.tsx @@ -401,7 +401,7 @@ export const WalletPassScreen: NavigationFunctionComponent<{ type={licenseType} title={data?.payload?.metadata?.name ?? undefined} loading={res.loading} - error={!!res.error} + error={res.error} logo={ isBarcodeEnabled && data?.license?.type === GenericLicenseType.DriversLicense diff --git a/apps/native/app/src/ui/lib/card/license-card.tsx b/apps/native/app/src/ui/lib/card/license-card.tsx index 0c7084ee1108..38cf2649ca42 100644 --- a/apps/native/app/src/ui/lib/card/license-card.tsx +++ b/apps/native/app/src/ui/lib/card/license-card.tsx @@ -8,6 +8,7 @@ import { ViewStyle, } from 'react-native' import styled, { useTheme } from 'styled-components/native' +import { ApolloError } from '@apollo/client' import { Barcode } from '../barcode/barcode' import { Skeleton } from '../skeleton/skeleton' @@ -22,6 +23,10 @@ import { dynamicColor } from '../../utils/dynamic-color' import { Typography } from '../typography/typography' import { screenWidth } from '../../../utils/dimensions' import { BARCODE_MAX_WIDTH } from '../../../screens/wallet-pass/wallet-pass.constants' +import { + findProblemInApolloError, + ProblemType, +} from '@island.is/shared/problem' const Host = styled(Animated.View)` position: relative; @@ -122,7 +127,7 @@ interface LicenseCardProps { backgroundColor?: string showBarcodeOfflineMessage?: boolean loading?: boolean - error?: boolean + error?: ApolloError barcode?: { value?: string | null loading?: boolean @@ -166,6 +171,10 @@ export function LicenseCard({ : screenWidth - theme.spacing[4] * 2 - theme.spacing.smallGutter * 2 const barcodeHeight = barcodeWidth / 3 + const badSessionError = error + ? findProblemInApolloError(error as any, [ProblemType.BAD_SESSION]) + : undefined + return ( {intl.formatMessage({ id: error - ? 'walletPass.barcodeErrorFailedToFetch' + ? badSessionError + ? 'walletPass.barcodeErrorBadSession' + : 'walletPass.barcodeErrorFailedToFetch' : 'walletPass.barcodeErrorNotConnected', })} From f483cdd78b36f19d3070f990b808b3bf9a4d1e78 Mon Sep 17 00:00:00 2001 From: thoreyjona Date: Wed, 22 Jan 2025 20:06:44 +0000 Subject: [PATCH 2/4] fix: update english translation --- apps/native/app/src/messages/en.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/native/app/src/messages/en.ts b/apps/native/app/src/messages/en.ts index 4ec4a0d61ec2..a9532c767412 100644 --- a/apps/native/app/src/messages/en.ts +++ b/apps/native/app/src/messages/en.ts @@ -271,7 +271,7 @@ export const en: TranslatedMessages = { 'Not possible to scan barcode if the device is not connected to the internet.', 'walletPass.barcodeErrorFailedToFetch': 'Could not fetch barcode', 'walletPass.barcodeErrorBadSession': - 'Too soon since the license was accessed on another device', + 'Too little time since license was accessed on another device', 'walletPass.validLicense': 'Valid', 'walletPass.expiredLicense': 'Expired', 'walletPass.passportNumber': 'Passport number: {licenseNumber}', From 7c1db9a20bce963504556afa3bc84540963e706e Mon Sep 17 00:00:00 2001 From: thoreyjona Date: Wed, 22 Jan 2025 19:53:39 +0000 Subject: [PATCH 3/4] fix: update findProblemInApolloError function to match nested graphql errors --- .../utils/findProblemInApolloError.spec.ts | 31 +++++++++++++++++++ .../src/utils/findProblemInApolloError.ts | 10 +++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/libs/shared/problem/src/utils/findProblemInApolloError.spec.ts b/libs/shared/problem/src/utils/findProblemInApolloError.spec.ts index 507c7bcd97d6..44f4affe9b99 100644 --- a/libs/shared/problem/src/utils/findProblemInApolloError.spec.ts +++ b/libs/shared/problem/src/utils/findProblemInApolloError.spec.ts @@ -74,6 +74,32 @@ describe('findProblemInApolloError', () => { expect(problem).toMatchObject({ type: ProblemType.HTTP_NOT_FOUND }) }) + it('return first problem matching types when problem is nested in exception object', () => { + // Arrange + const error = new ApolloError({ + graphQLErrors: [ + new GraphQLError('Error', null, null, null, null, null, { + exception: { + problem: { type: ProblemType.HTTP_BAD_REQUEST }, + }, + }), + new GraphQLError('Error', null, null, null, null, null, { + exception: { + problem: { type: ProblemType.HTTP_NOT_FOUND }, + }, + }), + ], + }) + + // Act + const problem = findProblemInApolloError(error, [ + ProblemType.HTTP_NOT_FOUND, + ]) + + // Assert + expect(problem).toMatchObject({ type: ProblemType.HTTP_NOT_FOUND }) + }) + it('return undefined if no problem matches types', () => { // Arrange const error = new ApolloError({ @@ -81,6 +107,11 @@ describe('findProblemInApolloError', () => { new GraphQLError('Error', null, null, null, null, null, { problem: { type: ProblemType.HTTP_BAD_REQUEST }, }), + new GraphQLError('Error', null, null, null, null, null, { + exception: { + problem: { type: ProblemType.HTTP_BAD_REQUEST }, + }, + }), ], }) diff --git a/libs/shared/problem/src/utils/findProblemInApolloError.ts b/libs/shared/problem/src/utils/findProblemInApolloError.ts index 17fd6106c335..5afb01a2a3a8 100644 --- a/libs/shared/problem/src/utils/findProblemInApolloError.ts +++ b/libs/shared/problem/src/utils/findProblemInApolloError.ts @@ -1,6 +1,13 @@ import type { ApolloError } from '@apollo/client' import { Problem } from '../Problem' +interface ProblemExtensions { + problem?: Problem + exception?: { + problem?: Problem + } +} + export const findProblemInApolloError = ( error: ApolloError | undefined, types?: string[], @@ -9,7 +16,8 @@ export const findProblemInApolloError = ( return undefined } const graphQLError = error.graphQLErrors.find((value) => { - const problem = value.extensions?.problem as Problem | undefined + const extensions = value.extensions as ProblemExtensions | undefined + const problem = extensions?.problem || extensions?.exception?.problem return problem && (!types || types.includes(problem.type)) }) From e8f6769e22f5cbae287e584be3fcbb30c32a6bde Mon Sep 17 00:00:00 2001 From: Eirikur Nilsson Date: Fri, 24 Jan 2025 10:10:06 +0000 Subject: [PATCH 4/4] fix: Return problem nested under exception --- .../src/utils/findProblemInApolloError.ts | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libs/shared/problem/src/utils/findProblemInApolloError.ts b/libs/shared/problem/src/utils/findProblemInApolloError.ts index 5afb01a2a3a8..997f3fa16460 100644 --- a/libs/shared/problem/src/utils/findProblemInApolloError.ts +++ b/libs/shared/problem/src/utils/findProblemInApolloError.ts @@ -15,16 +15,20 @@ export const findProblemInApolloError = ( if (!error) { return undefined } - const graphQLError = error.graphQLErrors.find((value) => { - const extensions = value.extensions as ProblemExtensions | undefined - const problem = extensions?.problem || extensions?.exception?.problem - return problem && (!types || types.includes(problem.type)) - }) - if (!graphQLError) { + const problems = error.graphQLErrors + .map((value) => { + const extensions = value.extensions as ProblemExtensions | undefined + const problem = extensions?.problem || extensions?.exception?.problem + if (problem && (!types || types.includes(problem.type))) { + return problem + } + }) + .filter((problem) => problem) as Problem[] + + if (problems.length === 0) { return undefined } - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return graphQLError.extensions!.problem as Problem + return problems[0] }