Skip to main content

astarte_interfaces/
error.rs

1// This file is part of Astarte.
2//
3// Copyright 2023 - 2025 SECO Mind Srl
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// 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//
17// SPDX-License-Identifier: Apache-2.0
18
19//! Errors generated by the interface module.
20
21use crate::{
22    interface::{datastream::object::ObjectError, name::InterfaceNameError, version::VersionError},
23    mapping::MappingError,
24    schema::{Aggregation, InterfaceType, SchemaError},
25};
26
27use super::{interface::validation::VersionChangeError, mapping::endpoint::EndpointError};
28
29/// Error for parsing and validating an interface.
30#[non_exhaustive]
31#[derive(thiserror::Error, Debug)]
32pub enum Error {
33    /// Couldn't parse the interface JSON.
34    #[error("cannot parse interface JSON")]
35    Parse(#[from] serde_json::Error),
36    /// Invalid [`InterfaceJson`](crate::schema::InterfaceJson)
37    #[error("invalid interface JSON schema")]
38    Schema(#[from] SchemaError),
39    /// Invalid [`InterfaceVersion`](crate::interface::version::InterfaceVersion)
40    #[error("invalid interface version")]
41    Version(#[from] VersionError),
42    /// Couldn't parse interface for an invalid mapping
43    #[error("couldn't parse interface for invalid mapping")]
44    Mapping(#[from] MappingError),
45    /// Couldn't parse object datastream interface
46    #[error("couldn't parse object datastream interface")]
47    Object(#[from] ObjectError),
48    /// Couldn't parse interface name.
49    #[error("couldn't parse object datastream interface")]
50    Name(#[from] InterfaceNameError),
51    /// The database retention policy is set to `use_ttl` but the TTL was not specified.
52    #[error("Database retention policy set to `use_ttl` but the TTL was not specified")]
53    MissingTtl,
54    /// Error while parsing the endpoint.
55    #[error("invalid endpoint")]
56    InvalidEndpoint(#[from] EndpointError),
57    /// The interfaced endpoints must all be unique.
58    #[error("duplicate endpoint mapping '{endpoint}' and '{duplicate}'")]
59    DuplicateMapping {
60        /// The existing endpoint.
61        endpoint: String,
62        /// The duplicated endpoint.
63        duplicate: String,
64    },
65    /// The name of the interface was changed.
66    #[error("this version has a different name {name} than the previous version {prev_name}")]
67    NameMismatch {
68        /// The new version name.
69        name: String,
70        /// The previous version name.
71        prev_name: String,
72    },
73    /// Invalid version change.
74    #[error("incompatible interface version")]
75    VersionChange(#[from] VersionChangeError),
76    /// Invalid interface type
77    #[error("couldn't convert interface, expected {exp_type} {exp_aggregation} but got {got_type} {got_aggregation}")]
78    InterfaceConversion {
79        /// The interface type we expected.
80        exp_type: InterfaceType,
81        /// The aggregation we expected.
82        exp_aggregation: Aggregation,
83        /// The actual interface type.
84        got_type: InterfaceType,
85        /// The actual aggregation.
86        got_aggregation: Aggregation,
87    },
88    /// Unsupported interface type.
89    ///
90    // Yeh we wished.
91    #[error("property object are supported")]
92    PropertyObject,
93}