iceberg_catalog_hms/
error.rs

1// Licensed to the Apache Software Foundation (ASF) 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,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::io;
20
21use anyhow::anyhow;
22use iceberg::{Error, ErrorKind};
23use volo_thrift::MaybeException;
24
25/// Format a thrift error into iceberg error.
26///
27/// Please only throw this error when you are sure that the error is caused by thrift.
28pub fn from_thrift_error(error: impl std::error::Error) -> Error {
29    Error::new(
30        ErrorKind::Unexpected,
31        "Operation failed for hitting thrift error".to_string(),
32    )
33    .with_source(anyhow!("thrift error: {error:?}"))
34}
35
36/// Format a thrift exception into iceberg error.
37pub fn from_thrift_exception<T, E: Debug>(value: MaybeException<T, E>) -> Result<T, Error> {
38    match value {
39        MaybeException::Ok(v) => Ok(v),
40        MaybeException::Exception(err) => Err(Error::new(
41            ErrorKind::Unexpected,
42            "Operation failed for hitting thrift error".to_string(),
43        )
44        .with_source(anyhow!("thrift error: {err:?}"))),
45    }
46}
47
48/// Format an io error into iceberg error.
49pub fn from_io_error(error: io::Error) -> Error {
50    Error::new(
51        ErrorKind::Unexpected,
52        "Operation failed for hitting io error".to_string(),
53    )
54    .with_source(error)
55}