risingwave_meta/model/
error.rs

1// Copyright 2025 RisingWave Labs
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 anyhow::anyhow;
16use risingwave_pb::PbFieldNotFound;
17use risingwave_rpc_client::error::ToTonicStatus;
18use thiserror::Error;
19
20pub type MetadataModelResult<T> = std::result::Result<T, MetadataModelError>;
21
22#[derive(Error, Debug)]
23pub enum MetadataModelError {
24    #[error("Pb decode error: {0}")]
25    PbDecode(#[from] prost::DecodeError),
26
27    #[error(transparent)]
28    InternalError(
29        #[from]
30        #[backtrace]
31        anyhow::Error,
32    ),
33}
34
35impl From<PbFieldNotFound> for MetadataModelError {
36    fn from(p: PbFieldNotFound) -> Self {
37        MetadataModelError::InternalError(anyhow::anyhow!(
38            "Failed to decode prost: field not found `{}`",
39            p.0
40        ))
41    }
42}
43
44impl From<MetadataModelError> for tonic::Status {
45    fn from(e: MetadataModelError) -> Self {
46        e.to_status(tonic::Code::Internal, "meta")
47    }
48}
49
50impl MetadataModelError {
51    pub fn internal(msg: impl ToString) -> Self {
52        MetadataModelError::InternalError(anyhow!(msg.to_string()))
53    }
54}