@@ -237,36 +237,69 @@ <h2 class="text-2xl font-light text-gray-800 mb-2">${collection.title}</h2>
237237 ` ;
238238 }
239239
240- // Simple working approach based on user's example
240+ // Simple working approach with better error handling
241241 async function getCuratedArtworks ( collectionKey ) {
242242 try {
243243 // Show loading state
244244 loadingIndicator . style . display = 'block' ;
245245 gallery . innerHTML = '' ;
246246
247+ console . log ( 'Attempting to fetch from Met Museum API...' ) ;
248+
247249 // Use the exact same approach as the working example
248250 const response = await fetch ( 'https://collectionapi.metmuseum.org/public/collection/v1/search?isHighlight=true&hasImages=true&q=a' ) ;
251+
252+ console . log ( 'Response status:' , response . status , response . statusText ) ;
253+
254+ if ( ! response . ok ) {
255+ throw new Error ( `HTTP ${ response . status } : ${ response . statusText } ` ) ;
256+ }
257+
249258 const data = await response . json ( ) ;
259+ console . log ( 'Search data received:' , data ) ;
260+
250261 const objectIDs = data . objectIDs ;
251262
252- if ( ! objectIDs ) {
253- showErrorState ( 'No artworks found' ) ;
263+ if ( ! objectIDs || objectIDs . length === 0 ) {
264+ showErrorState ( 'No artworks found in the Met Museum collection ' ) ;
254265 return ;
255266 }
256267
268+ console . log ( `Found ${ objectIDs . length } artworks, fetching first 20...` ) ;
269+
257270 // Fetch details for a selection of artworks (exactly like the working example)
258271 const artworks = await Promise . all ( objectIDs . slice ( 0 , 20 ) . map ( async ( id ) => {
259- const res = await fetch ( `https://collectionapi.metmuseum.org/public/collection/v1/objects/${ id } ` ) ;
260- return res . json ( ) ;
272+ try {
273+ const res = await fetch ( `https://collectionapi.metmuseum.org/public/collection/v1/objects/${ id } ` ) ;
274+ if ( ! res . ok ) return null ;
275+ return res . json ( ) ;
276+ } catch ( err ) {
277+ console . warn ( `Failed to fetch artwork ${ id } :` , err ) ;
278+ return null ;
279+ }
261280 } ) ) ;
262281
263282 // Filter and display only artworks with images
264- const validArtworks = artworks . filter ( artwork => artwork . primaryImageSmall ) ;
283+ const validArtworks = artworks . filter ( artwork => artwork && artwork . primaryImageSmall ) ;
284+ console . log ( `Found ${ validArtworks . length } artworks with images` ) ;
285+
286+ if ( validArtworks . length === 0 ) {
287+ showErrorState ( 'No artworks with images available at this time' ) ;
288+ return ;
289+ }
290+
265291 displayArtworks ( validArtworks ) ;
266292
267293 } catch ( error ) {
268294 console . error ( 'Error fetching artworks:' , error ) ;
269- showErrorState ( 'There was an error fetching the artworks. Please try again later.' ) ;
295+
296+ if ( error . message . includes ( 'HTTP 403' ) || error . message . includes ( 'Incapsula' ) ) {
297+ showErrorState ( 'The Met Museum API is temporarily unavailable due to security restrictions. Please try again in a few minutes.' ) ;
298+ } else if ( error . message . includes ( 'NetworkError' ) || error . message . includes ( 'Failed to fetch' ) ) {
299+ showErrorState ( 'Unable to connect to the Met Museum API. Please check your internet connection and try again.' ) ;
300+ } else {
301+ showErrorState ( `API Error: ${ error . message } . Please try again later.` ) ;
302+ }
270303 } finally {
271304 loadingIndicator . style . display = 'none' ;
272305 }
0 commit comments