milvus/
error.rs

1// Licensed to the LF AI & Data foundation under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16// For custom error handling
17// TODO
18
19use crate::collection::Error as CollectionError;
20use crate::proto::common::{ErrorCode, Status};
21use crate::schema::Error as SchemaError;
22use std::result;
23use thiserror::Error;
24use tonic::transport::Error as CommError;
25use tonic::Status as GrpcError;
26
27#[derive(Debug, Error)]
28pub enum Error {
29    #[error("{0:?}")]
30    Communication(#[from] CommError),
31
32    #[error("{0:?}")]
33    Collection(#[from] CollectionError),
34
35    #[error("{0:?}")]
36    Grpc(#[from] GrpcError),
37
38    #[error("{0:?}")]
39    Schema(#[from] SchemaError),
40
41    #[error("{0:?} {1:?}")]
42    Server(ErrorCode, String),
43
44    #[error("{0:?}")]
45    ProstEncode(#[from] prost::EncodeError),
46
47    #[error("{0:?}")]
48    ProstDecode(#[from] prost::DecodeError),
49    
50    #[error("Conversion error")]
51    Conversion,
52    #[error("{0:?}")]
53    SerdeJsonErr(#[from] serde_json::Error),
54
55    #[error("parameter {0:?} with invalid value {1:?}")]
56    InvalidParameter(String, String),
57
58    #[error("{0:?}")]
59    Other(#[from] anyhow::Error),
60
61    #[error("{0}")]
62    Unexpected(String),
63}
64
65impl From<Status> for Error {
66    fn from(s: Status) -> Self {
67        Error::Server(ErrorCode::from_i32(s.error_code).unwrap(), s.reason)
68    }
69}
70
71pub type Result<T> = result::Result<T, Error>;