// This is where we made the fetch request, we use try-catch for error handling try { // Make the fetch request const response = await fetch(url, config);
// Parse the response const responseBody = await response.json; // const responseBody = await response.text; // Check if the response status is OK (status in the range 200-299) if (!response.ok) { // If not OK, throw an error to be caught by the catch block throw new Error(`HTTP error! status: ${response.status}, Message: ${JSON.stringify(responseBody)}`); }
return { // Map the success path so we can continue in our flow next: { path: 'success' }, // Optionally, return the response body if needed outputVars: { id: responseBody.id } }; } // Catches all the errors we threw and displays the debug message catch (error) { return { // Maps the error path so we can continue in our design next: { path: 'error' }, // Renders a debug message in Voiceflow with the error trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }] }; } }