4#include "../Common/ResultErrorFormatter.h"
5#include "SerialPortError.h"
15 uint32_t nativeError, Span<char> output)
17 ResultErrorFormatter formatter(output);
20 case SerialPortError::InvalidBaudRate: formatter.append(
"Baud rate must be greater than zero");
break;
21 case SerialPortError::InvalidDataBits: formatter.append(
"Data bits value is invalid");
break;
22 case SerialPortError::InvalidParity: formatter.append(
"Parity value is invalid");
break;
23 case SerialPortError::InvalidStopBits: formatter.append(
"Stop bits value is invalid");
break;
24 case SerialPortError::InvalidFlowControl: formatter.append(
"Flow control value is invalid");
break;
25 case SerialPortError::InvalidHandle: formatter.append(
"Serial descriptor is invalid");
break;
26 case SerialPortError::UnsupportedPathEncoding: formatter.append(
"Serial path encoding is not supported");
break;
27 case SerialPortError::InvalidPath: formatter.append(
"Serial path is invalid");
break;
28 case SerialPortError::PathMustBeAbsolute: formatter.append(
"Serial path must be absolute");
break;
29 case SerialPortError::OpenFailed: formatter.append(
"Failed to open serial port");
break;
30 case SerialPortError::ReadSettingsFailed: formatter.append(
"Failed to read serial settings");
break;
31 case SerialPortError::SetSettingsFailed: formatter.append(
"Failed to set serial settings");
break;
32 case SerialPortError::UnsupportedHardwareFlowControl:
33 formatter.append(
"Hardware flow control is not supported");
35 case SerialPortError::UnsupportedBaudRate: formatter.append(
"Baud rate is not supported");
break;
36 case SerialPortError::UnsupportedDataBits: formatter.append(
"Data bits value is not supported");
break;
37 case SerialPortError::UnsupportedParity: formatter.append(
"Parity value is not supported");
break;
38 case SerialPortError::UnsupportedStopBits: formatter.append(
"Stop bits value is not supported");
break;
39 case SerialPortError::PathTooLong: formatter.append(
"Serial path is too long");
break;
40 default:
return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
43 Span<const char> detailText = {};
46 case SerialPortErrorDetail::None:
break;
47 case SerialPortErrorDetail::PosixOpen: detailText = {
"POSIX open",
sizeof(
"POSIX open") - 1};
break;
48 case SerialPortErrorDetail::PosixReadDescriptorFlags:
49 detailText = {
"POSIX read descriptor flags",
sizeof(
"POSIX read descriptor flags") - 1};
51 case SerialPortErrorDetail::PosixSetDescriptorFlags:
52 detailText = {
"POSIX set descriptor flags",
sizeof(
"POSIX set descriptor flags") - 1};
54 case SerialPortErrorDetail::PosixReadSettings:
55 detailText = {
"POSIX read serial settings",
sizeof(
"POSIX read serial settings") - 1};
57 case SerialPortErrorDetail::PosixSetInputBaudRate:
58 detailText = {
"POSIX set input baud rate",
sizeof(
"POSIX set input baud rate") - 1};
60 case SerialPortErrorDetail::PosixSetOutputBaudRate:
61 detailText = {
"POSIX set output baud rate",
sizeof(
"POSIX set output baud rate") - 1};
63 case SerialPortErrorDetail::PosixSetSettings:
64 detailText = {
"POSIX set serial settings",
sizeof(
"POSIX set serial settings") - 1};
66 case SerialPortErrorDetail::WindowsCreateFile:
67 detailText = {
"Windows CreateFile",
sizeof(
"Windows CreateFile") - 1};
69 case SerialPortErrorDetail::WindowsSetTimeouts:
70 detailText = {
"Windows SetCommTimeouts",
sizeof(
"Windows SetCommTimeouts") - 1};
72 case SerialPortErrorDetail::WindowsReadSettings:
73 detailText = {
"Windows GetCommState",
sizeof(
"Windows GetCommState") - 1};
75 case SerialPortErrorDetail::WindowsSetSettings:
76 detailText = {
"Windows SetCommState",
sizeof(
"Windows SetCommState") - 1};
78 default:
return ResultErrorFormatter::failure(ResultErrorFormatStatus::UnknownError, output);
81 if (not detailText.empty() or nativeError != 0)
83 formatter.append(
" (");
84 if (not detailText.empty())
86 formatter.append(detailText);
88 formatter.append(
", ");
92 formatter.append(
"native error: ");
93 formatter.append(
static_cast<uint64_t
>(nativeError));
95 formatter.append(
")");
97 return formatter.finish();
106 return detail::formatSerialPortErrorWithContext(error, SerialPortErrorDetail::None, 0, output);
113 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
114 if (result.category() != SerialPortResultCategory)
115 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
123 return ResultErrorFormatter::failure(ResultErrorFormatStatus::NotAnError, output);
124 if (result.result.category() != SerialPortResultCategory)
125 return ResultErrorFormatter::failure(ResultErrorFormatStatus::ForeignCategory, output);
126 return detail::formatSerialPortErrorWithContext(
static_cast<SerialPortError>(result.result.errorValue()),
127 result.detail, result.nativeError, output);
SerialPortErrorDetail
Stable lower-level and backend-specific context for a SerialPort error.
Definition SerialPortError.h:38
SerialPortError
Stable error codes returned by the SerialPort library.
Definition SerialPortError.h:13
ResultErrorFormat formatSerialPortError(SerialPortError error, Span< char > output)
Formats a canonical English SerialPort diagnostic into caller-owned storage.
Definition SerialPortErrorFormatter.h:104
SerialPort result retaining optional lower-level and native error details.
Definition SerialPortError.h:60