Cause: The upstream Groq API request failed.When it occurs:
Groq API returns a non-2xx status code
Network connectivity issues to Groq’s servers
Invalid API key configuration
Groq API service outages
Rate limit exceeded on Groq’s side
Response Example:
{ "error": "Groq error", "details": "Error message from Groq API"}
The details field contains the raw error response from Groq’s API, which may include sensitive information about API limits or authentication issues. Handle this information carefully in production environments.
Validate input before sending requests to avoid 400 errors:
function validateTemplateRequest(prompt, area) { const errors = []; // Validate prompt if (!prompt) { errors.push('El campo "prompt" es requerido'); } else if (typeof prompt !== 'string') { errors.push('El campo "prompt" debe ser una cadena de texto'); } else if (prompt.trim().length === 0) { errors.push('El campo "prompt" no puede estar vacío'); } else if (prompt.length > 1000) { errors.push('El campo "prompt" excede el límite de 1000 caracteres'); } // Validate area (optional but should be string if provided) if (area !== undefined && typeof area !== 'string') { errors.push('El campo "area" debe ser una cadena de texto'); } return { valid: errors.length === 0, errors };}// Usageconst validation = validateTemplateRequest(prompt, area);if (!validation.valid) { console.error('Validation errors:', validation.errors); return;}
Error Logging and Monitoring
Implement comprehensive error logging for production:
class APIErrorLogger { static log(error, context) { const errorLog = { timestamp: new Date().toISOString(), statusCode: error.status, errorType: error.error, details: error.details, context: { prompt: context.prompt?.substring(0, 100), // Log first 100 chars only area: context.area, userId: context.userId, requestId: context.requestId } }; // Send to logging service (e.g., Sentry, LogRocket, CloudWatch) console.error('[API Error]', JSON.stringify(errorLog)); // Different handling based on error type switch (error.status) { case 400: // Track validation errors to identify UX issues this.trackMetric('validation_error'); break; case 502: // Alert on Groq API failures this.alertOpsTeam('Groq API failure', errorLog); break; case 500: // Critical server errors need immediate attention this.alertOpsTeam('Server error', errorLog); break; } } static trackMetric(metric) { // Implement metric tracking } static alertOpsTeam(title, details) { // Implement alerting (e.g., PagerDuty, Slack) }}