how would you rewrite this vue app such that it returns proper error if anything goes wrong? 


<template>
  <div class="app-container">
    <header class="app-header">
      <h1>API Request Client</h1>
      <p class="subtitle">Query descriptive analytics from your API endpoint</p>
    </header>

    <main class="app-main">
      <div class="request-form">
        <div class="form-group">
          <label for="apiUrl">API Endpoint</label>
          <input
            type="text"
            id="apiUrl"
            v-model="apiUrl"
            placeholder="http://localhost:8080/descriptive"
            class="form-input"
          />
        </div>

        <div class="form-group">
          <label for="datasetPath">Dataset Path</label>
          <input
            type="text"
            id="datasetPath"
            v-model="datasetPath"
            placeholder="G:\projects\uv_rup99\handyscripts\products-100.csv"
            class="form-input"
          />
        </div>

        <div class="form-group">
          <label for="column">Column</label>
          <input
            type="text"
            id="column"
            v-model="column"
            placeholder="Price"
            class="form-input"
          />
        </div>

        <button
          @click="fetchData"
          :disabled="isLoading"
          class="submit-button"
        >
          <span v-if="!isLoading">Send Request</span>
          <span v-else class="loading-indicator">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
          </span>
        </button>
      </div>

      <div class="results-container">
        <div v-if="statusMessage" class="status-message" :class="statusType">
          <div class="status-icon">
            <svg v-if="statusType === 'success'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
              <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M11,16.5L6.5,12L7.91,10.59L11,13.67L16.59,8.09L18,9.5L11,16.5Z" />
            </svg>
            <svg v-else-if="statusType === 'error'" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
              <path fill="currentColor" d="M12,2C6.47,2 2,6.47 2,12C2,17.53 6.47,22 12,22C17.53,22 22,17.53 22,12C22,6.47 17.53,2 12,2M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 17.59,20 12,20M12,6C10.9,6 10,6.9 10,8C10,9.1 10.9,10 12,10C13.1,10 14,9.1 14,8C14,6.9 13.1,6 12,6M12,16C10.9,16 10,15.1 10,14C10,12.9 10.9,12 12,12C13.1,12 14,12.9 14,14C14,15.1 13.1,16 12,16Z" />
            </svg>
            <svg v-else xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
              <path fill="currentColor" d="M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4M12,7A5,5 0 0,1 17,12A5,5 0 0,1 12,17A5,5 0 0,1 7,12A5,5 0 0,1 12,7M12,9A3,3 0 0,0 9,12A3,3 0 0,0 12,15A3,3 0 0,0 15,12A3,3 0 0,0 12,9Z" />
            </svg>
          </div>
          <div class="status-content">
            <p>{{ statusMessage }}</p>
            <div v-if="fullUrl" class="url-display">
              <span class="url-label">Request URL:</span>
              <code class="url-text">{{ fullUrl }}</code>
            </div>
          </div>
        </div>

        <div v-if="results" class="result-section">
          <h3>Response Preview</h3>
          <div class="json-container">
            <highlightjs
              language="json"
              :code="formattedResults"
              class="json-highlight"
            />
          </div>
          <button @click="copyToClipboard" class="copy-button">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16">
              <path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H10A2,2 0 0,0 8,3V5H6A2,2 0 0,0 4,7V19A2,2 0 0,0 6,21H18A2,2 0 0,0 20,19V10H18V8A2,2 0 0,0 16,6V3A2,2 0 0,0 16,1Z" />
            </svg>
            Copy to Clipboard
          </button>
        </div>

        <div v-if="errorDetails" class="error-section">
          <h3>Error Details</h3>
          <div class="error-content">
            <highlightjs
              language="json"
              :code="formattedError"
              class="json-highlight"
            />
          </div>
        </div>
      </div>
    </main>

    <footer class="app-footer">
      <p>API Client &copy; {{ new Date().getFullYear() }}</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import { Highlightjs } from 'vue-highlightjs';
import 'highlight.js/styles/atom-one-dark.css';


// Reactive state
const apiUrl = ref('http://localhost:8080/descriptive');
const datasetPath = ref('G:\\projects\\uv_rup99\\handyscripts\\products-100.csv');
const column = ref('Price');
const fullUrl = ref('');
const statusMessage = ref('');
const statusType = ref('info'); // 'info', 'success', 'error'
const results = ref(null);
const errorDetails = ref(null);
const isLoading = ref(false);

// Computed properties
const formattedResults = computed(() => {
  return results.value ? JSON.stringify(results.value, null, 2) : '';
});

const formattedError = computed(() => {
  if (!errorDetails.value) return '';
  if (typeof errorDetails.value === 'object') {
    return JSON.stringify(errorDetails.value, null, 2);
  }
  return errorDetails.value;
});

// Methods
const fetchData = async () => {
  // Reset state
  isLoading.value = true;
  statusMessage.value = '';
  statusType.value = 'info';
  results.value = null;
  errorDetails.value = null;
  fullUrl.value = '';

  try {
    // URL-encode parameters
    const encodedDatasetPath = encodeURIComponent(datasetPath.value);
    const encodedColumn = encodeURIComponent(column.value);

    // Construct the full URL
    const constructedUrl = `${apiUrl.value}?datasetPath=${encodedDatasetPath}&column=${encodedColumn}`;
    console.log(constructedUrl);
    fullUrl.value = constructedUrl;

    statusMessage.value = `Querying URL: ${constructedUrl}`;
    statusType.value = 'info';

    const response = await fetch(constructedUrl, {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
      },
    });

    if (response.ok) {
      const data = await response.json();

      if (data) {
        results.value = data;
        statusMessage.value = 'Request successful!';
        statusType.value = 'success';
      } else {
        statusMessage.value = 'Received an empty response from the API.';
        statusType.value = 'warning';
      }
    } else {
      statusType.value = 'error';
      let errorBody = 'Could not read error body.';

      try {
        const text = await response.text();
        try {
          errorBody = JSON.parse(text);
        } catch (e) {
          errorBody = text;
        }
      } catch (readError) {
        console.error("Failed to read error body:", readError);
      }

      statusMessage.value = `API request failed with status: ${response.status}`;
      errorDetails.value = {
        statusCode: response.status,
        statusText: response.statusText,
        details: response.text,
        body: errorBody,
      };
    }
  }  
  catch (error) {
    console.error("An error occurred during the API request:", error);
    statusType.value = 'error';
    statusMessage.value = 'An error occurred during the API request.';
    errorDetails.value = {
      message: error.message,
      stack: error.stack,
      details: response.text
    };
  // catch (jsonError) {
  // // Handle invalid JSON
  // errorDetails.value = {
  //   message: 'Invalid JSON response',
  //   details: response.text()
  // };
  throw jsonError; // Rethrow to trigger catch block
  } finally {
    isLoading.value = false;
  }
};

const copyToClipboard = () => {
  if (!results.value) return;

  navigator.clipboard.writeText(formattedResults.value)
    .then(() => {
      statusMessage.value = 'Copied to clipboard!';
      statusType.value = 'success';
      setTimeout(() => {
        if (results.value) {
          statusMessage.value = 'Request successful!';
        }
      }, 2000);
    })
    .catch(err => {
      console.error('Failed to copy: ', err);
      statusMessage.value = 'Failed to copy to clipboard';
      statusType.value = 'error';
    });
};
</script>

<style>
/* Base styles */
:root {
  --primary-color: #4361ee;
  --primary-dark: #3a56d4;
  --success-color: #3bb273;
  --error-color: #f5365c;
  --warning-color: #ffd166;
  --info-color: #11cdef;
  --text-color: #333;
  --light-text: #777;
  --background: #f8f9fa;
  --card-bg: #ffffff;
  --border-color: #e9ecef;
  --shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  --radius: 8px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.6;
  color: var(--text-color);
  background-color: var(--background);
}

/* App container */
.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* Header styles */
.app-header {
  text-align: center;
  margin-bottom: 30px;
  padding: 20px 0;
}

.app-header h1 {
  color: var(--primary-color);
  font-size: 2.2rem;
  margin-bottom: 10px;
}

.subtitle {
  color: var(--light-text);
  font-size: 1.1rem;
}

/* Main content */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

/* Request form */
.request-form {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 30px;
  box-shadow: var(--shadow);
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: var(--text-color);
}

.form-input {
  width: 100%;
  padding: 12px 15px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 2px rgba(67, 97, 238, 0.2);
}

/* Submit button */
.submit-button {
  width: 100%;
  padding: 14px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
}

.submit-button:hover:not(:disabled) {
  background-color: var(--primary-dark);
  transform: translateY(-2px);
}

.submit-button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
  transform: none;
}

/* Loading indicator */
.loading-indicator {
  display: flex;
  gap: 5px;
}

.dot {
  width: 8px;
  height: 8px;
  background-color: white;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}

.dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

/* Results container */
.results-container {
  flex: 1;
}

/* Status message */
.status-message {
  display: flex;
  align-items: flex-start;
  gap: 15px;
  padding: 15px 20px;
  border-radius: var(--radius);
  margin-bottom: 20px;
}

.status-message.info {
  background-color: rgba(17, 205, 239, 0.1);
  border-left: 4px solid var(--info-color);
}

.status-message.success {
  background-color: rgba(59, 178, 115, 0.1);
  border-left: 4px solid var(--success-color);
}

.status-message.warning {
  background-color: rgba(255, 209, 102, 0.1);
  border-left: 4px solid var(--warning-color);
}

.status-message.error {
  background-color: rgba(245, 54, 92, 0.1);
  border-left: 4px solid var(--error-color);
}

.status-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.status-message.info .status-icon {
  color: var(--info-color);
}

.status-message.success .status-icon {
  color: var(--success-color);
}

.status-message.warning .status-icon {
  color: var(--warning-color);
}

.status-message.error .status-icon {
  color: var(--error-color);
}

.status-content {
  flex: 1;
}

.url-display {
  margin-top: 10px;
  padding: 10px;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: var(--radius);
  font-family: monospace;
  word-break: break-all;
}

.url-label {
  display: block;
  font-size: 0.9rem;
  color: var(--light-text);
  margin-bottom: 5px;
}

.url-text {
  color: var(--primary-color);
  font-size: 0.9rem;
}

/* Result section */
.result-section, .error-section {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 25px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

.result-section h3, .error-section h3 {
  color: var(--primary-color);
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.json-container {
  background-color: #282c34;
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 15px;
  overflow-x: auto;
}

.json-highlight {
  font-size: 0.9rem;
  line-height: 1.5;
}

/* Copy button */
.copy-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.copy-button:hover {
  background-color: var(--primary-dark);
}

/* Footer */
.app-footer {
  text-align: center;
  padding: 20px 0;
  color: var(--light-text);
  font-size: 0.9rem;
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .app-container {
    padding: 10px;
  }

  .request-form, .result-section, .error-section {
    padding: 20px;
  }

  .status-message {
    flex-direction: column;
    gap: 10px;
  }

  .status-icon {
    width: 30px;
    height: 30px;
  }
}
</style>