@@ -213,6 +213,73 @@ describe('standardHandler', () => {
213213 } )
214214 } )
215215
216+ it ( 'on decode error' , async ( ) => {
217+ matcher . match . mockResolvedValue ( {
218+ path : [ 'ping' ] ,
219+ procedure : ping ,
220+ params : { id : '__id__' } ,
221+ } )
222+
223+ const error = new Error ( 'Something bad' )
224+ codec . decode . mockRejectedValueOnce ( error )
225+ const client = vi . fn ( )
226+ vi . mocked ( createProcedureClient ) . mockReturnValueOnce ( client )
227+
228+ codec . decode . mockReturnValueOnce ( '__input__' )
229+
230+ codec . encodeError . mockReturnValueOnce ( response )
231+
232+ const result = await handler . handle ( request , {
233+ context : { db : 'postgres' } ,
234+ prefix : '/api/v1' ,
235+ } )
236+
237+ expect ( result ) . toEqual ( { matched : true , response } )
238+
239+ expect ( matcher . match ) . toHaveBeenCalledOnce ( )
240+ expect ( matcher . match ) . toHaveBeenCalledWith ( 'GET' , '/users/1' )
241+
242+ expect ( createProcedureClient ) . toHaveBeenCalledOnce ( )
243+ expect ( createProcedureClient ) . toHaveBeenCalledWith ( ping , {
244+ context : { db : 'postgres' } ,
245+ path : [ 'ping' ] ,
246+ } )
247+
248+ expect ( codec . decode ) . toHaveBeenCalledOnce ( )
249+ expect ( codec . decode ) . toHaveBeenCalledWith ( request , { id : '__id__' } , ping )
250+
251+ expect ( client ) . not . toHaveBeenCalledOnce ( )
252+ expect ( codec . encode ) . not . toBeCalled ( )
253+
254+ expect ( codec . encodeError ) . toHaveBeenCalledOnce ( )
255+ expect ( codec . encodeError . mock . calls [ 0 ] ! [ 0 ] ) . toSatisfy ( ( e : any ) => {
256+ expect ( e ) . toBeInstanceOf ( ORPCError )
257+ expect ( e . code ) . toEqual ( 'BAD_REQUEST' )
258+ expect ( e . message ) . toEqual (
259+ `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.` ,
260+ )
261+ expect ( e . cause ) . toEqual ( error )
262+
263+ return true
264+ } )
265+
266+ expect ( interceptor ) . toHaveBeenCalledOnce ( )
267+ expect ( interceptor ) . toHaveBeenCalledWith ( {
268+ request,
269+ next : expect . any ( Function ) ,
270+ context : { db : 'postgres' } ,
271+ prefix : '/api/v1' ,
272+ } )
273+
274+ expect ( interceptorRoot ) . toHaveBeenCalledOnce ( )
275+ expect ( interceptorRoot ) . toHaveBeenCalledWith ( {
276+ request,
277+ next : expect . any ( Function ) ,
278+ context : { db : 'postgres' } ,
279+ prefix : '/api/v1' ,
280+ } )
281+ } )
282+
216283 it ( 'work without context and prefix' , async ( ) => {
217284 matcher . match . mockResolvedValue ( {
218285 path : [ 'ping' ] ,
0 commit comments