BlueGit

ict rusty-teams

Files

rusty-teams / crates / webview / src / cdp_failure.rs

cdp_failure.rs

cdp_failure.rs 3.2 KiB Download Raw
1use serde_json::Value;
2use session::{App, is_login_url};
3
4/// Coarse state of the app tab for error reports, never the URL itself.
5pub(crate) fn tab_state(app: App, url: &str) -> &'static str {
6 if url.is_empty() {
7 "no document"
8 } else if app.is_park_url(url) {
9 "parked"
10 } else if is_login_url(url) {
11 "login"
12 } else if app.origins().iter().any(|origin| url.starts_with(origin)) {
13 "app"
14 } else if url.starts_with("about:") {
15 "about"
16 } else if url.starts_with("chrome-error:") {
17 "error page"
18 } else {
19 "other"
20 }
21}
22
23pub(crate) struct CallContext<'a> {
24 pub method: &'a str,
25 pub params_bytes: usize,
26 pub tab: &'static str,
27 pub in_flight: usize,
28}
29
30/// WebView2 answers every protocol error with E_INVALIDARG; the protocol's own error sits in the JSON.
31pub(crate) fn failure_reason(context: &CallContext, hresult: &str, json: &str) -> String {
32 let parsed = serde_json::from_str::<Value>(json).unwrap_or(Value::Null);
33 let error = parsed.get("error").unwrap_or(&parsed);
34 let message = error.get("message").and_then(Value::as_str).unwrap_or(hresult);
35 let code = error
36 .get("code")
37 .and_then(Value::as_i64)
38 .map(|code| format!(" code {code},"))
39 .unwrap_or_default();
40 format!(
41 "{message} [{}{code} params {} bytes, tab {}, {} call(s) in flight]",
42 context.method, context.params_bytes, context.tab, context.in_flight,
43 )
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 const HRESULT: &str = "The parameter is incorrect. (0x80070057)";
51
52 fn context() -> CallContext<'static> {
53 CallContext {
54 method: "Runtime.evaluate",
55 params_bytes: 48213,
56 tab: "parked",
57 in_flight: 2,
58 }
59 }
60
61 #[test]
62 fn the_protocol_message_leads_the_reason() {
63 let json = r#"{"code":-32000,"message":"Execution context was destroyed."}"#;
64 assert_eq!(
65 failure_reason(&context(), HRESULT, json),
66 "Execution context was destroyed. [Runtime.evaluate code -32000, params 48213 bytes, tab parked, 2 call(s) in flight]"
67 );
68 }
69
70 #[test]
71 fn a_nested_protocol_error_is_read_too() {
72 let json = r#"{"error":{"code":-32601,"message":"'Foo.bar' wasn't found"}}"#;
73 assert!(failure_reason(&context(), HRESULT, json).starts_with("'Foo.bar' wasn't found [Runtime.evaluate code -32601,"));
74 }
75
76 #[test]
77 fn without_a_protocol_error_the_hresult_and_the_call_are_named() {
78 assert_eq!(
79 failure_reason(&context(), HRESULT, ""),
80 "The parameter is incorrect. (0x80070057) [Runtime.evaluate params 48213 bytes, tab parked, 2 call(s) in flight]"
81 );
82 }
83
84 #[test]
85 fn the_tab_state_never_carries_the_url() {
86 assert_eq!(tab_state(App::Teams, "https://teams.cloud.microsoft/robots.txt"), "parked");
87 assert_eq!(tab_state(App::Teams, "https://teams.cloud.microsoft/v2/?ctx=chat"), "app");
88 assert_eq!(tab_state(App::Teams, "about:blank"), "about");
89 assert_eq!(tab_state(App::Teams, "chrome-error://chromewebdata/"), "error page");
90 assert_eq!(tab_state(App::Teams, ""), "no document");
91 assert_eq!(tab_state(App::Teams, "https://example.com/private/path"), "other");
92 }
93}