Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: display error messages during installation #1909

Merged
merged 6 commits into from
Jan 16, 2025
Merged
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
1 change: 1 addition & 0 deletions rust/agama-server/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ async fn run_events_monitor(dbus: zbus::Connection, events: EventsSender) -> Res
tokio::pin!(stream);
let e = events.clone();
while let Some((_, event)) = stream.next().await {
tracing::info!("event: {:?}", &event);
_ = e.send(event);
}
Ok(())
Expand Down
5 changes: 4 additions & 1 deletion rust/agama-server/src/web/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ async fn handle_socket(mut socket: WebSocket, events: EventsSender) {
let mut rx = events.subscribe();
while let Ok(msg) = rx.recv().await {
if let Ok(json) = serde_json::to_string(&msg) {
_ = socket.send(Message::Text(json)).await;
if socket.send(Message::Text(json)).await.is_err() {
tracing::info!("ws: client disconnected");
return;
}
}
}
}
7 changes: 7 additions & 0 deletions rust/package/agama.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Thu Jan 16 13:10:54 UTC 2025 - Imobach Gonzalez Sosa <[email protected]>

- Stop the WebSocket handler when the client is disconnected
(gh#agama-project/agama#1909).
- Log the events.

-------------------------------------------------------------------
Thu Jan 16 09:32:00 UTC 2025 - Martin Vidner <[email protected]>

Expand Down
6 changes: 6 additions & 0 deletions web/package/agama-web-ui.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Thu Jan 16 13:11:06 UTC 2025 - Imobach Gonzalez Sosa <[email protected]>

- Display error messages during package installation
(gh#agama-project/agama#1909).

-------------------------------------------------------------------
Mon Jan 13 11:11:49 UTC 2025 - David Diaz <[email protected]>

Expand Down
15 changes: 10 additions & 5 deletions web/src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
* find current contact information at www.suse.com.
*/

import { WSClient } from "./ws";
import { WSClient, EventHandlerFn, ErrorHandlerFn } from "./ws";

type VoidFn = () => void;
type BooleanFn = () => boolean;
type EventHandlerFn = (event) => void;

export type InstallerClient = {
/** Whether the client is connected. */
Expand All @@ -37,10 +36,15 @@ export type InstallerClient = {
*/
onConnect: (handler: VoidFn) => VoidFn;
/**
* Registers a handler to run when connection is lost. It returns a function
* Registers a handler to run when connection is closed. It returns a function
* for deregistering the handler.
*/
onDisconnect: (handler: VoidFn) => VoidFn;
onClose: (handler: VoidFn) => VoidFn;
/**
* Registers a handler to run when there is an error. It returns a function
* for deregistering the handler.
*/
onError: (handler: ErrorHandlerFn) => VoidFn;
/**
* Registers a handler to run on events. It returns a function for
* deregistering the handler.
Expand All @@ -66,7 +70,8 @@ const createClient = (url: URL): InstallerClient => {
isConnected,
isRecoverable,
onConnect: (handler: VoidFn) => ws.onOpen(handler),
onDisconnect: (handler: VoidFn) => ws.onClose(handler),
onClose: (handler: VoidFn) => ws.onClose(handler),
onError: (handler: ErrorHandlerFn) => ws.onError(handler),
onEvent: (handler: EventHandlerFn) => ws.onEvent(handler),
};
};
Expand Down
19 changes: 10 additions & 9 deletions web/src/client/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

type RemoveFn = () => void;
type BaseHandlerFn = () => void;
type EventHandlerFn = (event) => void;
export type EventHandlerFn = (event) => void;
export type ErrorHandlerFn = (error: object) => void;

/**
* Enum for the WebSocket states.
Expand Down Expand Up @@ -53,7 +54,7 @@ class WSClient {
handlers: {
open: Array<BaseHandlerFn>;
close: Array<BaseHandlerFn>;
error: Array<BaseHandlerFn>;
error: Array<ErrorHandlerFn>;
events: Array<EventHandlerFn>;
};

Expand Down Expand Up @@ -109,14 +110,14 @@ class WSClient {
};

client.onclose = () => {
console.log(`WebSocket closed`);
console.log("WebSocket closed");
this.dispatchCloseEvent();
this.timeout = setTimeout(() => this.connect(this.reconnectAttempts + 1), ATTEMPT_INTERVAL);
};

client.onerror = (e) => {
console.error("WebSocket error:", e);
this.dispatchErrorEvent();
client.onerror = (error) => {
console.error("WebSocket error:", error);
this.dispatchErrorEvent(error);
};

return client;
Expand Down Expand Up @@ -179,7 +180,7 @@ class WSClient {
*
* The handler is executed when an error is reported by the socket.
*/
onError(func: BaseHandlerFn): RemoveFn {
onError(func: ErrorHandlerFn): RemoveFn {
this.handlers.error.push(func);

return () => {
Expand Down Expand Up @@ -214,8 +215,8 @@ class WSClient {
*
* Dispatchs an error event by running all its handlers.
*/
dispatchErrorEvent() {
this.handlers.error.forEach((f) => f());
dispatchErrorEvent(error) {
this.handlers.error.forEach((f) => f(error));
}

/**
Expand Down
22 changes: 21 additions & 1 deletion web/src/context/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,28 @@ import React from "react";
import { InstallerClientProvider } from "./installer";
import { InstallerL10nProvider } from "./installerL10n";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { localConnection } from "~/utils";

const queryClient = new QueryClient();
// Determines which "network mode" should Tanstack Query use
//
// When running on a local connection, we assume that the server is always
// available so Tanstack Query is expected to perform all the request, no
// matter whether the network is available on not.
//
// For remote connections, let's use the default "online" mode.
//
// See https://tanstack.com/query/latest/docs/framework/react/guides/network-mode
const networkMode = (): "always" | "online" => {
return localConnection() ? "always" : "online";
};

const queryClient = new QueryClient({
defaultOptions: {
queries: {
networkMode: networkMode(),
},
},
});

/**
* Combines all application providers.
Expand Down
3 changes: 2 additions & 1 deletion web/src/context/installer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ describe("installer context", () => {
(createDefaultClient as jest.Mock).mockImplementation(() => {
return {
onConnect: jest.fn(),
onDisconnect: jest.fn(),
onClose: jest.fn(),
onError: jest.fn(),
};
});
});
Expand Down
2 changes: 1 addition & 1 deletion web/src/context/installer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function InstallerClientProvider({ children, client = null }: InstallerClientPro
setError(false);
});

value.onDisconnect(() => {
value.onClose(() => {
setConnected(false);
setError(!value.isRecoverable());
});
Expand Down
3 changes: 2 additions & 1 deletion web/src/context/installerL10n.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const client = {
isConnected: jest.fn().mockResolvedValue(true),
isRecoverable: jest.fn(),
onConnect: jest.fn(),
onDisconnect: jest.fn(),
onClose: jest.fn(),
onError: jest.fn(),
onEvent: jest.fn(),
};

Expand Down
8 changes: 8 additions & 0 deletions web/src/queries/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const useQuestionsChanges = () => {
}
});
}, [client, queryClient]);

React.useEffect(() => {
if (!client) return;

return client.onConnect(() => {
queryClient.invalidateQueries({ queryKey: ["questions"] });
});
}, [client, queryClient]);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions web/src/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ const Providers = ({ children, withL10n }) => {

// FIXME: workaround to fix the tests. We should inject
// the client instead of mocking `createClient`.
if (!client.onDisconnect) {
client.onDisconnect = noop;
if (!client.onClose) {
client.onClose = noop;
}

if (withL10n) {
Expand Down
Loading