BlueGit

ict rusty-teams

Files

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

cdp_failure.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
cdp_failure.rs 3.4 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
35 .get("message")
36 .and_then(Value::as_str)
37 .unwrap_or(hresult);
38 let code = error
39 .get("code")
40 .and_then(Value::as_i64)
41 .map(|code| format!(" code {code},"))
42 .unwrap_or_default();
43 format!(
44 "{message} [{}{code} params {} bytes, tab {}, {} call(s) in flight]",
45 context.method, context.params_bytes, context.tab, context.in_flight,
46 )
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 const HRESULT: &str = "The parameter is incorrect. (0x80070057)";
54
55 fn context() -> CallContext<'static> {
56 CallContext {
57 method: "Runtime.evaluate",
58 params_bytes: 48213,
59 tab: "parked",
60 in_flight: 2,
61 }
62 }
63
64 #[test]
65 fn the_protocol_message_leads_the_reason() {
66 let json = r#"{"code":-32000,"message":"Execution context was destroyed."}"#;
67 assert_eq!(
68 failure_reason(&context(), HRESULT, json),
69 "Execution context was destroyed. [Runtime.evaluate code -32000, params 48213 bytes, tab parked, 2 call(s) in flight]"
70 );
71 }
72
73 #[test]
74 fn a_nested_protocol_error_is_read_too() {
75 let json = r#"{"error":{"code":-32601,"message":"'Foo.bar' wasn't found"}}"#;
76 assert!(
77 failure_reason(&context(), HRESULT, json)
78 .starts_with("'Foo.bar' wasn't found [Runtime.evaluate code -32601,")
79 );
80 }
81
82 #[test]
83 fn without_a_protocol_error_the_hresult_and_the_call_are_named() {
84 assert_eq!(
85 failure_reason(&context(), HRESULT, ""),
86 "The parameter is incorrect. (0x80070057) [Runtime.evaluate params 48213 bytes, tab parked, 2 call(s) in flight]"
87 );
88 }
89
90 #[test]
91 fn the_tab_state_never_carries_the_url() {
92 assert_eq!(
93 tab_state(App::Teams, "https://teams.cloud.microsoft/robots.txt"),
94 "parked"
95 );
96 assert_eq!(
97 tab_state(App::Teams, "https://teams.cloud.microsoft/v2/?ctx=chat"),
98 "app"
99 );
100 assert_eq!(tab_state(App::Teams, "about:blank"), "about");
101 assert_eq!(
102 tab_state(App::Teams, "chrome-error://chromewebdata/"),
103 "error page"
104 );
105 assert_eq!(tab_state(App::Teams, ""), "no document");
106 assert_eq!(
107 tab_state(App::Teams, "https://example.com/private/path"),
108 "other"
109 );
110 }
111}