tremor_codec/
errors.rs

1// Copyright 2020-2021, The Tremor 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
15//NOTE: error_chain
16#![allow(deprecated, missing_docs, clippy::large_enum_variant)]
17
18use error_chain::error_chain;
19use value_trait::prelude::*;
20
21pub type Kind = ErrorKind;
22
23impl From<TryTypeError> for Error {
24    fn from(e: TryTypeError) -> Self {
25        ErrorKind::TypeError(e.expected, e.got).into()
26    }
27}
28
29#[cfg(test)]
30impl PartialEq for Error {
31    fn eq(&self, _other: &Self) -> bool {
32        // This might be Ok since we try to compare Result in tests
33        false
34    }
35}
36
37// TODO: This is a workaround for the fact that `error_chain` does not have sync send errors,
38// this is a temporary solution until we can replace `error_chain` with `anyhow`
39unsafe impl Sync for Error {}
40unsafe impl Send for Error {}
41
42error_chain! {
43    foreign_links {
44        CsvError(csv::Error);
45        DateTimeParseError(chrono::ParseError);
46        FromUtf8Error(std::string::FromUtf8Error);
47        InfluxEncoderError(tremor_influx::EncoderError);
48        Io(std::io::Error);
49        JsonAccessError(value_trait::AccessError);
50        JsonError(simd_json::Error);
51        MsgPackDecoderError(rmp_serde::decode::Error);
52        MsgPackEncoderError(rmp_serde::encode::Error);
53        ReqwestError(reqwest::Error);
54        InvalidHeaderName(reqwest::header::InvalidHeaderName);
55        TryFromIntError(std::num::TryFromIntError);
56        ValueError(tremor_value::Error);
57        Utf8Error(std::str::Utf8Error);
58        YamlError(serde_yaml::Error) #[doc = "Error during yaml parsing"];
59        Uuid(uuid::Error);
60        Lexical(lexical::Error);
61        SimdUtf8(simdutf8::basic::Utf8Error);
62        TremorCodec(crate::codec::tremor::Error);
63        AvroError(apache_avro::Error);
64        UrlParseError(tremor_common::url::ParseError);
65        SRCError(schema_registry_converter::error::SRCError);
66    }
67
68    errors {
69        TypeError(expected: ValueType, found: ValueType) {
70            description("Type error")
71                display("Type error: Expected {}, found {}", expected, found)
72        }
73
74        CodecNotFound(name: String) {
75            description("Codec not found")
76                display("Codec \"{}\" not found.", name)
77        }
78
79        NotCSVSerializableValue(value: String) {
80            description("The value cannot be serialized to CSV. Expected an array.")
81            display("The value {} cannot be serialized to CSV. Expected an array.", value)
82        }
83
84        InvalidStatsD {
85            description("Invalid statsd metric")
86                display("Invalid statsd metric")
87        }
88
89        InvalidGraphitePlaintext {
90            description("Invalid graphite plaintext protocol metric")
91                display("Invalid graphite plaintext protocol metric")
92        }
93
94        InvalidDogStatsD {
95            description("Invalid dogstatsd metric")
96                display("Invalid dogstatsd metric")
97        }
98        InvalidInfluxData(s: String, e: tremor_influx::DecoderError) {
99            description("Invalid Influx Line Protocol data")
100                display("Invalid Influx Line Protocol data: {}\n{}", e, s)
101        }
102        InvalidBInfluxData(s: String) {
103            description("Invalid BInflux Line Protocol data")
104                display("Invalid BInflux Line Protocol data: {}", s)
105        }
106        InvalidSyslogData(s: &'static str) {
107            description("Invalid Syslog Protocol data")
108                display("Invalid Syslog Protocol data: {}", s)
109        }
110    }
111}
112
113#[cfg(test)]
114mod test {
115    use super::*;
116
117    #[test]
118    fn test_type_error() {
119        let r = Error::from(TryTypeError {
120            expected: ValueType::Object,
121            got: ValueType::String,
122        })
123        .0;
124        matches!(
125            r,
126            ErrorKind::TypeError(ValueType::Object, ValueType::String)
127        );
128    }
129}