refactor(actions): enhance error handling and ensure serializability in product actions

This commit is contained in:
Nils-Johan Gynther
2026-04-19 17:30:18 +02:00
parent 40b0d5fd3a
commit 81a8390bb7
+27 -9
View File
@@ -5,6 +5,7 @@ import { getAuthHeaders } from '../../lib/auth-headers';
const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080'; const API_BASE = process.env.NEXT_PUBLIC_API_URL_INTERNAL || 'http://recipe-api:8080';
export async function createProductAction(name: string) { export async function createProductAction(name: string) {
try {
const authHeaders = await getAuthHeaders(); const authHeaders = await getAuthHeaders();
console.log('[createProductAction] Creating product with name:', name); console.log('[createProductAction] Creating product with name:', name);
console.log('[createProductAction] Auth headers:', authHeaders ? 'YES' : 'NO'); console.log('[createProductAction] Auth headers:', authHeaders ? 'YES' : 'NO');
@@ -21,15 +22,25 @@ export async function createProductAction(name: string) {
} }
const product = await res.json(); const product = await res.json();
// Return only serializable fields (Dates can't be serialized across RSC boundary) console.log('[createProductAction] Response:', product);
return {
// Explicitly convert to plain object to ensure serializability
const result = JSON.parse(JSON.stringify({
id: product.id, id: product.id,
name: product.name, name: product.name,
canonicalName: product.canonicalName, canonicalName: product.canonicalName ?? null,
}; }));
console.log('[createProductAction] Returning:', result);
return result;
} catch (err) {
console.error('[createProductAction] Error:', err);
throw err;
}
} }
export async function updateProductCategoryAction(productId: number, categoryId: number) { export async function updateProductCategoryAction(productId: number, categoryId: number) {
try {
const authHeaders = await getAuthHeaders(); const authHeaders = await getAuthHeaders();
const res = await fetch(`${API_BASE}/api/products/${productId}`, { const res = await fetch(`${API_BASE}/api/products/${productId}`, {
@@ -44,11 +55,18 @@ export async function updateProductCategoryAction(productId: number, categoryId:
} }
const product = await res.json(); const product = await res.json();
// Return only serializable fields
return { // Explicitly convert to plain object to ensure serializability
const result = JSON.parse(JSON.stringify({
id: product.id, id: product.id,
name: product.name, name: product.name,
canonicalName: product.canonicalName, canonicalName: product.canonicalName ?? null,
categoryId: product.categoryId, categoryId: product.categoryId ?? null,
}; }));
return result;
} catch (err) {
console.error('[updateProductCategoryAction] Error:', err);
throw err;
}
} }