nicer retry error message
This commit is contained in:
parent
d6638263a7
commit
2c726c5bd2
|
|
@ -3,6 +3,7 @@
|
||||||
// input, output, etc
|
// input, output, etc
|
||||||
|
|
||||||
import { scrollback, $$ } from "./dom.js"
|
import { scrollback, $$ } from "./dom.js"
|
||||||
|
import { randomId } from "../shared/utils.js"
|
||||||
import type { CommandOutput } from "../shared/types.js"
|
import type { CommandOutput } from "../shared/types.js"
|
||||||
|
|
||||||
type InputStatus = "waiting" | "streaming" | "ok" | "error"
|
type InputStatus = "waiting" | "streaming" | "ok" | "error"
|
||||||
|
|
@ -48,7 +49,7 @@ export function setStatus(id: string, status: InputStatus) {
|
||||||
export function addOutput(id: string, output: CommandOutput) {
|
export function addOutput(id: string, output: CommandOutput) {
|
||||||
const item = $$("li")
|
const item = $$("li")
|
||||||
item.classList.add("output")
|
item.classList.add("output")
|
||||||
item.dataset.id = id
|
item.dataset.id = id || randomId()
|
||||||
|
|
||||||
const [format, content] = processOutput(output)
|
const [format, content] = processOutput(output)
|
||||||
|
|
||||||
|
|
@ -61,6 +62,10 @@ export function addOutput(id: string, output: CommandOutput) {
|
||||||
autoScroll()
|
autoScroll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function addErrorMessage(message: string) {
|
||||||
|
addOutput("", { html: `<span class="red">${message}</span>` })
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export function appendOutput(id: string, output: CommandOutput) {
|
export function appendOutput(id: string, output: CommandOutput) {
|
||||||
const item = document.querySelector(`[data-id="${id}"].output`)
|
const item = document.querySelector(`[data-id="${id}"].output`)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,10 @@
|
||||||
import type { Message } from "../shared/types.js"
|
import type { Message } from "../shared/types.js"
|
||||||
import { sessionID } from "./session.js"
|
import { sessionID } from "./session.js"
|
||||||
import { handleMessage } from "./shell.js"
|
import { handleMessage } from "./shell.js"
|
||||||
|
import { addErrorMessage } from "./scrollback.js"
|
||||||
|
|
||||||
|
const MAX_RETRIES = 5
|
||||||
|
let retries = 0
|
||||||
|
|
||||||
let ws: WebSocket | null = null
|
let ws: WebSocket | null = null
|
||||||
|
|
||||||
|
|
@ -14,7 +18,7 @@ export function startConnection() {
|
||||||
ws = new WebSocket(url)
|
ws = new WebSocket(url)
|
||||||
|
|
||||||
ws.onmessage = receive
|
ws.onmessage = receive
|
||||||
ws.onclose = () => setTimeout(startConnection, 1000) // simple retry
|
ws.onclose = retryConnection
|
||||||
ws.onerror = () => ws?.close()
|
ws.onerror = () => ws?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,3 +40,13 @@ export function close() {
|
||||||
ws?.close(1000, 'bye')
|
ws?.close(1000, 'bye')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function retryConnection() {
|
||||||
|
if (retries >= MAX_RETRIES) {
|
||||||
|
addErrorMessage(`!! Failed to reconnect ${retries} times. Server is down.`)
|
||||||
|
if (ws) ws.onclose = () => { }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
retries++
|
||||||
|
addErrorMessage(`!! Connection lost. Retrying...`)
|
||||||
|
setTimeout(startConnection, 1000)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user