Skip to content

Commit

Permalink
fix(auth-admin): Show relevant error messages (#16180)
Browse files Browse the repository at this point in the history
* 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 <builders@andes.is>
Co-authored-by: Sævar Már Atlason <saevar.m.atlason@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 30, 2024
1 parent c590e28 commit efbef76
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 } =
Expand All @@ -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([
Expand Down Expand Up @@ -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,
})
}
}

Expand Down
14 changes: 14 additions & 0 deletions libs/portals/admin/delegation-admin/src/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -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, MessageDescriptor> = {
[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,
}
26 changes: 26 additions & 0 deletions libs/portals/admin/delegation-admin/src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
CreateDelegationMutation,
CreateDelegationMutationVariables,
} from './CreateDelegation.generated'
import {
findProblemInApolloError,
Problem,
ProblemType,
} from '@island.is/shared/problem'

const schema = z
.object({
Expand Down Expand Up @@ -50,6 +55,7 @@ export type CreateDelegationResult = ValidateFormDataResult<typeof schema> & {
* Global error message if the mutation fails
*/
globalError?: boolean
problem?: Problem
success?: boolean
}

Expand Down Expand Up @@ -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,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -402,10 +403,21 @@ const CreateDelegationScreen = () => {
/>
</GridColumn>
{actionData?.globalError && (
<GridColumn span={['12/12']}>
<GridColumn span={['12/12', '12/12', '7/12']}>
<AlertMessage
title=""
message={formatMessage(m.errorDefault)}
message={
// if problem title is object extract code and use it as key
actionData?.problem?.title
? formatMessage(
FORM_ERRORS[
actionData?.problem
?.title as keyof typeof FORM_ERRORS
],
)
: actionData?.problem?.detail ||
formatMessage(m.errorDefault)
}
type="error"
/>
</GridColumn>
Expand Down
1 change: 1 addition & 0 deletions libs/shared/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ export * from './lib/shouldLinkBeAnAnchorTag'
export * from './lib/videoEmbed'
export * from './lib/web'
export * from './lib/postalCodes'
export * from './lib/errorCodes'
8 changes: 8 additions & 0 deletions libs/shared/utils/src/lib/errorCodes.ts
Original file line number Diff line number Diff line change
@@ -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',
}

0 comments on commit efbef76

Please sign in to comment.