@@ -56,6 +56,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
5656 switch ( _options . strategy . type ) {
5757 case 'URI' : {
5858 const errorFunction = _options . strategy . onError
59+ const statusFunction = _options . strategy . onStatus
60+ const catchFunction = _options . strategy . onCatch
5961 const redirectDefault = _options . strategy . redirectDefault ?? true
6062
6163 for ( const [ version , handler ] of versions ) {
@@ -96,13 +98,16 @@ function versioning<TVersions extends Record<string, Elysia>>(
9698 const handler = _options . versions [ defaultVersion ]
9799 if ( ! handler ) {
98100 errorFunction ?.( new UriUnknownVersionError ( defaultVersion ) )
101+ statusFunction ?.( 'unknown_version' )
99102 return
100103 }
101104
102105 try {
103106 return await handler . handle ( request )
104107 } catch ( error : unknown ) {
105108 errorFunction ?.( new UriUnexpectedError ( error as Error ) )
109+ catchFunction ?.( error as Error )
110+ statusFunction ?.( 'unexpected' )
106111 }
107112 }
108113 } )
@@ -114,6 +119,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
114119 const queryName = _options . strategy . queryName ?? prefix
115120 const redirectDefault = _options . strategy . redirectDefault ?? true
116121 const errorFunction = _options . strategy . onError
122+ const statusFunction = _options . strategy . onStatus
123+ const catchFunction = _options . strategy . onCatch
117124
118125 plugin . onRequest ( async ( { request } ) => {
119126 const url = new URL ( request . url )
@@ -124,27 +131,33 @@ function versioning<TVersions extends Record<string, Elysia>>(
124131 const handler = _options . versions [ versionQueryParameter ]
125132 if ( ! handler ) {
126133 errorFunction ?.( new QueryUnknownVersionError ( versionQueryParameter ) )
134+ statusFunction ?.( 'unknown_version' )
127135 return
128136 }
129137
130138 try {
131139 return await handler . handle ( request )
132140 } catch ( error : unknown ) {
133141 errorFunction ?.( new QueryUnexpectedError ( error as Error ) )
142+ catchFunction ?.( error as Error )
143+ statusFunction ?.( 'unexpected' )
134144 }
135145 }
136146
137147 if ( redirectDefault ) {
138148 const defaultHandler = _options . versions [ defaultVersion ]
139149 if ( ! defaultHandler ) {
140150 errorFunction ?.( new QueryUnknownVersionError ( defaultVersion ) )
151+ statusFunction ?.( 'unknown_version' )
141152 return
142153 }
143154
144155 try {
145156 return await defaultHandler . handle ( request )
146157 } catch ( error : unknown ) {
147158 errorFunction ?.( new QueryUnexpectedError ( error as Error ) )
159+ catchFunction ?.( error as Error )
160+ statusFunction ?.( 'unexpected' )
148161 }
149162 }
150163 } )
@@ -154,6 +167,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
154167 case 'HEADER' : {
155168 const headerName = _options . strategy . headerName ?? 'X-Version'
156169 const errorFunction = _options . strategy . onError
170+ const statusFunction = _options . strategy . onStatus
171+ const catchFunction = _options . strategy . onCatch
157172
158173 plugin . onRequest ( async ( { request } ) => {
159174 const header = request . headers . get ( headerName )
@@ -162,6 +177,7 @@ function versioning<TVersions extends Record<string, Elysia>>(
162177 if ( header ) {
163178 if ( ! header . startsWith ( prefix ) ) {
164179 errorFunction ?.( new HeaderWrongPrefixError ( prefix , header ) )
180+ statusFunction ?.( 'wrong_prefix' )
165181 return
166182 }
167183 version = header . replace ( prefix , '' )
@@ -170,13 +186,16 @@ function versioning<TVersions extends Record<string, Elysia>>(
170186 const handler = _options . versions [ version ]
171187 if ( ! handler ) {
172188 errorFunction ?.( new HeaderUnknownVersionError ( version ) )
189+ statusFunction ?.( 'unknown_version' )
173190 return
174191 }
175192
176193 try {
177194 return await handler . handle ( request )
178195 } catch ( error : unknown ) {
179196 errorFunction ?.( new HeaderUnexpectedError ( error as Error ) )
197+ catchFunction ?.( error as Error )
198+ statusFunction ?.( 'unexpected' )
180199 }
181200 } )
182201 break
@@ -185,6 +204,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
185204 case 'CUSTOM' : {
186205 const extractFunction = _options . strategy . extract
187206 const errorFunction = _options . strategy . onError
207+ const statusFunction = _options . strategy . onStatus
208+ const catchFunction = _options . strategy . onCatch
188209
189210 plugin . onRequest ( async ( { request } ) => {
190211 try {
@@ -194,6 +215,8 @@ function versioning<TVersions extends Record<string, Elysia>>(
194215 ) . handle ( request )
195216 } catch ( error : unknown ) {
196217 errorFunction ?.( new CustomUnexpectedError ( error as Error ) )
218+ catchFunction ?.( error as Error )
219+ statusFunction ?.( 'unexpected' )
197220 }
198221 } )
199222 break
0 commit comments