import { AxiosError } from "axios" import type { ApiError } from "./client" import { ago } from "./components/Health/queries" import { offlineDetail } from "./lib/connectionStore" function extractErrorMessage(err: ApiError): string { // An unreachable installation is not a rejected action: say plainly that // nothing was delivered, so nobody is left wondering whether it half-landed. const offline = offlineDetail(err) if (offline) { return offline.lastSeen ? `Not delivered — the installation is offline (last seen ${ago(offline.lastSeen)})` : "Not delivered — the installation is offline" } if (err instanceof AxiosError) { return err.message } const errDetail = (err.body as any)?.detail if (Array.isArray(errDetail) && errDetail.length > 0) { return errDetail[0].msg } return errDetail || "Something went wrong." } export const handleError = function ( this: (msg: string) => void, err: ApiError, ) { const errorMessage = extractErrorMessage(err) this(errorMessage) }