@@ -2,7 +2,9 @@ import { NextFunction, Request, RequestHandler, Response, Router } from 'express
22import { ArraySchema as IArraySchema , ObjectSchema as IObjectSchema , Schema , ValidationError } from 'yup' ;
33
44import { basicAuth } from '../middlewares/basic-auth.middleware' ;
5+ import { CodedError } from './errors' ;
56import logger from './logger' ;
7+ import { evmQueryParamsSchema } from './schemas' ;
68
79interface ObjectStorageMethods < V > {
810 getByKey : ( key : string ) => Promise < V > ;
@@ -33,6 +35,36 @@ export const withBodyValidation =
3335 return handler ( req , res , next ) ;
3436 } ;
3537
38+ interface EvmQueryParams {
39+ walletAddress : string ;
40+ chainId : string ;
41+ }
42+
43+ type TypedEvmQueryRequestHandler = (
44+ req : Request ,
45+ res : Response ,
46+ next : NextFunction ,
47+ evmQueryParams : EvmQueryParams
48+ ) => void ;
49+
50+ export const withEvmQueryValidation =
51+ ( handler : TypedEvmQueryRequestHandler ) : RequestHandler =>
52+ async ( req , res , next ) => {
53+ let evmQueryParams : EvmQueryParams ;
54+
55+ try {
56+ evmQueryParams = await evmQueryParamsSchema . validate ( req . query ) ;
57+ } catch ( error ) {
58+ if ( error instanceof ValidationError ) {
59+ return res . status ( 400 ) . send ( { error : error . message } ) ;
60+ }
61+
62+ throw error ;
63+ }
64+
65+ return handler ( req , res , next , evmQueryParams ) ;
66+ } ;
67+
3668export const withExceptionHandler =
3769 ( handler : RequestHandler ) : RequestHandler =>
3870 async ( req , res , next ) => {
@@ -44,6 +76,22 @@ export const withExceptionHandler =
4476 }
4577 } ;
4678
79+ export const withCodedExceptionHandler =
80+ ( handler : RequestHandler ) : RequestHandler =>
81+ async ( req , res , next ) => {
82+ try {
83+ await handler ( req , res , next ) ;
84+ } catch ( error : any ) {
85+ logger . error ( error ) ;
86+
87+ if ( error instanceof CodedError ) {
88+ res . status ( error . code ) . send ( error . buildResponse ( ) ) ;
89+ } else {
90+ res . status ( 500 ) . send ( { message : error ?. message } ) ;
91+ }
92+ }
93+ } ;
94+
4795interface ObjectStorageMethodsEntrypointsConfig < StoredValue , ObjectResponse , ValueResponse > {
4896 path : string ;
4997 methods : ObjectStorageMethods < StoredValue > ;
0 commit comments