archivist_lib/
error.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
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::logs::error::LogsError;
use fidl::prelude::*;
use fidl_fuchsia_diagnostics::BatchIteratorControlHandle;
use thiserror::Error;
use tracing::warn;
use zx_status::Status as ZxStatus;

#[derive(Debug, Error)]
pub enum Error {
    #[error(transparent)]
    Logs(#[from] LogsError),

    #[error("Failed to serve outgoing dir: {0}")]
    ServeOutgoing(#[source] anyhow::Error),

    #[error(transparent)]
    Inspect(#[from] fuchsia_inspect::Error),

    #[error("Encountered a diagnostics data repository node with more than one artifact container. {0:?}")]
    MultipleArtifactContainers(Vec<String>),

    #[error(transparent)]
    Hierarchy(#[from] diagnostics_hierarchy::Error),

    #[error(transparent)]
    Selectors(#[from] anyhow::Error),
}

#[derive(Debug, Error)]
pub enum AccessorError {
    #[error("data_type must be set")]
    MissingDataType,

    #[error("client_selector_configuration must be set")]
    MissingSelectors,

    #[error("no selectors were provided")]
    EmptySelectors,

    #[error("requested selectors are unsupported: {}", .0)]
    InvalidSelectors(&'static str),

    #[error("couldn't parse/validate the provided selectors: {}", .0)]
    ParseSelectors(#[from] selectors::Error),

    #[error("only selectors of type `component:root` are supported for logs at the moment")]
    InvalidLogSelector,

    #[error("format must be set")]
    MissingFormat,

    #[error("only JSON supported right now")]
    UnsupportedFormat,

    #[error("stream_mode must be set")]
    MissingMode,

    #[error("only snapshot supported right now")]
    UnsupportedMode,

    #[error("IPC failure")]
    Ipc {
        #[from]
        source: fidl::Error,
    },

    #[error("Unable to create a VMO -- extremely unusual!")]
    VmoCreate(#[source] ZxStatus),

    #[error("Unable to write to VMO -- we may be OOMing")]
    VmoWrite(#[source] ZxStatus),

    #[error("Unable to get VMO size -- extremely unusual")]
    VmoSize(#[source] ZxStatus),

    #[error("JSON serialization failure: {0}")]
    Serialization(#[from] serde_json::Error),

    #[error("CBOR serialization failure: {0}")]
    CborSerialization(#[from] serde_cbor::Error),

    #[error("batch timeout was set on StreamParameter and on PerformanceConfiguration")]
    DuplicateBatchTimeout,

    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

impl AccessorError {
    pub fn close(self, control: BatchIteratorControlHandle) {
        warn!(error = %self, "Closing BatchIterator.");
        let epitaph = match self {
            AccessorError::DuplicateBatchTimeout
            | AccessorError::MissingDataType
            | AccessorError::EmptySelectors
            | AccessorError::MissingSelectors
            | AccessorError::InvalidSelectors(_)
            | AccessorError::InvalidLogSelector
            | AccessorError::ParseSelectors(_) => ZxStatus::INVALID_ARGS,
            AccessorError::VmoCreate(status)
            | AccessorError::VmoWrite(status)
            | AccessorError::VmoSize(status) => status,
            AccessorError::MissingFormat | AccessorError::MissingMode => ZxStatus::INVALID_ARGS,
            AccessorError::UnsupportedFormat | AccessorError::UnsupportedMode => {
                ZxStatus::WRONG_TYPE
            }
            AccessorError::Serialization { .. } => ZxStatus::BAD_STATE,
            AccessorError::CborSerialization { .. } => ZxStatus::BAD_STATE,
            AccessorError::Ipc { .. } | AccessorError::Io(_) => ZxStatus::IO,
        };
        control.shutdown_with_epitaph(epitaph);
    }
}