Skip to content

Commit 9cf3cac

Browse files
committed
fix: add better error handling
1 parent 34b111f commit 9cf3cac

File tree

2 files changed

+25
-14
lines changed
  • packages/backend/src/apps/paysg/actions/create-payment-form-submission

2 files changed

+25
-14
lines changed

packages/backend/src/apps/paysg/actions/create-payment-form-submission/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { IRawAction } from '@plumber/types'
33
import { ZodError } from 'zod'
44
import { fromZodError } from 'zod-validation-error'
55

6-
import StepError, { GenericSolution } from '@/errors/step'
6+
import StepError from '@/errors/step'
77

88
import { requestSchema } from './schema'
99

@@ -79,14 +79,14 @@ const action: IRawAction = {
7979
required: false,
8080
subFields: [
8181
{
82-
label: 'Question',
82+
placeholder: 'Question',
8383
key: 'question',
8484
type: 'string' as const,
8585
required: true,
8686
variables: true,
8787
},
8888
{
89-
label: 'Answer',
89+
placeholder: 'Answer',
9090
key: 'answer',
9191
type: 'string' as const,
9292
required: true,
@@ -119,7 +119,7 @@ const action: IRawAction = {
119119

120120
throw new StepError(
121121
`${firstError.message} at "${firstError.path}"`,
122-
GenericSolution.ReconfigureInvalidField,
122+
`${firstError.message} under set up step`,
123123
$.step.position,
124124
$.app.name,
125125
)

packages/backend/src/apps/paysg/actions/create-payment-form-submission/schema.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const PAYMENT_FORM_LINK_REGEX = /^https:\/\/(staging.)?pay.gov.sg\/forms\/(.*)$/
99

1010
export const requestSchema = z.object({
1111
formId: z
12-
.string()
12+
.string({ invalid_type_error: 'Empty payment form link' })
1313
.trim()
14-
.min(1, { message: 'Specify a form link' })
14+
.min(1, { message: 'Specify a payment form link' })
1515
.transform((value, context) => {
1616
const match = value.match(PAYMENT_FORM_LINK_REGEX)
1717

@@ -26,16 +26,19 @@ export const requestSchema = z.object({
2626
return match[2]
2727
}),
2828
formsg_form_id: z
29-
.string()
29+
.string({ invalid_type_error: 'Empty FormSG form ID' })
3030
.trim()
3131
.length(FORM_ID_LENGTH, { message: 'Specify a valid FormSG form ID' }),
3232
formsg_submission_id: z
33-
.string()
33+
.string({ invalid_type_error: 'Empty FormSG submission ID' })
3434
.trim()
3535
.min(1, { message: 'Specify a submission ID' }),
36-
nonce: z.string().trim().min(1, { message: 'Specify a nonce field' }),
36+
nonce: z
37+
.string({ invalid_type_error: 'Empty FormSG reference field answer' })
38+
.trim()
39+
.min(1, { message: 'Specify a FormSG reference field answer' }),
3740
payer_name: z
38-
.string()
41+
.string({ invalid_type_error: 'Empty payer name' })
3942
.trim()
4043
.min(1, { message: 'Empty payer name' })
4144
.max(255, { message: 'Payer name cannot be more than 255 characters' })
@@ -65,8 +68,10 @@ export const requestSchema = z.object({
6568
.min(1, { message: 'Empty payment amount' })
6669
.pipe(
6770
z.coerce
68-
.number()
69-
.int('Payment amount must be round number')
71+
.number({
72+
invalid_type_error: 'Payment amount must be a number',
73+
})
74+
.int('Payment amount must be a round number')
7075
.min(50, { message: 'Payment amount must be larger than 50 cents' })
7176
.max(99999999, {
7277
message: 'Payment amount cannot be larger than $999999.99',
@@ -80,8 +85,14 @@ export const requestSchema = z.object({
8085
.optional(),
8186
responses: z.array(
8287
z.object({
83-
question: z.string().trim().min(1, { message: 'Empty question' }),
84-
answer: z.string().trim().min(1, { message: 'Empty answer' }),
88+
question: z
89+
.string({ invalid_type_error: 'Empty question' })
90+
.trim()
91+
.min(1, { message: 'Empty question' }),
92+
answer: z
93+
.string({ invalid_type_error: 'Empty answer' })
94+
.trim()
95+
.min(1, { message: 'Empty answer' }),
8596
}),
8697
),
8798
})

0 commit comments

Comments
 (0)