@@ -2128,3 +2128,101 @@ test('beforeRetry handles stream to Buffer conversion', withServer, async (t, se
21282128 t . is ( response . body , 'Got: buffer-data' ) ;
21292129 t . is ( requestCount , 2 ) ;
21302130} ) ;
2131+
2132+ test ( 'handler error is properly thrown in .json()' , withServer , async ( t , _server , got ) => {
2133+ const customError = new Error ( 'Custom handler error' ) ;
2134+ const instance = got . extend ( {
2135+ handlers : [
2136+ ( options , next ) => ( async ( ) => {
2137+ try {
2138+ return await next ( options ) ;
2139+ } catch {
2140+ throw customError ;
2141+ }
2142+ } ) ( ) ,
2143+ ] ,
2144+ } ) ;
2145+
2146+ await t . throwsAsync ( instance ( '' ) . json ( ) , { message : 'Custom handler error' } ) ;
2147+ } ) ;
2148+
2149+ test ( 'handler error is properly thrown in .text()' , withServer , async ( t , _server , got ) => {
2150+ const customError = new Error ( 'Custom handler error for text' ) ;
2151+ const instance = got . extend ( {
2152+ handlers : [
2153+ ( options , next ) => ( async ( ) => {
2154+ try {
2155+ return await next ( options ) ;
2156+ } catch {
2157+ throw customError ;
2158+ }
2159+ } ) ( ) ,
2160+ ] ,
2161+ } ) ;
2162+
2163+ await t . throwsAsync ( instance ( '' ) . text ( ) , { message : 'Custom handler error for text' } ) ;
2164+ } ) ;
2165+
2166+ test ( 'handler error is properly thrown in .buffer()' , withServer , async ( t , _server , got ) => {
2167+ const customError = new Error ( 'Custom handler error for buffer' ) ;
2168+ const instance = got . extend ( {
2169+ handlers : [
2170+ ( options , next ) => ( async ( ) => {
2171+ try {
2172+ return await next ( options ) ;
2173+ } catch {
2174+ throw customError ;
2175+ }
2176+ } ) ( ) ,
2177+ ] ,
2178+ } ) ;
2179+
2180+ await t . throwsAsync ( instance ( '' ) . buffer ( ) , { message : 'Custom handler error for buffer' } ) ;
2181+ } ) ;
2182+
2183+ test ( 'handler throwing on successful response works with .json()' , withServer , async ( t , server , got ) => {
2184+ server . get ( '/' , ( _request , response ) => {
2185+ response . setHeader ( 'content-type' , 'application/json' ) ;
2186+ response . end ( '{"success": true}' ) ;
2187+ } ) ;
2188+
2189+ const customError = new Error ( 'Handler rejected success' ) ;
2190+ const instance = got . extend ( {
2191+ handlers : [
2192+ ( options , next ) => ( async ( ) => {
2193+ await next ( options ) ;
2194+ throw customError ;
2195+ } ) ( ) ,
2196+ ] ,
2197+ } ) ;
2198+
2199+ await t . throwsAsync ( instance ( '' ) . json ( ) , { message : 'Handler rejected success' } ) ;
2200+ } ) ;
2201+
2202+ test ( 'multiple handlers with error transformation work with .json()' , withServer , async ( t , _server , got ) => {
2203+ const instance = got . extend ( {
2204+ handlers : [
2205+ // First handler: catches and wraps error
2206+ ( options , next ) => ( async ( ) => {
2207+ try {
2208+ return await next ( options ) ;
2209+ } catch ( error : any ) {
2210+ const wrappedError = new Error ( `Handler 1: ${ error . message } ` ) ;
2211+ throw wrappedError ;
2212+ }
2213+ } ) ( ) ,
2214+ // Second handler: catches and wraps error again
2215+ ( options , next ) => ( async ( ) => {
2216+ try {
2217+ return await next ( options ) ;
2218+ } catch ( error : any ) {
2219+ const wrappedError = new Error ( `Handler 2: ${ error . message } ` ) ;
2220+ throw wrappedError ;
2221+ }
2222+ } ) ( ) ,
2223+ ] ,
2224+ } ) ;
2225+
2226+ // Should get error from first handler (outermost)
2227+ await t . throwsAsync ( instance ( '' ) . json ( ) , { message : / H a n d l e r 1 : H a n d l e r 2 : / } ) ;
2228+ } ) ;
0 commit comments