#![deny(missing_docs)]
use alloc::boxed::Box;
use alloc::string::String;
#[cfg(feature = "wasm-bindgen")]
use alloc::string::ToString;
#[cfg(target_os = "android")]
use alloc::sync::Arc;
use core::num::ParseIntError;
use thiserror::Error;
use crate::op::Metadata;
use crate::serialize::binary::DecodeError;
pub(crate) type ProtoResult<T> = ::core::result::Result<T, ProtoError>;
#[derive(Clone, Debug, Error)]
#[non_exhaustive]
pub enum ProtoError {
#[non_exhaustive]
#[error("char data length exceeds {max}: {len}")]
CharacterDataTooLong {
max: usize,
len: usize,
},
#[error("crypto error: {0}")]
#[cfg(feature = "__dnssec")]
Crypto(&'static str),
#[error("decoding error: {0}")]
Decode(#[from] DecodeError),
#[error("message format error: {error}")]
FormError {
header: Metadata,
error: Box<Self>,
},
#[error("maximum buffer size exceeded: {0}")]
MaxBufferSizeExceeded(usize),
#[error("{0}")]
Message(&'static str),
#[error("{0}")]
Msg(String),
#[non_exhaustive]
#[error("not all records could be written, wrote: {count}")]
NotAllRecordsWritten {
count: usize,
},
#[error("response received with incorrect QR flag")]
NotAResponse,
#[error("url parsing error")]
UrlParsing(#[from] url::ParseError),
#[error("error parsing utf8 string")]
Utf8(#[from] core::str::Utf8Error),
#[error("error parsing utf8 string")]
FromUtf8(#[from] alloc::string::FromUtf8Error),
#[error("error parsing int")]
ParseInt(#[from] ParseIntError),
#[cfg(target_os = "android")]
#[error("JNI call error: {0}")]
Jni(Arc<jni::errors::Error>),
}
impl From<String> for ProtoError {
fn from(msg: String) -> Self {
Self::Msg(msg)
}
}
impl From<&'static str> for ProtoError {
fn from(msg: &'static str) -> Self {
Self::Message(msg)
}
}
#[cfg(target_os = "android")]
impl From<jni::errors::Error> for ProtoError {
fn from(e: jni::errors::Error) -> Self {
ProtoError::Jni(Arc::new(e))
}
}
#[cfg(feature = "wasm-bindgen")]
impl From<ProtoError> for wasm_bindgen_crate::JsValue {
fn from(e: ProtoError) -> Self {
js_sys::Error::new(&e.to_string()).into()
}
}