estandarizado el manejo de errores
Some checks failed
build-and-deploy / filter (push) Successful in 2s
Sync to GitHub / sync (push) Failing after 1s
build-and-deploy / build (push) Successful in 10s
build-and-deploy / deploy (push) Successful in 15s

This commit is contained in:
2025-05-30 11:57:19 -06:00
parent 1eccd8d424
commit 4066217862
5 changed files with 89 additions and 71 deletions

View File

@@ -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
}
},

View File

@@ -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,13 +22,14 @@ export const useEmpleadosStore = defineStore('empleados', {
}),
actions: {
// ────────── CRUD básico ──────────
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;
}
},
@@ -39,9 +37,11 @@ export const useEmpleadosStore = defineStore('empleados', {
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;
}
},
@@ -49,10 +49,11 @@ export const useEmpleadosStore = defineStore('empleados', {
try {
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;
}
},
@@ -61,10 +62,11 @@ export const useEmpleadosStore = defineStore('empleados', {
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;
}
},
@@ -72,13 +74,13 @@ export const useEmpleadosStore = defineStore('empleados', {
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() {
this.currentEmpleado = getDefaultEmpleado();
},

View File

@@ -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
}
},

View File

@@ -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();
}

View File

@@ -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;
}