tokio_modbus/
error.rs

1// SPDX-FileCopyrightText: Copyright (c) 2017-2025 slowtec GmbH <post@slowtec.de>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Error types.
5
6use thiserror::Error;
7
8use crate::{ExceptionResponse, FunctionCode, Response};
9
10/// Protocol or transport errors.
11///
12/// Devices that don't implement the _Modbus_ protocol correctly
13/// or network issues can cause these errors.
14#[derive(Debug, Error)]
15pub enum Error {
16    #[error(transparent)]
17    Protocol(#[from] ProtocolError),
18    #[error(transparent)]
19    Transport(#[from] std::io::Error),
20}
21
22/// _Modbus_ protocol error.
23#[derive(Debug, Error)]
24pub enum ProtocolError {
25    /// The received response header doesn't match the request.
26    ///
27    /// The error message contains details about the mismatch.
28    ///
29    /// The result received from the server is included for further analysis and handling.
30    #[error("mismatching headers: {message} {result:?}")]
31    HeaderMismatch {
32        message: String,
33        result: Result<Response, ExceptionResponse>,
34    },
35
36    /// The received response function code doesn't match the request.
37    ///
38    /// The result received from the server is included for further analysis and handling.
39    #[error("mismatching function codes: {request} {result:?}")]
40    FunctionCodeMismatch {
41        request: FunctionCode,
42        result: Result<Response, ExceptionResponse>,
43    },
44}