Skip to content

Commit 1217e7a

Browse files
committed
Add methods on some errors to retrieve cause more ergonomically (#8)
1 parent 7e7e107 commit 1217e7a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.7.1] - 2023-10-29
8+
### Added
9+
- `ClientError::inner()`
10+
- `ParseUrlError::inner()`
11+
712
## [0.7.0] - 2023-10-13
813
### Added
914
- `RuleGroup::limit()`

src/error.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use serde::Deserialize;
33
use std::error::Error as StdError;
44
use std::fmt;
55

6-
/// A global error enum that encapsulates other more specific types of errors.
7-
/// Some variants contain errors that in turn wrap errors from underlying libraries
8-
/// like [`reqwest`]. These errors can be obtained from [`Error::source()`](StdError::source()).
6+
/// A global error enum that contains all errors that are returned by this
7+
/// library. Some errors are wrappers for errors from underlying libraries.
8+
/// All errors (this enum as well as all contained structs) implement [`std::error::Error`].
99
#[non_exhaustive]
1010
#[derive(Debug)]
1111
pub enum Error {
@@ -138,6 +138,8 @@ impl fmt::Display for PrometheusErrorType {
138138
}
139139
}
140140

141+
/// Is thrown when the [`Client`](crate::Client) or the underlying
142+
/// [`reqwest::Error`] fail to build or execute a request.
141143
#[derive(Debug)]
142144
pub struct ClientError {
143145
pub(crate) message: &'static str,
@@ -156,6 +158,17 @@ impl StdError for ClientError {
156158
}
157159
}
158160

161+
impl ClientError {
162+
/// Obtain the [`reqwest::Error`] that is the actual cause of this
163+
/// error or `None` if the error originated in [`Client`](crate::Client)
164+
/// itself.<br>
165+
pub fn inner(&self) -> Option<&reqwest::Error> {
166+
self.source.as_ref()
167+
}
168+
}
169+
170+
/// Is thrown when the URL that is used to instantiate the [`Client`](crate::Client)
171+
/// is invalid.
159172
#[derive(Debug)]
160173
pub struct ParseUrlError {
161174
pub(crate) message: &'static str,
@@ -173,3 +186,10 @@ impl StdError for ParseUrlError {
173186
Some(&self.source)
174187
}
175188
}
189+
190+
impl ParseUrlError {
191+
/// Obtain the [`url::ParseError`] that is the actual cause of this error.
192+
pub fn inner(&self) -> &url::ParseError {
193+
&self.source
194+
}
195+
}

0 commit comments

Comments
 (0)