You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe what feature you'd like. Pseudo-code, mockups, or screenshots of similar solutions are encouraged!
To ensure that the build process fails if agendabasel.ch is not reachable or if the returned data is empty, you can modify the getActivities function to throw an error in these cases. This will cause the build process to fail when running in production mode, as the error will propagate and stop the build.
Here's how you can modify the getActivities function:
Check if the response from fetch is successful: If the fetch request fails (e.g., due to a network issue or a server error), throw an error.
Check if the response body is empty: If the fetched data is empty, throw an error.
Handle build process interruption: In production mode (i.e., when dev is false), the error should cause the build process to fail.
Modified getActivities Function
asyncfunctiongetActivities(){try{// Fetch the XML export from the external URLconstresponse=awaitfetch('https://agendabasel.ch/xmlexport/kzexport-basel.xml');// Check if the response is OK (status 200-299)if(!response.ok){thrownewError(`Failed to fetch data: ${response.statusText}`);}// Parse the response as textconstxml=awaitresponse.text();// Check if the response is emptyif(!xml){thrownewError('Received empty response from the server.');}// Parse the XML into a JavaScript objectconstdata=awaitparseStringPromise(xml);// Extract activitiesconstactivities=data['kdz:exportActivities']['Activities'][0]['Activity'];// If activities array is empty, throw an errorif(!activities||activities.length===0){thrownewError('No activities found in the response.');}// Save data to a file in dev modeif(dev){awaitsaveToFile(activities,join(tmpdir(),'activities.json'));console.log(getUniqueOwners(activities));}// Filter and parse the activities based on configured partnersconstpartners=config.partners;constparsedActivities=activities.filter(({$: { owner }})=>partners.includes(owner)).map(({$: { owner, dauerausstellung },Title: [title],ShortDescription: [shortDesc],LongDescription: [longDesc],OriginURL: [originUrl],ActivityDates: [{ActivityDate: dates=[]}={}]})=>({
owner,
dauerausstellung,
title,shortDescription: DOMPurify.sanitize(shortDesc,{ALLOWED_TAGS: []}),longDescription: DOMPurify.sanitize(longDesc,{ALLOWED_TAGS: []}),
originUrl,dates: dates.map(({$: { startDate, endDate, startTime, endTime },TicketURL: [ticketURL]})=>({
startDate,
endDate,
startTime,
endTime,ticketURL: ticketURL}))}));constexhibitions=parsedActivities.filter(({ dauerausstellung })=>dauerausstellung==='1').sort((a,b)=>a.owner.localeCompare(b.owner));constevents=parsedActivities.filter(({ dauerausstellung })=>dauerausstellung==='0');constflatEvents=events.flatMap(({ dates, owner, title, shortDescription, originUrl })=>dates.map((date)=>({ ...date, owner, title, shortDescription, originUrl }))).sort((a,b)=>newDate(a.startDate).getTime()-newDate(b.startDate).getTime());return{events: flatEvents, exhibitions };}catch(error){// In production, throw an error to stop the build processif(!dev){thrownewError(`Error fetching activities: ${error.message}`);}else{console.error(error);return{events: [],exhibitions: []};// Return empty arrays in dev mode for testing}}}
Key Changes:
Fetch Error Handling: If fetch fails (e.g., the server is unreachable), it throws an error.
Empty Response Handling: If the response is empty or if there are no activities in the XML, it throws an error.
Production Mode Build Fail: If dev is false, an error will be thrown, which will cause the build to fail.
Development Mode: In dev mode, the error is logged to the console, but it won't interrupt the process, allowing testing and debugging.
With this setup, the build process will halt with an error if the external resource is unreachable or the data is invalid, ensuring that the application doesn't proceed with incomplete or missing data.
What type of pull request would this be?
Enhancement
Any links to similar examples or other references we should review?
Describe what feature you'd like. Pseudo-code, mockups, or screenshots of similar solutions are encouraged!
To ensure that the build process fails if
agendabasel.ch
is not reachable or if the returned data is empty, you can modify thegetActivities
function to throw an error in these cases. This will cause the build process to fail when running in production mode, as the error will propagate and stop the build.Here's how you can modify the
getActivities
function:fetch
is successful: If the fetch request fails (e.g., due to a network issue or a server error), throw an error.dev
is false), the error should cause the build process to fail.Modified
getActivities
FunctionKey Changes:
fetch
fails (e.g., the server is unreachable), it throws an error.dev
isfalse
, an error will be thrown, which will cause the build to fail.dev
mode, the error is logged to the console, but it won't interrupt the process, allowing testing and debugging.With this setup, the build process will halt with an error if the external resource is unreachable or the data is invalid, ensuring that the application doesn't proceed with incomplete or missing data.
What type of pull request would this be?
Enhancement
Any links to similar examples or other references we should review?
stadtgeschichtebasel.ch/src/routes/agenda.json/+server.js
Line 76 in 3a8c798
The text was updated successfully, but these errors were encountered: