From 4066217862739442ed2f1a81362d5612fedd7a63 Mon Sep 17 00:00:00 2001 From: josedario87 Date: Fri, 30 May 2025 11:57:19 -0600 Subject: [PATCH] estandarizado el manejo de errores --- ui/src/stores/useAsistencias.js | 16 ++++-- ui/src/stores/useEmpleados.js | 64 +++++++++++---------- ui/src/stores/usePlanillas.js | 33 ++++++----- ui/src/stores/useTareas.js | 44 +++++++------- ui/src/views/asistencias/AsistenciaForm.vue | 3 +- 5 files changed, 89 insertions(+), 71 deletions(-) diff --git a/ui/src/stores/useAsistencias.js b/ui/src/stores/useAsistencias.js index cab02f7..4b4fafd 100644 --- a/ui/src/stores/useAsistencias.js +++ b/ui/src/stores/useAsistencias.js @@ -25,7 +25,8 @@ export const useAsistenciasStore = defineStore('asistencias', { this.asistencias = response.data; } catch (error) { console.error('Error fetching asistencias:', error); - // Handle error (e.g., show a notification to the user) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; // Re-throw to allow handling in component } }, @@ -39,6 +40,8 @@ export const useAsistenciasStore = defineStore('asistencias', { } catch (error) { console.error(`Error fetching asistencia with id ${id}:`, error); this.currentAsistencia = getDefaultCurrentAsistencia(); // Reset on error + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; // Re-throw to allow handling in component } }, @@ -52,8 +55,9 @@ export const useAsistenciasStore = defineStore('asistencias', { await this.fetchAsistencias(); // Refresh the list return data; } catch (error) { - console.error('Error creating asistencia:', error.message); - throw error; // Re-throw to allow form to handle it + console.error('Error creating asistencia:', error); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; // Re-throw to allow form to handle it } }, @@ -68,7 +72,8 @@ export const useAsistenciasStore = defineStore('asistencias', { return data; } catch (error) { console.error(`Error updating asistencia with id ${id}:`, error); - throw error; // Re-throw to allow form to handle it + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; // Re-throw to allow form to handle it } }, @@ -78,7 +83,8 @@ export const useAsistenciasStore = defineStore('asistencias', { await this.fetchAsistencias(); // Refresh the list } catch (error) { console.error(`Error deleting asistencia with id ${id}:`, error); - throw error; // Re-throw to allow handling in component + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; // Re-throw to allow handling in component } }, diff --git a/ui/src/stores/useEmpleados.js b/ui/src/stores/useEmpleados.js index 0bbb3b3..ab43803 100644 --- a/ui/src/stores/useEmpleados.js +++ b/ui/src/stores/useEmpleados.js @@ -1,21 +1,18 @@ import { defineStore } from 'pinia'; -import apiClient from '../apiClient'; // ← asegurate que tenga baseURL y headers +import apiClient from '../apiClient'; -// Valor por defecto para el empleado activo const getDefaultEmpleado = () => ({ id: null, created_at: null, updated_at: null, name: '', cedula: null, - ubicacion: '.', // default del schema + ubicacion: '.', grupo_estudio: null, - empleado: true, // marcamos que SÍ es empleado + empleado: true, avatar_url: null, telefono: null, idciat: null, - // Ojo: asistencias, tareasRealizadas y planillas se manejan en otras stores - // No sé la forma exacta de esos arrays; se cargan aparte. }); export const useEmpleadosStore = defineStore('empleados', { @@ -25,61 +22,66 @@ export const useEmpleadosStore = defineStore('empleados', { }), actions: { - // ────────── CRUD básico ────────── - async fetchEmpleados () { + async fetchEmpleados() { try { const { data } = await apiClient.get('/api/empleados'); this.empleados = data; - } catch (err) { - console.error('Error al traer empleados:', err); + } catch (error) { + console.error('Error al traer empleados:', error); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - async fetchEmpleadoById (id) { + async fetchEmpleadoById(id) { try { const { data } = await apiClient.get(`/api/empleados/${id}`); this.currentEmpleado = data; - } catch (err) { - console.error(`Error al traer empleado ${id}:`, err); + } catch (error) { + console.error(`Error al traer empleado ${id}:`, error); this.currentEmpleado = getDefaultEmpleado(); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - async createEmpleado (empleadoData) { + async createEmpleado(empleadoData) { try { - const {data} = await apiClient.post('/api/empleados/', empleadoData); + const { data } = await apiClient.post('/api/empleados/', empleadoData); await this.fetchEmpleados(); - return data - } catch (err) { - console.error('Error creando empleado:', err); - throw err; // para que el form muestre feedback + return data; + } catch (error) { + console.error('Error creando empleado:', error); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - async updateEmpleado (id, empleadoData) { + async updateEmpleado(id, empleadoData) { try { - const {data} = await apiClient.put(`/api/empleados/${id}`, empleadoData); + const { data } = await apiClient.put(`/api/empleados/${id}`, empleadoData); await this.fetchEmpleados(); this.clearCurrentEmpleado(); - return data - } catch (err) { - console.error(`Error actualizando empleado ${id}:`, err); - throw err; + return data; + } catch (error) { + console.error(`Error actualizando empleado ${id}:`, error); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - async deleteEmpleado (id) { + async deleteEmpleado(id) { try { await apiClient.delete(`/api/empleados/${id}`); await this.fetchEmpleados(); - } catch (err) { - console.error(`Error eliminando empleado ${id}:`, err); - throw err; + } catch (error) { + console.error(`Error eliminando empleado ${id}:`, error); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - // ────────── helpers ────────── - clearCurrentEmpleado () { + clearCurrentEmpleado() { this.currentEmpleado = getDefaultEmpleado(); }, }, diff --git a/ui/src/stores/usePlanillas.js b/ui/src/stores/usePlanillas.js index b415fad..c56fc0c 100644 --- a/ui/src/stores/usePlanillas.js +++ b/ui/src/stores/usePlanillas.js @@ -24,8 +24,10 @@ export const usePlanillasStore = defineStore('planillas', { try { const { data } = await apiClient.get('/api/planillas') this.planillas = data - } catch (e) { - console.error('Error fetching planillas:', e) + } catch (error) { + console.error('Error fetching planillas:', error) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.' + throw errorMsg } }, @@ -33,9 +35,11 @@ export const usePlanillasStore = defineStore('planillas', { try { const { data } = await apiClient.get(`/api/planillas/${id}`) this.currentPlanilla = data - } catch (e) { - console.error(`Error fetching planilla ${id}:`, e) + } catch (error) { + console.error(`Error fetching planilla ${id}:`, error) this.currentPlanilla = getDefaultCurrentPlanilla() + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.' + throw errorMsg } }, @@ -44,9 +48,10 @@ export const usePlanillasStore = defineStore('planillas', { const { data } = await apiClient.post('/api/planillas', planillaData) await this.fetchPlanillas() return data - } catch (e) { - console.error('Error creating planilla:', e) - throw e + } catch (error) { + console.error('Error creating planilla:', error) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.' + throw errorMsg } }, @@ -56,9 +61,10 @@ export const usePlanillasStore = defineStore('planillas', { await this.fetchPlanillas() this.clearCurrentPlanilla() return data - } catch (e) { - console.error(`Error updating planilla ${id}:`, e) - throw e + } catch (error) { + console.error(`Error updating planilla ${id}:`, error) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.' + throw errorMsg } }, @@ -66,9 +72,10 @@ export const usePlanillasStore = defineStore('planillas', { try { await apiClient.delete(`/api/planillas/${id}`) await this.fetchPlanillas() - } catch (e) { - console.error(`Error deleting planilla ${id}:`, e) - throw e + } catch (error) { + console.error(`Error deleting planilla ${id}:`, error) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.' + throw errorMsg } }, diff --git a/ui/src/stores/useTareas.js b/ui/src/stores/useTareas.js index 0866087..f39ccc5 100644 --- a/ui/src/stores/useTareas.js +++ b/ui/src/stores/useTareas.js @@ -1,18 +1,17 @@ import { defineStore } from 'pinia'; -import apiClient from '../apiClient'; // Assuming apiClient is configured +import apiClient from '../apiClient'; // Helper function to get default values for currentTarea const getDefaultCurrentTarea = () => ({ id: null, empleado_id: null, - planilla_id: null, // Optional + planilla_id: null, titulo: '', - precio: null, // Optional - estado: 'pendiente', // Default from schema - observacion: '', // Optional - fecha: null, // Should be a date - tipo: '', // Default from schema - // fecha_anulado is not typically part of creation/edit form directly + precio: null, + estado: 'pendiente', + observacion: '', + fecha: null, + tipo: '', }); export const useTareasStore = defineStore('tareas', { @@ -27,7 +26,8 @@ export const useTareasStore = defineStore('tareas', { this.tareas = response.data; } catch (error) { console.error('Error fetching tareas:', error); - // Consider more sophisticated error handling (e.g., user notifications) + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, @@ -37,44 +37,48 @@ export const useTareasStore = defineStore('tareas', { this.currentTarea = response.data; } catch (error) { console.error(`Error fetching tarea with id ${id}:`, error); - this.currentTarea = getDefaultCurrentTarea(); // Reset on error + this.currentTarea = getDefaultCurrentTarea(); + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, async createTarea(tareaData) { try { const { data } = await apiClient.post('/api/tareas', tareaData); - await this.fetchTareas(); // Refresh the list - return data; // Return created tarea data if needed + await this.fetchTareas(); + return data; } catch (error) { console.error('Error creating tarea:', error); - throw error; // Re-throw to allow form to handle it + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, async updateTarea(id, tareaData) { try { const { data } = await apiClient.put(`/api/tareas/${id}`, tareaData); - await this.fetchTareas(); // Refresh the list - this.currentTarea = getDefaultCurrentTarea(); // Reset currentTarea - return data; // Return updated tarea data if needed + await this.fetchTareas(); + this.currentTarea = getDefaultCurrentTarea(); + return data; } catch (error) { console.error(`Error updating tarea with id ${id}:`, error); - throw error; // Re-throw to allow form to handle it + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, async deleteTarea(id) { try { await apiClient.delete(`/api/tareas/${id}`); - await this.fetchTareas(); // Refresh the list + await this.fetchTareas(); } catch (error) { console.error(`Error deleting tarea with id ${id}:`, error); - throw error; // Re-throw to allow handling in component + const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; + throw errorMsg; } }, - // Action to clear currentTarea, useful when navigating away from a form clearCurrentTarea() { this.currentTarea = getDefaultCurrentTarea(); } diff --git a/ui/src/views/asistencias/AsistenciaForm.vue b/ui/src/views/asistencias/AsistenciaForm.vue index cdf4b78..cad12e1 100644 --- a/ui/src/views/asistencias/AsistenciaForm.vue +++ b/ui/src/views/asistencias/AsistenciaForm.vue @@ -191,8 +191,7 @@ const handleSubmit = async () => { router.push({ name: 'asistencias-index' }); } catch (error) { console.error('Error saving asistencia:', error); - const errorMsg = error.response?.data?.message || error.message || 'Ocurrió un error.'; - alert(`Error al guardar la asistencia: ${errorMsg}`); + alert(`Error al guardar la asistencia: ${error}`); } finally { isSubmitting.value = false; }