Add error handling to notifications

This commit is contained in:
Thomas Avé 2025-11-05 15:50:17 +07:00
parent a54e216279
commit b04613407b
2 changed files with 54 additions and 10 deletions

View File

@ -39,8 +39,38 @@ export default function Notification({
onHoverLost: () => void; onHoverLost: () => void;
}) { }) {
const timer = timeout(3000, () => { const timer = timeout(3000, () => {
onHoverLost(); try {
onHoverLost();
} catch (error) {
console.error("Failed to handle onHoverLost:", error);
}
}); });
const handleDismiss = () => {
try {
n.dismiss();
} catch (error) {
console.error("Failed to dismiss notification:", error);
}
};
const handleInvoke = (id: string) => {
try {
n.invoke(id);
} catch (error) {
console.error(`Failed to invoke action ${id}:`, error);
}
};
const handleTime = (timeValue: number) => {
try {
return time(timeValue);
} catch (error) {
console.error("Failed to format time:", error);
return "Invalid time";
}
};
return ( return (
<Adw.Clamp maximumSize={400}> <Adw.Clamp maximumSize={400}>
<box <box
@ -70,9 +100,9 @@ export default function Notification({
class="time" class="time"
hexpand hexpand
halign={Gtk.Align.END} halign={Gtk.Align.END}
label={time(n.time)} label={handleTime(n.time)}
/> />
<button onClicked={() => n.dismiss()}> <button onClicked={handleDismiss}>
<image iconName="window-close-symbolic" /> <image iconName="window-close-symbolic" />
</button> </button>
</box> </box>
@ -114,7 +144,7 @@ export default function Notification({
{n.actions.length > 0 && ( {n.actions.length > 0 && (
<box class="actions"> <box class="actions">
{n.actions.map(({ label, id }) => ( {n.actions.map(({ label, id }) => (
<button hexpand onClicked={() => n.invoke(id)}> <button hexpand onClicked={() => handleInvoke(id)}>
<label label={label} halign={Gtk.Align.CENTER} hexpand /> <label label={label} halign={Gtk.Align.CENTER} hexpand />
</button> </button>
))} ))}

View File

@ -14,17 +14,31 @@ export default function NotificationPopups() {
); );
const notifiedHandler = notifd.connect("notified", (_, id, replaced) => { const notifiedHandler = notifd.connect("notified", (_, id, replaced) => {
const notification = notifd.get_notification(id); try {
const notification = notifd.get_notification(id);
if (!notification) {
console.error(`Failed to get notification with id: ${id}`);
return;
}
if (replaced && notifications.get().some((n) => n.id === id)) { if (replaced && notifications.get().some((n) => n.id === id)) {
setNotifications((ns) => ns.map((n) => (n.id === id ? notification : n))); setNotifications((ns) =>
} else { ns.map((n) => (n.id === id ? notification : n)),
setNotifications((ns) => [notification, ...ns]); );
} else {
setNotifications((ns) => [notification, ...ns]);
}
} catch (error) {
console.error("Failed to handle notification:", error);
} }
}); });
const resolvedHandler = notifd.connect("resolved", (_, id) => { const resolvedHandler = notifd.connect("resolved", (_, id) => {
setNotifications((ns) => ns.filter((n) => n.id !== id)); try {
setNotifications((ns) => ns.filter((n) => n.id !== id));
} catch (error) {
console.error("Failed to resolve notification:", error);
}
}); });
// technically, we don't need to cleanup because in this example this is a root component // technically, we don't need to cleanup because in this example this is a root component