common_mem_prof/jemalloc/
error.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::Any;
16use std::path::PathBuf;
17
18use common_error::ext::{BoxedError, ErrorExt};
19use common_error::status_code::StatusCode;
20use common_macro::stack_trace_debug;
21use snafu::{Location, Snafu};
22
23#[derive(Snafu)]
24#[snafu(visibility(pub))]
25#[stack_trace_debug]
26pub enum Error {
27    #[snafu(display("Failed to read OPT_PROF"))]
28    ReadOptProf {
29        #[snafu(source)]
30        error: tikv_jemalloc_ctl::Error,
31    },
32
33    #[snafu(display("Memory profiling is not enabled"))]
34    ProfilingNotEnabled,
35
36    #[snafu(display("Failed to build temp file from given path: {:?}", path))]
37    BuildTempPath {
38        path: PathBuf,
39        #[snafu(implicit)]
40        location: Location,
41    },
42
43    #[snafu(display("Failed to open temp file: {}", path))]
44    OpenTempFile {
45        path: String,
46        #[snafu(source)]
47        error: std::io::Error,
48    },
49
50    #[snafu(display("Failed to dump profiling data to temp file: {:?}", path))]
51    DumpProfileData {
52        path: PathBuf,
53        #[snafu(source)]
54        error: tikv_jemalloc_ctl::Error,
55    },
56}
57
58impl ErrorExt for Error {
59    fn status_code(&self) -> StatusCode {
60        match self {
61            Error::ReadOptProf { .. } => StatusCode::Internal,
62            Error::ProfilingNotEnabled => StatusCode::InvalidArguments,
63            Error::BuildTempPath { .. } => StatusCode::Internal,
64            Error::OpenTempFile { .. } => StatusCode::StorageUnavailable,
65            Error::DumpProfileData { .. } => StatusCode::StorageUnavailable,
66        }
67    }
68
69    fn as_any(&self) -> &dyn Any {
70        self
71    }
72}
73
74impl From<Error> for crate::error::Error {
75    fn from(e: Error) -> Self {
76        Self::Internal {
77            source: BoxedError::new(e),
78        }
79    }
80}