@@ -78,17 +78,24 @@ export async function getBitcoinPrice(timeframe: TimeFrame): Promise<BitcoinPric
7878
7979 clearTimeout ( timeoutId ) ;
8080
81- if ( realtimeResponse . ok ) {
82- const realtimeData = await realtimeResponse . json ( ) ;
83-
84- if ( realtimeData . success && realtimeData . price ) {
85- realtimePrice = realtimeData . price ;
86- console . log ( 'Using real-time price:' , realtimePrice , 'from' , realtimeData . source || 'unknown' ) ;
87- } else {
88- console . warn ( 'Real-time API returned success:false' ) ;
89- }
90- } else {
81+ // Try to parse even if status is not OK
82+ let realtimeData ;
83+ try {
84+ realtimeData = await realtimeResponse . json ( ) ;
85+ console . log ( 'Real-time API response:' , realtimeData ) ;
86+ } catch ( parseError ) {
87+ console . warn ( 'Failed to parse real-time API response' , parseError ) ;
88+ const text = await realtimeResponse . text ( ) ;
89+ console . warn ( 'Raw response:' , text ) ;
90+ }
91+
92+ if ( realtimeData && realtimeData . success && realtimeData . price ) {
93+ realtimePrice = realtimeData . price ;
94+ console . log ( 'Using real-time price:' , realtimePrice , 'from' , realtimeData . source || 'unknown' ) ;
95+ } else if ( ! realtimeResponse . ok ) {
9196 console . warn ( 'Real-time API returned non-OK status:' , realtimeResponse . status ) ;
97+ } else {
98+ console . warn ( 'Real-time API returned invalid data:' , realtimeData ) ;
9299 }
93100 } catch ( error ) {
94101 console . warn ( 'Failed to get real-time price, will use historical data:' , error ) ;
@@ -123,11 +130,31 @@ export async function getBitcoinPrice(timeframe: TimeFrame): Promise<BitcoinPric
123130
124131 clearTimeout ( timeoutId ) ;
125132
133+ // Try to parse the response even if status is not OK
134+ let result ;
135+ try {
136+ result = await response . json ( ) ;
137+ console . log ( 'Historical API response structure:' ,
138+ result ?
139+ `success=${ ! ! result . success } , has timeframes=${ ! ! result . timeframes } , timeframe count=${ result . timeframes ? Object . keys ( result . timeframes ) . length : 0 } ` :
140+ 'null response'
141+ ) ;
142+ } catch ( error ) {
143+ console . error ( 'Failed to parse historical API response' , error ) ;
144+ const text = await response . text ( ) ;
145+ console . error ( 'Raw historical response:' , text ) ;
146+ throw new Error ( `Failed to parse API response: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
147+ }
148+
126149 if ( ! response . ok ) {
127- throw new Error ( ` API error: ${ response . status } `) ;
150+ console . error ( `Historical API error: ${ response . status } `) ;
128151 }
129152
130- const result = await response . json ( ) ;
153+ // Validate the response structure
154+ if ( ! result || ! result . price || ! result . timeframes ) {
155+ console . error ( 'Invalid historical API response structure:' , result ) ;
156+ throw new Error ( 'Invalid historical data structure' ) ;
157+ }
131158
132159 // Save to cache for future use
133160 saveToCache ( result ) ;
0 commit comments