Skip to main content

hickory_proto/
error.rs

1// Copyright 2015-2020 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! Error types for the crate
9
10#![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
25/// An alias for results returned by functions of this crate
26pub(crate) type ProtoResult<T> = ::core::result::Result<T, ProtoError>;
27
28/// The error kind for errors that get returned in the crate
29#[derive(Clone, Debug, Error)]
30#[non_exhaustive]
31pub enum ProtoError {
32    /// Character data length exceeded the limit
33    #[non_exhaustive]
34    #[error("char data length exceeds {max}: {len}")]
35    CharacterDataTooLong {
36        /// Specified maximum
37        max: usize,
38        /// Actual length
39        len: usize,
40    },
41
42    /// Crypto operation failed
43    #[error("crypto error: {0}")]
44    #[cfg(feature = "__dnssec")]
45    Crypto(&'static str),
46
47    /// Message decoding error
48    #[error("decoding error: {0}")]
49    Decode(#[from] DecodeError),
50
51    /// Format error in Message Parsing
52    #[error("message format error: {error}")]
53    FormError {
54        /// Header of the bad Message
55        header: Metadata,
56        /// Error that occurred while parsing the Message
57        error: Box<Self>,
58    },
59
60    /// The maximum buffer size was exceeded
61    #[error("maximum buffer size exceeded: {0}")]
62    MaxBufferSizeExceeded(usize),
63
64    /// An error with an arbitrary message, referenced as &'static str
65    #[error("{0}")]
66    Message(&'static str),
67
68    /// An error with an arbitrary message, stored as String
69    #[error("{0}")]
70    Msg(String),
71
72    /// Not all records were able to be written
73    #[non_exhaustive]
74    #[error("not all records could be written, wrote: {count}")]
75    NotAllRecordsWritten {
76        /// Number of records that were written before the error
77        count: usize,
78    },
79
80    /// A response was received with QR=0, indicating it was a query and not a response
81    #[error("response received with incorrect QR flag")]
82    NotAResponse,
83
84    /// An url parsing error
85    #[error("url parsing error")]
86    UrlParsing(#[from] url::ParseError),
87
88    /// A utf8 parsing error
89    #[error("error parsing utf8 string")]
90    Utf8(#[from] core::str::Utf8Error),
91
92    /// A utf8 parsing error
93    #[error("error parsing utf8 string")]
94    FromUtf8(#[from] alloc::string::FromUtf8Error),
95
96    /// An int parsing error
97    #[error("error parsing int")]
98    ParseInt(#[from] ParseIntError),
99
100    /// A JNI call error
101    #[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}