Skip to main content

glean_core/upload/
result.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/// The result of an attempted ping upload.
6#[derive(Debug)]
7pub enum UploadResult {
8    /// A recoverable failure.
9    ///
10    /// During upload something went wrong,
11    /// e.g. the network connection failed.
12    /// The upload should be retried at a later time.
13    RecoverableFailure {
14        #[doc(hidden)]
15        /// Unused field. Required because UniFFI can't handle variants without fields.
16        unused: i8,
17    },
18
19    /// An unrecoverable upload failure.
20    ///
21    /// A possible cause might be a malformed URL.
22    UnrecoverableFailure {
23        #[doc(hidden)]
24        /// Unused field. Required because UniFFI can't handle variants without fields.
25        unused: i8,
26    },
27
28    /// The uploader is not capable of uploading this request due to lack of or
29    /// mismatched capabilities.
30    ///
31    /// e.g. The ping requires upload over OHTTP, but the uploader doesn't support OHTTP.
32    Incapable {
33        #[doc(hidden)]
34        /// Unused field. Required because UniFFI can't handle variants without fields.
35        unused: i8,
36    },
37
38    /// A HTTP response code.
39    ///
40    /// This can still indicate an error, depending on the status code.
41    HttpStatus {
42        /// The HTTP status code
43        code: i32,
44    },
45
46    /// Signal that this uploader is done with work
47    /// and won't accept new work.
48    Done {
49        #[doc(hidden)]
50        /// Unused field. Required because UniFFI can't handle variants without fields.
51        unused: i8,
52    },
53}
54
55impl UploadResult {
56    /// Gets the label to be used in recording error counts for upload.
57    ///
58    /// Returns `None` if the upload finished succesfully.
59    /// Failures are recorded in the `ping_upload_failure` metric.
60    pub fn get_label(&self) -> Option<&str> {
61        match self {
62            UploadResult::HttpStatus { code: 200..=299 } => None,
63            UploadResult::HttpStatus { code: 408 } => Some("status_code_408"),
64            UploadResult::HttpStatus { code: 429 } => Some("status_code_429"),
65            UploadResult::HttpStatus { code: 400..=499 } => Some("status_code_4xx"),
66            UploadResult::HttpStatus { code: 500..=599 } => Some("status_code_5xx"),
67            UploadResult::HttpStatus { .. } => Some("status_code_unknown"),
68            UploadResult::UnrecoverableFailure { .. } => Some("unrecoverable"),
69            UploadResult::RecoverableFailure { .. } => Some("recoverable"),
70            UploadResult::Incapable { .. } => Some("incapable"),
71            UploadResult::Done { .. } => None,
72        }
73    }
74
75    /// A recoverable failure.
76    ///
77    /// During upload something went wrong,
78    /// e.g. the network connection failed.
79    /// The upload should be retried at a later time.
80    pub fn recoverable_failure() -> Self {
81        Self::RecoverableFailure { unused: 0 }
82    }
83
84    /// An unrecoverable upload failure.
85    ///
86    /// A possible cause might be a malformed URL.
87    pub fn unrecoverable_failure() -> Self {
88        Self::UnrecoverableFailure { unused: 0 }
89    }
90
91    /// The uploader is not capable of uploading this request due to lack of or
92    /// mismatched capabilities.
93    ///
94    /// e.g. The ping requires upload over OHTTP, but the uploader doesn't support OHTTP.
95    pub fn incapable() -> Self {
96        Self::Incapable { unused: 0 }
97    }
98
99    /// A HTTP response code.
100    ///
101    /// This can still indicate an error, depending on the status code.
102    pub fn http_status(code: i32) -> Self {
103        Self::HttpStatus { code }
104    }
105
106    /// This uploader is done.
107    pub fn done() -> Self {
108        Self::Done { unused: 0 }
109    }
110}
111
112/// Communication back whether the uploader loop should continue.
113#[derive(Debug)]
114pub enum UploadTaskAction {
115    /// Instruct the caller to continue with work.
116    Next,
117    /// Instruct the caller to end work.
118    End,
119}