relay_event_normalization/
stacktrace.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::mem;

use relay_event_schema::processor;
use relay_event_schema::protocol::{Frame, RawStacktrace};
use relay_protocol::{Annotated, Empty, Meta};
use url::Url;

fn is_url(filename: &str) -> bool {
    filename.starts_with("file:")
        || filename.starts_with("http:")
        || filename.starts_with("https:")
        || filename.starts_with("applewebdata:")
}

pub fn normalize_stacktrace(stacktrace: &mut RawStacktrace, _meta: &mut Meta) {
    // This processing is only done for non raw frames (i.e. not for exception.raw_stacktrace).
    if let Some(frames) = stacktrace.frames.value_mut() {
        for frame in frames.iter_mut() {
            normalize_non_raw_frame(frame);
        }
    }
}

pub fn normalize_non_raw_frame(frame: &mut Annotated<Frame>) {
    let _ = processor::apply(frame, |frame, _meta| {
        if frame.abs_path.value().is_empty() {
            frame.abs_path = mem::replace(&mut frame.filename, Annotated::empty());
        }

        if frame.filename.value().is_empty() {
            if let Some(abs_path) = frame.abs_path.value_mut() {
                frame.filename = Annotated::new(abs_path.clone());

                if is_url(abs_path.as_str()) {
                    if let Ok(url) = Url::parse(abs_path.as_str()) {
                        let path = url.path();

                        if !path.is_empty() && path != "/" {
                            frame.filename = Annotated::new(path.into());
                        }
                    }
                }
            }
        }

        if frame.function.as_str() == Some("?") {
            frame.function.set_value(None);
        }

        if frame.symbol.as_str() == Some("?") {
            frame.symbol.set_value(None);
        }

        if let Some(lines) = frame.pre_context.value_mut() {
            for line in lines.iter_mut() {
                line.get_or_insert_with(String::new);
            }
        }

        if let Some(lines) = frame.post_context.value_mut() {
            for line in lines.iter_mut() {
                line.get_or_insert_with(String::new);
            }
        }

        if frame.context_line.value().is_none()
            && (!frame.pre_context.is_empty() || !frame.post_context.is_empty())
        {
            frame.context_line.set_value(Some(String::new()));
        }

        Ok(())
    });
}

#[cfg(test)]
mod tests {
    use similar_asserts::assert_eq;

    use super::*;

    #[test]
    fn test_coerces_url_filenames() {
        let mut frame = Annotated::new(Frame {
            lineno: Annotated::new(1),
            filename: Annotated::new("http://foo.com/foo.js".into()),
            ..Default::default()
        });

        normalize_non_raw_frame(&mut frame);
        let frame = frame.value().unwrap();

        assert_eq!(frame.filename.value().unwrap().as_str(), "/foo.js");
        assert_eq!(
            frame.abs_path.value().unwrap().as_str(),
            "http://foo.com/foo.js"
        );
    }

    #[test]
    fn test_does_not_overwrite_filename() {
        let mut frame = Annotated::new(Frame {
            lineno: Annotated::new(1),
            filename: Annotated::new("foo.js".into()),
            abs_path: Annotated::new("http://foo.com/foo.js".into()),
            ..Default::default()
        });

        normalize_non_raw_frame(&mut frame);
        let frame = frame.value().unwrap();

        assert_eq!(frame.filename.value().unwrap().as_str(), "foo.js");
        assert_eq!(
            frame.abs_path.value().unwrap().as_str(),
            "http://foo.com/foo.js"
        );
    }

    #[test]
    fn test_ignores_results_with_empty_path() {
        let mut frame = Annotated::new(Frame {
            lineno: Annotated::new(1),
            abs_path: Annotated::new("http://foo.com".into()),
            ..Default::default()
        });

        normalize_non_raw_frame(&mut frame);
        let frame = frame.value().unwrap();

        assert_eq!(frame.filename.value().unwrap().as_str(), "http://foo.com");
        assert_eq!(
            frame.abs_path.value().unwrap().as_str(),
            frame.filename.value().unwrap().as_str()
        );
    }

    #[test]
    fn test_ignores_results_with_slash_path() {
        let mut frame = Annotated::new(Frame {
            lineno: Annotated::new(1),
            abs_path: Annotated::new("http://foo.com/".into()),
            ..Default::default()
        });

        normalize_non_raw_frame(&mut frame);
        let frame = frame.value().unwrap();

        assert_eq!(frame.filename.value().unwrap().as_str(), "http://foo.com/");
        assert_eq!(
            frame.abs_path.value().unwrap().as_str(),
            frame.filename.value().unwrap().as_str()
        );
    }

    #[test]
    fn test_coerce_empty_filename() {
        let mut frame = Annotated::new(Frame {
            lineno: Annotated::new(1),
            filename: Annotated::new("".into()),
            abs_path: Annotated::new("http://foo.com/foo.js".into()),
            ..Default::default()
        });

        normalize_non_raw_frame(&mut frame);
        let frame = frame.value().unwrap();

        assert_eq!(frame.filename.value().unwrap().as_str(), "/foo.js");
        assert_eq!(
            frame.abs_path.value().unwrap().as_str(),
            "http://foo.com/foo.js"
        );
    }

    #[test]
    fn test_is_url() {
        assert!(is_url("http://example.org/"));
        assert!(is_url("https://example.org/"));
        assert!(is_url("file:///tmp/filename"));
        assert!(is_url(
            "applewebdata://00000000-0000-1000-8080-808080808080"
        ));
        assert!(!is_url("app:///index.bundle")); // react native
        assert!(!is_url("webpack:///./app/index.jsx")); // webpack bundle
        assert!(!is_url("data:,"));
        assert!(!is_url("blob:\x00"));
    }
}