diff --git a/src/middleware/error.ts b/src/middleware/error.ts index fe3189d3..c93113a6 100644 --- a/src/middleware/error.ts +++ b/src/middleware/error.ts @@ -13,7 +13,7 @@ export const errorToStatusCode = ( ) => { if (error instanceof ValidationError) { res.status(400).json({ - error: error.message, + error: error.fiatConnectError, data: error.validationError, }) } else if ( diff --git a/src/routes/accounts.ts b/src/routes/accounts.ts index 08562b0b..f67abc12 100644 --- a/src/routes/accounts.ts +++ b/src/routes/accounts.ts @@ -8,6 +8,7 @@ import { } from '../types' import { siweAuthMiddleware } from '../middleware/authenticate' import { + FiatConnectError, deleteFiatAccountRequestParamsSchema, postFiatAccountRequestBodySchema, } from '@fiatconnect/fiatconnect-types' @@ -27,7 +28,11 @@ export function accountsRouter({ _res: express.Response, next: express.NextFunction, ) => { - validateZodSchema(req.body, postFiatAccountRequestBodySchema) + validateZodSchema( + req.body, + postFiatAccountRequestBodySchema, + FiatConnectError.InvalidSchema, + ) next() } diff --git a/src/routes/kyc.ts b/src/routes/kyc.ts index 138cb600..2ce6abfc 100644 --- a/src/routes/kyc.ts +++ b/src/routes/kyc.ts @@ -2,6 +2,7 @@ import express from 'express' import { asyncRoute } from './async-route' import { validateZodSchema } from '../schema/' import { + FiatConnectError, KycRequestParams, KycSchemas, NotImplementedError, @@ -55,7 +56,11 @@ export function kycRouter({ _res: express.Response, ) => { // Delegate to type-specific handlers after validation provides type guards - validateZodSchema(req.body, kycSchemaToZodSchema[req.params.kycSchema]) + validateZodSchema( + req.body, + kycSchemaToZodSchema[req.params.kycSchema], + FiatConnectError.InvalidParameters, + ) throw new NotImplementedError('POST /kyc/:kycSchema not implemented') }, diff --git a/src/schema/index.ts b/src/schema/index.ts index ec1c62f8..5da18449 100644 --- a/src/schema/index.ts +++ b/src/schema/index.ts @@ -1,4 +1,4 @@ -import { ValidationError } from '../types' +import { FiatConnectError, ValidationError } from '../types' import { z, ZodError } from 'zod' import { ZodType } from 'zod/lib/types' @@ -12,6 +12,7 @@ import { ZodType } from 'zod/lib/types' export function validateZodSchema( obj: any, schema: T, + fiatConnectError: FiatConnectError = FiatConnectError.InvalidParameters, ): z.infer { try { return schema.parse(obj) @@ -20,6 +21,7 @@ export function validateZodSchema( throw new ValidationError( `Error validating object with schema ${schema.description}`, err.issues, + fiatConnectError, ) } throw err diff --git a/src/types.ts b/src/types.ts index 3b04f70e..cb7e8ff4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -68,9 +68,15 @@ export enum Network { export class ValidationError extends Error { validationError: any - constructor(msg: string, validationError: any) { + fiatConnectError: FiatConnectError + constructor( + msg: string, + validationError: any, + fiatConnectError: FiatConnectError, + ) { super(msg) this.validationError = validationError + this.fiatConnectError = fiatConnectError } }