Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cloudflare/internal/ai-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ export class Ai {
return new InferenceUpstreamError(content);
}
} catch {
// FL2 returns a plain text "error code: 1031" response when the edge
// preview token has expired (e.g. during `wrangler dev`).
if (content.includes('error code: 1031')) {
return new InferenceUpstreamError(
`${content} (your API token may have expired — try running \`wrangler login\` to obtain a fresh token and restart your dev server)`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think running wrangler login again is required. Simply restarting wrangler solves the problem.

);
}

return new InferenceUpstreamError(content);
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/cloudflare/internal/test/ai/ai-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,39 @@ export const tests = {
assert.equal(err.message, 'Unknown error');
}

{
// Test FL2 plain text "error code: 1031" (expired preview token)
// includes wrangler login hint. FL2 returns this as text/plain when
// the edge preview token has expired during `wrangler dev`.
const err = await env.ai._parseError(
new Response('error code: 1031', {
status: 400,
headers: { 'content-type': 'text/plain; charset=UTF-8' },
})
);
assert.equal(err.name, 'InferenceUpstreamError');
assert.ok(err.message.includes('1031'), 'should include error code 1031');
assert.ok(
err.message.includes('wrangler login'),
'should suggest running wrangler login'
);
}

{
// Test error code 1031 via mock (expired preview token model)
try {
await env.ai.run('expiredTokenModel', { prompt: 'test' });
assert.fail('should have thrown');
} catch (e) {
assert.equal(e.name, 'InferenceUpstreamError');
assert.ok(e.message.includes('1031'), 'should include error code 1031');
assert.ok(
e.message.includes('wrangler login'),
'should suggest running wrangler login'
);
}
}

{
// Test raw input
const resp = await env.ai.run('rawInputs', { prompt: 'test' });
Expand Down
12 changes: 12 additions & 0 deletions src/cloudflare/internal/test/ai/ai-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ export default {
);
}

if (modelName === 'expiredTokenModel') {
// Simulate the real FL2 response when an edge preview token has expired.
// FL2 returns plain text "error code: 1031" with HTTP 400.
return new Response('error code: 1031', {
status: 400,
headers: {
'content-type': 'text/plain; charset=UTF-8',
...respHeaders,
},
});
}

// Handle websocket requests
if (isWebsocket && modelName === '@cf/test/websocket') {
// For websocket requests, extract data from URL 'body' parameter
Expand Down
Loading