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: 400..=499 } => Some("status_code_4xx"),
64 UploadResult::HttpStatus { code: 500..=599 } => Some("status_code_5xx"),
65 UploadResult::HttpStatus { .. } => Some("status_code_unknown"),
66 UploadResult::UnrecoverableFailure { .. } => Some("unrecoverable"),
67 UploadResult::RecoverableFailure { .. } => Some("recoverable"),
68 UploadResult::Incapable { .. } => Some("incapable"),
69 UploadResult::Done { .. } => None,
70 }
71 }
72
73 /// A recoverable failure.
74 ///
75 /// During upload something went wrong,
76 /// e.g. the network connection failed.
77 /// The upload should be retried at a later time.
78 pub fn recoverable_failure() -> Self {
79 Self::RecoverableFailure { unused: 0 }
80 }
81
82 /// An unrecoverable upload failure.
83 ///
84 /// A possible cause might be a malformed URL.
85 pub fn unrecoverable_failure() -> Self {
86 Self::UnrecoverableFailure { unused: 0 }
87 }
88
89 /// The uploader is not capable of uploading this request due to lack of or
90 /// mismatched capabilities.
91 ///
92 /// e.g. The ping requires upload over OHTTP, but the uploader doesn't support OHTTP.
93 pub fn incapable() -> Self {
94 Self::Incapable { unused: 0 }
95 }
96
97 /// A HTTP response code.
98 ///
99 /// This can still indicate an error, depending on the status code.
100 pub fn http_status(code: i32) -> Self {
101 Self::HttpStatus { code }
102 }
103
104 /// This uploader is done.
105 pub fn done() -> Self {
106 Self::Done { unused: 0 }
107 }
108}
109
110/// Communication back whether the uploader loop should continue.
111#[derive(Debug)]
112pub enum UploadTaskAction {
113 /// Instruct the caller to continue with work.
114 Next,
115 /// Instruct the caller to end work.
116 End,
117}