fix: log errors in writeShopifyIdToSheet and skip 404s in sync-stock

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-26 11:40:12 +02:00
parent a9850c8efd
commit d276f6224c
2 changed files with 17 additions and 4 deletions
+8 -2
View File
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth' import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth' import { authOptions } from '@/lib/auth'
import { fetchPendingProducts, findColIndex } from '@/lib/creationSheets' import { findColIndex } from '@/lib/creationSheets'
import { getConfig } from '@/lib/shopifySync' import { getConfig } from '@/lib/shopifySync'
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
@@ -86,8 +86,13 @@ export async function POST(req: NextRequest) {
try { try {
// Récupérer le variant pour obtenir inventory_item_id // Récupérer le variant pour obtenir inventory_item_id
const pRes = await fetch(`${baseUrl}/products/${shopifyId}.json?fields=variants`, { headers }) const pRes = await fetch(`${baseUrl}/products/${shopifyId}.json?fields=variants`, { headers })
if (pRes.status === 404) {
// Produit supprimé de Shopify, on ignore silencieusement
continue
}
if (!pRes.ok) { if (!pRes.ok) {
send({ type: 'error', title, message: `Produit ${shopifyId} introuvable (${pRes.status})` }) const msg = `Produit ${shopifyId} erreur (${pRes.status})`
send({ type: 'error', title, message: msg })
errors++ errors++
continue continue
} }
@@ -121,6 +126,7 @@ export async function POST(req: NextRequest) {
send({ type: 'done', updated, errors }) send({ type: 'done', updated, errors })
} catch (err) { } catch (err) {
const message = err instanceof Error ? err.message : 'Erreur inconnue' const message = err instanceof Error ? err.message : 'Erreur inconnue'
console.error('[sync-stock] fatal:', message)
send({ type: 'done', updated: 0, errors: 1, fatalError: message }) send({ type: 'done', updated: 0, errors: 1, fatalError: message })
} finally { } finally {
controller.close() controller.close()
+9 -2
View File
@@ -114,11 +114,14 @@ export async function writeShopifyIdToSheet(tab: string, rowNumber: number, shop
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: jwt }), body: new URLSearchParams({ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: jwt }),
}) })
if (!tokenRes.ok) return if (!tokenRes.ok) {
console.error(`[sheets] token error ${tokenRes.status} for ${tab}!A${rowNumber}`)
return
}
const { access_token } = await tokenRes.json() as { access_token: string } const { access_token } = await tokenRes.json() as { access_token: string }
const range = encodeURIComponent(`${tab}!A${rowNumber}`) const range = encodeURIComponent(`${tab}!A${rowNumber}`)
await fetch( const writeRes = await fetch(
`https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?valueInputOption=RAW`, `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}/values/${range}?valueInputOption=RAW`,
{ {
method: 'PUT', method: 'PUT',
@@ -126,6 +129,10 @@ export async function writeShopifyIdToSheet(tab: string, rowNumber: number, shop
body: JSON.stringify({ range: `${tab}!A${rowNumber}`, majorDimension: 'ROWS', values: [[shopifyId]] }), body: JSON.stringify({ range: `${tab}!A${rowNumber}`, majorDimension: 'ROWS', values: [[shopifyId]] }),
}, },
) )
if (!writeRes.ok) {
const txt = await writeRes.text()
console.error(`[sheets] write error ${writeRes.status} for ${tab}!A${rowNumber}: ${txt}`)
}
} }
export async function fetchPendingProducts( export async function fetchPendingProducts(