From efbef765c03c1fa1ddb7199e8e04a26dc4de71b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnea=20R=C3=BAn=20Vignisd=C3=B3ttir?= Date: Mon, 30 Sep 2024 14:06:01 +0000 Subject: [PATCH] fix(auth-admin): Show relevant error messages (#16180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * show propper error messages * chore: nx format:write update dirty files * return error message and code, use code to translate error on client * chore: nx format:write update dirty files * Fix typos * Explicitly type FORM_ERRORS --------- Co-authored-by: andes-it Co-authored-by: Sævar Már Atlason Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- .../admin/delegation-admin-custom.service.ts | 41 ++++++++++++------- .../delegation-admin/src/constants/errors.ts | 14 +++++++ .../delegation-admin/src/lib/messages.ts | 26 ++++++++++++ .../CreateDelegation.action.ts | 9 +++- .../CreateDelegation/CreateDelegation.tsx | 16 +++++++- libs/shared/utils/src/index.ts | 1 + libs/shared/utils/src/lib/errorCodes.ts | 8 ++++ 7 files changed, 97 insertions(+), 18 deletions(-) create mode 100644 libs/portals/admin/delegation-admin/src/constants/errors.ts create mode 100644 libs/shared/utils/src/lib/errorCodes.ts diff --git a/libs/auth-api-lib/src/lib/delegations/admin/delegation-admin-custom.service.ts b/libs/auth-api-lib/src/lib/delegations/admin/delegation-admin-custom.service.ts index 04403fecda27..99f41074d6e3 100644 --- a/libs/auth-api-lib/src/lib/delegations/admin/delegation-admin-custom.service.ts +++ b/libs/auth-api-lib/src/lib/delegations/admin/delegation-admin-custom.service.ts @@ -1,4 +1,4 @@ -import { BadRequestException, Injectable } from '@nestjs/common' +import { BadRequestException, HttpStatus, Injectable } from '@nestjs/common' import { InjectModel } from '@nestjs/sequelize' import { Sequelize } from 'sequelize-typescript' import kennitala from 'kennitala' @@ -28,6 +28,7 @@ import { DELEGATION_TAG, ZENDESK_CUSTOM_FIELDS } from '../constants/zendesk' import { DelegationDelegationType } from '../models/delegation-delegation-type.model' import { DelegationsIncomingCustomService } from '../delegations-incoming-custom.service' import { DelegationValidity } from '../types/delegationValidity' +import { ErrorCodes } from '@island.is/shared/utils' @Injectable() export class DelegationAdminCustomService { @@ -57,9 +58,10 @@ export class DelegationAdminCustomService { ) if (!fromReferenceId || !toReferenceId) { - throw new BadRequestException( - 'Zendesk ticket is missing required custom fields', - ) + throw new BadRequestException({ + message: 'Zendesk ticket is missing required custom fields', + error: ErrorCodes.ZENDESK_CUSTOM_FIELDS_MISSING, + }) } return { @@ -155,11 +157,17 @@ export class DelegationAdminCustomService { ) if (!zendeskCase.tags.includes(DELEGATION_TAG)) { - throw new BadRequestException('Zendesk ticket is missing required tag') + throw new BadRequestException({ + message: 'Zendesk case is missing required tag', + error: ErrorCodes.ZENDESK_TAG_MISSING, + }) } if (zendeskCase.status !== TicketStatus.Solved) { - throw new BadRequestException('Zendesk case is not solved') + throw new BadRequestException({ + message: 'Zendesk case is not solved', + error: ErrorCodes.ZENDESK_STATUS, + }) } const { fromReferenceId, toReferenceId } = @@ -169,9 +177,10 @@ export class DelegationAdminCustomService { fromReferenceId !== delegation.fromNationalId || toReferenceId !== delegation.toNationalId ) { - throw new BadRequestException( - 'Zendesk ticket nationalIds does not match delegation nationalIds', - ) + throw new BadRequestException({ + message: 'National Ids do not match the Zendesk ticket', + error: ErrorCodes.ZENDESK_NATIONAL_IDS_MISMATCH, + }) } const [fromDisplayName, toName] = await Promise.all([ @@ -253,17 +262,19 @@ export class DelegationAdminCustomService { fromNationalId: string, ) { if (toNationalId === fromNationalId) { - throw new BadRequestException( - 'Cannot create a delegation between the same nationalId.', - ) + throw new BadRequestException({ + message: 'National Ids cannot be the same', + error: ErrorCodes.INPUT_VALIDATION_SAME_NATIONAL_ID, + }) } if ( !(kennitala.isPerson(fromNationalId) && kennitala.isPerson(toNationalId)) ) { - throw new BadRequestException( - 'National ids needs to be valid person national ids', - ) + throw new BadRequestException({ + message: 'National Ids are not valid', + error: ErrorCodes.INPUT_VALIDATION_INVALID_PERSON, + }) } } diff --git a/libs/portals/admin/delegation-admin/src/constants/errors.ts b/libs/portals/admin/delegation-admin/src/constants/errors.ts new file mode 100644 index 000000000000..373be7f6a2d2 --- /dev/null +++ b/libs/portals/admin/delegation-admin/src/constants/errors.ts @@ -0,0 +1,14 @@ +import { MessageDescriptor } from 'react-intl' + +import { ErrorCodes } from '@island.is/shared/utils' + +import { m } from '../lib/messages' + +export const FORM_ERRORS: Record = { + [ErrorCodes.ZENDESK_NATIONAL_IDS_MISMATCH]: m.nationalIdsMismatchError, + [ErrorCodes.ZENDESK_CUSTOM_FIELDS_MISSING]: m.zendeskCustomFieldsMissingError, + [ErrorCodes.ZENDESK_TAG_MISSING]: m.zendeskMissingTagError, + [ErrorCodes.ZENDESK_STATUS]: m.zendeskCaseNotSolvedError, + [ErrorCodes.INPUT_VALIDATION_SAME_NATIONAL_ID]: m.sameNationalIdError, + [ErrorCodes.INPUT_VALIDATION_INVALID_PERSON]: m.validPersonError, +} diff --git a/libs/portals/admin/delegation-admin/src/lib/messages.ts b/libs/portals/admin/delegation-admin/src/lib/messages.ts index f6377eff0302..1735e3f2b94d 100644 --- a/libs/portals/admin/delegation-admin/src/lib/messages.ts +++ b/libs/portals/admin/delegation-admin/src/lib/messages.ts @@ -121,4 +121,30 @@ export const m = defineMessages({ id: 'admin.delegationAdmin:createDelegationSuccessToast', defaultMessage: 'Umboð var skráð', }, + nationalIdsMismatchError: { + id: 'admin.delegationAdmin:nationalIdsMismatchError', + defaultMessage: + 'Kennitölur á umboði stemma ekki við kennitölur í Zendesk máli', + }, + zendeskCaseNotSolvedError: { + id: 'admin.delegationAdmin:zendeskCaseNotSolvedError', + defaultMessage: 'Zendesk málið er ekki í stöðunni leyst', + }, + zendeskMissingTagError: { + id: 'admin.delegationAdmin:zendeskMissingTagError', + defaultMessage: 'Zendesk málið vantar nauðsynlegt tagg', + }, + zendeskCustomFieldsMissingError: { + id: 'admin.delegationAdmin:zendeskCustomFieldsMissingError', + defaultMessage: + 'Zendesk málið vantar nauðsynlegar upplýsingar um kennitölur umboðsveitanda og umboðshafa', + }, + sameNationalIdError: { + id: 'admin.delegationAdmin:sameNationalIdError', + defaultMessage: 'Kennitölur mega ekki vera eins', + }, + validPersonError: { + id: 'admin.delegationAdmin:validPersonError', + defaultMessage: 'Kennitölur þurfa að vera gildar kennitölur', + }, }) diff --git a/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.action.ts b/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.action.ts index 83aac0a774f7..b84eed25888f 100644 --- a/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.action.ts +++ b/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.action.ts @@ -11,6 +11,11 @@ import { CreateDelegationMutation, CreateDelegationMutationVariables, } from './CreateDelegation.generated' +import { + findProblemInApolloError, + Problem, + ProblemType, +} from '@island.is/shared/problem' const schema = z .object({ @@ -50,6 +55,7 @@ export type CreateDelegationResult = ValidateFormDataResult & { * Global error message if the mutation fails */ globalError?: boolean + problem?: Problem success?: boolean } @@ -89,11 +95,12 @@ export const createDelegationAction: WrappedActionFn = success: true, } } catch (e) { - console.error(e) + const problem = findProblemInApolloError(e) return { errors: null, data: null, globalError: true, + problem, } } } diff --git a/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.tsx b/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.tsx index c7ec091a643f..04c41efecd99 100644 --- a/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.tsx +++ b/libs/portals/admin/delegation-admin/src/screens/CreateDelegation/CreateDelegation.tsx @@ -43,6 +43,7 @@ import kennitala from 'kennitala' import { maskString, unmaskString } from '@island.is/shared/utils' import { useAuth } from '@island.is/auth/react' import { replaceParams } from '@island.is/react-spa/shared' +import { FORM_ERRORS } from '../../constants/errors' const CreateDelegationScreen = () => { const { formatMessage } = useLocale() @@ -402,10 +403,21 @@ const CreateDelegationScreen = () => { /> {actionData?.globalError && ( - + diff --git a/libs/shared/utils/src/index.ts b/libs/shared/utils/src/index.ts index 4e41c0d0c5d5..c0b0c1a77a83 100644 --- a/libs/shared/utils/src/index.ts +++ b/libs/shared/utils/src/index.ts @@ -23,3 +23,4 @@ export * from './lib/shouldLinkBeAnAnchorTag' export * from './lib/videoEmbed' export * from './lib/web' export * from './lib/postalCodes' +export * from './lib/errorCodes' diff --git a/libs/shared/utils/src/lib/errorCodes.ts b/libs/shared/utils/src/lib/errorCodes.ts new file mode 100644 index 000000000000..d0ec30392c07 --- /dev/null +++ b/libs/shared/utils/src/lib/errorCodes.ts @@ -0,0 +1,8 @@ +export enum ErrorCodes { + ZENDESK_NATIONAL_IDS_MISMATCH = 'ZENDESK_NATIONAL_IDS_MISMATCH', + ZENDESK_CUSTOM_FIELDS_MISSING = 'ZENDESK_CUSTOM_FIELDS_MISSING', + ZENDESK_TAG_MISSING = 'ZENDESK_TAG_MISSING', + ZENDESK_STATUS = 'ZENDESK_STATUS', + INPUT_VALIDATION_SAME_NATIONAL_ID = 'INPUT_VALIDATION_SAME_NATIONAL_ID', + INPUT_VALIDATION_INVALID_PERSON = 'INPUT_VALIDATION_INVALID_PERSON', +}