'use strict';

async function getData(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      // Handle HTTP errors by throwing an exception
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    // Centralized error logging and fallback behavior
    console.error('Error fetching data:', error);
    // Optionally, return a default value or handle the error gracefully
    return null;
  }
}

// Usage
getData('https://dummyjson.com/posts')
  .then((data) => {
    if (data) {
      console.log('Data:', data);
    } else {
      console.log('No data received');
    }
  })
  .catch((error) => {
    console.error('Error in getData:', error);
  });
