Unverified Commit 2ad93490 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Improve common errors in turbo-trace-server (#100862)

More than once i have been blocked by the the vscode sandbox when
attempting to read a trace file, now we print the PermissionDenied error
so this is easier to diagnose

a client disconnect also gets a simpler error message, these are not a
big deal
parent fc5ebd29
use std::{ use std::{
io,
net::{SocketAddr, SocketAddrV4, TcpListener, TcpStream}, net::{SocketAddr, SocketAddrV4, TcpListener, TcpStream},
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread::spawn, thread::spawn,
...@@ -6,7 +7,7 @@ use std::{ ...@@ -6,7 +7,7 @@ use std::{
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tungstenite::{Message, accept}; use tungstenite::{Message, accept, error::ProtocolError};
use crate::{ use crate::{
store::SpanId, store::SpanId,
...@@ -124,13 +125,39 @@ pub fn serve(store: Arc<StoreContainer>, port: u16) { ...@@ -124,13 +125,39 @@ pub fn serve(store: Arc<StoreContainer>, port: u16) {
spawn(move || { spawn(move || {
let websocket = accept(stream.unwrap()).unwrap(); let websocket = accept(stream.unwrap()).unwrap();
if let Err(err) = handle_connection(websocket, store) { match handle_connection(websocket, store) {
eprintln!("Error: {err:?}"); Ok(()) => {}
Err(err) if is_disconnect(&err) => {
println!("Trace viewer disconnected");
}
Err(err) => {
eprintln!("Error: {err:?}");
}
} }
}); });
} }
} }
/// Whether the error is caused by the client going away without a clean close handshake, e.g.
/// when the trace viewer tab is closed or reloaded while an update is being sent.
fn is_disconnect(err: &anyhow::Error) -> bool {
match err.downcast_ref::<tungstenite::Error>() {
Some(
tungstenite::Error::ConnectionClosed
| tungstenite::Error::AlreadyClosed
| tungstenite::Error::Protocol(ProtocolError::ResetWithoutClosingHandshake),
) => true,
Some(tungstenite::Error::Io(err)) => matches!(
err.kind(),
io::ErrorKind::ConnectionReset
| io::ErrorKind::ConnectionAborted
| io::ErrorKind::BrokenPipe
| io::ErrorKind::UnexpectedEof
),
_ => false,
}
}
fn handle_connection( fn handle_connection(
mut websocket: tungstenite::WebSocket<TcpStream>, mut websocket: tungstenite::WebSocket<TcpStream>,
store: Arc<StoreContainer>, store: Arc<StoreContainer>,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment