Prisma + D1 fails when external API returns 429 error in Cloudflare Workers

Issue When an external API (CoinGecko) returns a 429 error, subsequent Prisma queries to D1 fail with:
PrismaClientUnknownRequestError: Invalid `prisma.userDeposit.create()` invocation:
Error occurred during query execution: ConnectorError(ConnectorError {
user_facing_error: None,
kind: QueryError(SqliteError {
extended_code: 1,
message: Some("D1_ERROR: Failed to parse body as JSON, got: ")
}),
transient: false
})
PrismaClientUnknownRequestError: Invalid `prisma.userDeposit.create()` invocation:
Error occurred during query execution: ConnectorError(ConnectorError {
user_facing_error: None,
kind: QueryError(SqliteError {
extended_code: 1,
message: Some("D1_ERROR: Failed to parse body as JSON, got: ")
}),
transient: false
})
Environment - Cloudflare Workers - D1 Database - Prisma with D1 adapter - Axios for HTTP requests Minimal Reproduction
// Works fine when external API succeeds
async function workingFlow() {
try {
// This succeeds
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');

// Database insert works
await prisma.userDeposit.create({
data: {
owner: 'test-user',
tokenId: 1,
amount: 100,
depositStatus: 'SUCCESS'
}
});
} catch (error) {
console.error(error);
}
}

// Fails when external API returns 429
async function failingFlow() {
try {
// This throws 429 error
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');
} catch (error) {
console.error('API failed with 429');
// Even though we caught the error, this still fails!
}

try {
// This fails with "Failed to parse body as JSON" error
await prisma.userDeposit.create({
data: {
owner: 'test-user',
tokenId: 1,
amount: 100,
depositStatus: 'SUCCESS'
}
});
} catch (error) {
console.error('Database insert failed:', error);
}
}
// Works fine when external API succeeds
async function workingFlow() {
try {
// This succeeds
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');

// Database insert works
await prisma.userDeposit.create({
data: {
owner: 'test-user',
tokenId: 1,
amount: 100,
depositStatus: 'SUCCESS'
}
});
} catch (error) {
console.error(error);
}
}

// Fails when external API returns 429
async function failingFlow() {
try {
// This throws 429 error
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd');
} catch (error) {
console.error('API failed with 429');
// Even though we caught the error, this still fails!
}

try {
// This fails with "Failed to parse body as JSON" error
await prisma.userDeposit.create({
data: {
owner: 'test-user',
tokenId: 1,
amount: 100,
depositStatus: 'SUCCESS'
}
});
} catch (error) {
console.error('Database insert failed:', error);
}
}
What I've Tried 1. Wrapping the API call in try-catch 2. Using separate try-catch blocks 3. Adding delays between API call and database operation 4. Logging the data being inserted (it's identical in both cases) Questions 1. Why does a 429 error from an external API affect subsequent D1/Prisma operations? 2. Is this a known issue with Cloudflare Workers context corruption? 3. Are there any workarounds besides switching from axios to fetch? Additional Context - The issue ONLY occurs when the external API returns 429 - The exact same database insert data works when the API succeeds - The error suggests D1 is receiving malformed data, but we're not sending any data from the API response Any insights would be greatly appreciated!
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?