1#![deny(missing_docs)]
11
12use alloc::boxed::Box;
13use alloc::string::String;
14#[cfg(feature = "wasm-bindgen")]
15use alloc::string::ToString;
16#[cfg(target_os = "android")]
17use alloc::sync::Arc;
18use core::num::ParseIntError;
19
20use thiserror::Error;
21
22use crate::op::Metadata;
23use crate::serialize::binary::DecodeError;
24
25pub(crate) type ProtoResult<T> = ::core::result::Result<T, ProtoError>;
27
28#[derive(Clone, Debug, Error)]
30#[non_exhaustive]
31pub enum ProtoError {
32 #[non_exhaustive]
34 #[error("char data length exceeds {max}: {len}")]
35 CharacterDataTooLong {
36 max: usize,
38 len: usize,
40 },
41
42 #[error("crypto error: {0}")]
44 #[cfg(feature = "__dnssec")]
45 Crypto(&'static str),
46
47 #[error("decoding error: {0}")]
49 Decode(#[from] DecodeError),
50
51 #[error("message format error: {error}")]
53 FormError {
54 header: Metadata,
56 error: Box<Self>,
58 },
59
60 #[error("maximum buffer size exceeded: {0}")]
62 MaxBufferSizeExceeded(usize),
63
64 #[error("{0}")]
66 Message(&'static str),
67
68 #[error("{0}")]
70 Msg(String),
71
72 #[non_exhaustive]
74 #[error("not all records could be written, wrote: {count}")]
75 NotAllRecordsWritten {
76 count: usize,
78 },
79
80 #[error("response received with incorrect QR flag")]
82 NotAResponse,
83
84 #[error("url parsing error")]
86 UrlParsing(#[from] url::ParseError),
87
88 #[error("error parsing utf8 string")]
90 Utf8(#[from] core::str::Utf8Error),
91
92 #[error("error parsing utf8 string")]
94 FromUtf8(#[from] alloc::string::FromUtf8Error),
95
96 #[error("error parsing int")]
98 ParseInt(#[from] ParseIntError),
99
100 #[cfg(target_os = "android")]
102 #[error("JNI call error: {0}")]
103 Jni(Arc<jni::errors::Error>),
104}
105
106impl From<String> for ProtoError {
107 fn from(msg: String) -> Self {
108 Self::Msg(msg)
109 }
110}
111
112impl From<&'static str> for ProtoError {
113 fn from(msg: &'static str) -> Self {
114 Self::Message(msg)
115 }
116}
117
118#[cfg(target_os = "android")]
119impl From<jni::errors::Error> for ProtoError {
120 fn from(e: jni::errors::Error) -> Self {
121 ProtoError::Jni(Arc::new(e))
122 }
123}
124
125#[cfg(feature = "wasm-bindgen")]
126impl From<ProtoError> for wasm_bindgen_crate::JsValue {
127 fn from(e: ProtoError) -> Self {
128 js_sys::Error::new(&e.to_string()).into()
129 }
130}