Skip to main content

traci_rs/
error.rs

1// SPDX-License-Identifier: EPL-2.0
2//! Error types for the `sumo-traci` crate.
3
4use std::{fmt, io};
5
6/// All errors that can be returned by the TraCI client.
7#[derive(Debug)]
8pub enum TraciError {
9    /// A TCP/IP I/O error occurred (connect, send, receive, …).
10    Connection(io::Error),
11
12    /// The server responded with a protocol-level error
13    /// (wrong command id, bad message length, unexpected type tag, …).
14    Protocol(String),
15
16    /// The SUMO server returned `RTYPE_ERR` for a command we sent.
17    SimulationError(String),
18
19    /// The SUMO server replied that the requested command is not implemented.
20    NotImplemented(String),
21
22    /// SUMO has reached the configured end time and closed the simulation.
23    /// Returned by [`TraciClient::simulation_step`] when `CMD_CLOSE` is received.
24    SimulationEnd,
25}
26
27impl fmt::Display for TraciError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            TraciError::Connection(e) => write!(f, "TraCI connection error: {e}"),
31            TraciError::Protocol(msg) => write!(f, "TraCI protocol error: {msg}"),
32            TraciError::SimulationError(msg) => write!(f, "TraCI simulation error: {msg}"),
33            TraciError::NotImplemented(msg) => write!(f, "TraCI command not implemented: {msg}"),
34            TraciError::SimulationEnd => write!(f, "SUMO simulation ended"),
35        }
36    }
37}
38
39impl std::error::Error for TraciError {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        match self {
42            TraciError::Connection(e) => Some(e),
43            _ => None,
44        }
45    }
46}
47
48impl From<io::Error> for TraciError {
49    fn from(e: io::Error) -> Self {
50        TraciError::Connection(e)
51    }
52}