Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

[Metabot] Handle expired sessions (#79742)

Sloan Spargerd678585e399e
diff --git a/frontend/src/metabase/api/ai-streaming/requests.ts b/frontend/src/metabase/api/ai-streaming/requests.ts
index 6ca31b01245..6511493b839 100644
--- a/frontend/src/metabase/api/ai-streaming/requests.ts
+++ b/frontend/src/metabase/api/ai-streaming/requests.ts
@@ -65,6 +65,11 @@ export async function aiStreamingQuery(
});
if (!response.ok) {
+ // ensure we reach the global 401 handler, so the user can sign in if session is expired
+ if (response.status === 401) {
+ api.emit(401, req.url);
+ }
+
// Mirror the legacy client's error shape (`{ status, data }`) so streaming
// and non-streaming callers handle failures the same way. A non-JSON or
// empty error body leaves `data` undefined; the status still identifies it.
diff --git a/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
index e39cef6a503..b16ca101015 100644
--- a/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
+++ b/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
@@ -1,6 +1,7 @@
import fetchMock from "fetch-mock";
import { setupBasename } from "__support__/basename";
+import { api } from "metabase/api/client";
import {
aiStreamingQuery,
@@ -16,6 +17,8 @@ describe("ai requests", () => {
setupBasename(fakeBasename);
describe("aiStreamingQuery", () => {
+ afterEach(() => api.removeAllListeners(401));
+
it("should call a request with a baseurl", async () => {
const fetchSpy = jest.spyOn(global, "fetch");
@@ -87,10 +90,13 @@ describe("ai requests", () => {
});
it("throw error if bad http status code", async () => {
+ const on401 = jest.fn();
+ api.on(401, on401);
fetchMock.post(`path:${endpoint}`, 500);
await expect(
aiStreamingQuery({ url: endpoint, body: {} }),
).rejects.toMatchObject({ status: 500 });
+ expect(on401).not.toHaveBeenCalled();
});
it("preserves structured JSON error responses", async () => {
@@ -113,6 +119,17 @@ describe("ai requests", () => {
});
});
+ it("emits 401 so the global handler can send the user to login", async () => {
+ const on401 = jest.fn();
+ api.on(401, on401);
+ fetchMock.post(`path:${endpoint}`, 401);
+
+ await expect(
+ aiStreamingQuery({ url: endpoint, body: {} }),
+ ).rejects.toMatchObject({ status: 401 });
+ expect(on401).toHaveBeenCalledWith(endpoint);
+ });
+
it("throw error if no response", async () => {
mockEndpoint(endpoint, async () => new Response(null, { status: 202 }));
await expect(