common_decimal/
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 bigdecimal::BigDecimal;
16use common_error::ext::ErrorExt;
17use common_error::status_code::StatusCode;
18use common_macro::stack_trace_debug;
19use snafu::{Location, Snafu};
20
21#[derive(Snafu)]
22#[snafu(visibility(pub))]
23#[stack_trace_debug]
24pub enum Error {
25    #[snafu(display("Decimal out of range, decimal value: {}", value))]
26    BigDecimalOutOfRange {
27        value: BigDecimal,
28        #[snafu(implicit)]
29        location: Location,
30    },
31
32    #[snafu(display("Failed to parse string to rust decimal, raw: {}", raw))]
33    ParseRustDecimalStr {
34        raw: String,
35        #[snafu(source)]
36        error: rust_decimal::Error,
37    },
38
39    #[snafu(display("Failed to parse string to big decimal, raw: {}", raw))]
40    ParseBigDecimalStr {
41        raw: String,
42        #[snafu(source)]
43        error: bigdecimal::ParseBigDecimalError,
44    },
45
46    #[snafu(display("Invalid precision or scale, resion: {}", reason))]
47    InvalidPrecisionOrScale {
48        reason: String,
49        #[snafu(implicit)]
50        location: Location,
51    },
52}
53
54impl ErrorExt for Error {
55    fn status_code(&self) -> StatusCode {
56        match self {
57            Error::BigDecimalOutOfRange { .. }
58            | Error::ParseRustDecimalStr { .. }
59            | Error::InvalidPrecisionOrScale { .. }
60            | Error::ParseBigDecimalStr { .. } => StatusCode::InvalidArguments,
61        }
62    }
63
64    fn as_any(&self) -> &dyn std::any::Any {
65        self
66    }
67}
68
69pub type Result<T> = std::result::Result<T, Error>;