cat_dev/mion/proto/cgis/
errors.rs

1//! Errors related to CGI, and HTML pages for the MION.
2
3use bytes::Bytes;
4use mac_address::MacParseError;
5use miette::Diagnostic;
6use serde_urlencoded::ser::Error as SerdeUrlEncodeError;
7use std::{net::AddrParseError, num::ParseIntError};
8use thiserror::Error;
9
10/// Errors related to handling and dealing with the HTML CGI pages.
11#[derive(Debug, Diagnostic, Error, PartialEq, Eq)]
12pub enum MIONCGIErrors {
13	/// See [`serde_urlencoded::ser::Error`] for details.
14	#[error("Failed to encode data as form data: {0}")]
15	#[diagnostic(code(cat_dev::net::parse::mion::cgi::encode::form_data_error))]
16	FormDataEncodeError(#[from] SerdeUrlEncodeError),
17	/// The HTML response we got was expected to contain hexadecimal bytes.
18	///
19	/// We could not parse one of these hexadecimal bytes. Either the device
20	/// responded with an error we didn't properly pick up on, or we got corrupt
21	/// data somehow.,
22	#[error("Could not parse byte from memory dump: {0}")]
23	#[diagnostic(code(cat_dev::net::parse::mion::cgi::bad_memory_byte))]
24	HtmlResponseBadByte(String),
25	/// We expected this HTML response to have an IP encoded as a string, but we
26	/// could not parse the string as an IP.
27	#[error("Expected HTML Response to have an IP as a string, but could not parse: `{0:?}`")]
28	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_ip_encoding_error))]
29	HtmlResponseIpExpectedButNotThere(AddrParseError),
30	/// We expected this HTML response to have a MAC Address encoded as a string,
31	/// but we could not parse the string as a MAC address.
32	#[error("Expected HTML Response to have a MAC as a string, but could not parse: `{0:?}`")]
33	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_mac_encoding_error))]
34	HtmlResponseMacExpectedButNotThere(MacParseError),
35	/// We could not find the `<body>` tags in a page that is supposed to return
36	/// HTML.
37	#[error("Could not parse HTML response could not find one of the body tags: `<body>`, or `</body>`: {0}")]
38	#[diagnostic(code(cat_dev::net::parse::mion::cgi::no_body_tag))]
39	HtmlResponseMissingBody(String),
40	#[error("Expected to find closing tag: {0}, in the rest of the HTML Body: {1}")]
41	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_missing_closing_tag))]
42	HtmlResponseMissingClosingTag(String, String),
43	#[error("Could not find Memory Dump Table Body, failed to find sigils: {0}")]
44	#[diagnostic(code(cat_dev::net::parse::mion::cgi::no_mem_dump_sigil))]
45	HtmlResponseMissingMemoryDumpSigil(String),
46	/// We attempted to find an `<input>` element with a certain name in the
47	/// HTML response, but were not able to find one.
48	#[error("Could not find input with name: `{0}`, within HTML body: `{1}`")]
49	#[diagnostic(code(cat_dev::net::parse::mion::cgi::missing_tagged_input))]
50	HtmlResponseMissingTaggedInput(String, String),
51	#[error("Expected to find a string to help identify the version in the HTML ({0}) as part of the string ({1}), but did not find one.")]
52	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_missing_version_prefix))]
53	HtmlResponseMissingVersionPart(String, String),
54	#[error("When fetching the versions of the MION we expect to find both the FW version, and the FPGA version, but only found the following versions: {0:?}")]
55	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_missing_versions))]
56	HtmlResponseMissingVersions(Vec<String>),
57	/// We expected the HTML response to have one radio box checked out of all
58	/// the radio boxes, but we found no radio boxes that were checked.
59	#[error("Expected HTML Response to have a radio button, could not parse: `{0}`")]
60	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_radio_wasnt_checked))]
61	HtmlResponseNoRadioChecked(String),
62	/// We expected to find an item in a table `<tr>`/`<td>`, but were not able
63	/// to find one in the HTML response we got back.
64	#[error("Expected HTML Response to have a table item with prefix: {1}, but couldn't find one in: `{0}`")]
65	HtmlResponseNoTableItemWithPrefix(String, String),
66	/// We expected this HTML response to have a number encoded as a string, but we
67	/// could not parse the string as a number.
68	#[error("Expected HTML Response to have an number as a string, but could not parse: `{0:?}`")]
69	#[diagnostic(code(cat_dev::net::parse::mion::cgi::html_response_number_encoding_error))]
70	HtmlResponseNumberExpectedButNotThere(ParseIntError),
71	/// We got an unexpected status code from the CAT-DEV, it also came with an
72	/// HTTP body that may contain clues to it's error.
73	#[error("Got an unexpected status code that wasn't successful over HTTP: {0}, Body: {1:02x?}")]
74	#[diagnostic(code(cat_dev::net::parse::mion::cgi::status_code))]
75	UnexpectedStatusCode(u16, Bytes),
76	/// We got an unexpected status code from the CAT-DEV, this is only used when
77	/// we did not get an HTTP body back from the CAT-DEV as well.
78	#[error("Got an unexpected status code that wasn't successful over HTTP: {0}")]
79	#[diagnostic(code(cat_dev::net::parse::http::bad_status_code_without_body))]
80	UnexpectedStatusCodeNoBody(u16),
81}