Skip to content

Commit

Permalink
feat: implement std::error::Error for crate-level error types
Browse files Browse the repository at this point in the history
  • Loading branch information
lus committed Apr 4, 2024
1 parent bb2e037 commit 463b589
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
thiserror = "1.0"
reqwest = { version = "0.12", optional = true, default-features = false, features = ["http2", "json"]}
serde = { version = "1.0.197", optional = true, features = ["derive"] }

Expand Down
45 changes: 33 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use std::{
str::FromStr,
};

use thiserror::Error;

#[cfg(feature = "hetzner")]
pub mod hetzner;

Expand Down Expand Up @@ -64,16 +66,19 @@ pub trait Provider {
///
/// Providers can provide a custom error type ([`Provider::CustomRetrieveError`]) and return it using [`RetrieveZoneError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum RetrieveZoneError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that there is no zone with the given ID.
#[error("the requested zone was not found")]
NotFound,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

/// Represents a [`Provider`] that supports zone creation.
Expand All @@ -93,16 +98,19 @@ pub trait CreateZone: Provider {
///
/// Providers can provide a custom error type ([`CreateZone::CustomCreateError`]) and return it using [`CreateZoneError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum CreateZoneError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that the specified domain name was not accepted.
#[error("the given domain name is invalid")]
InvalidDomainName,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

/// Represents a [`Provider`] that supports zone deletion.
Expand All @@ -123,16 +131,19 @@ pub trait DeleteZone: Provider {
///
/// Providers can provide a custom error type ([`DeleteZone::CustomDeleteError`]) and return it using [`DeleteZoneError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum DeleteZoneError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that there is no zone with the given ID.
#[error("the requested zone was not found")]
NotFound,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

/// Represents a DNS record value.
Expand Down Expand Up @@ -293,16 +304,19 @@ pub trait Zone {
///
/// Providers can provide a custom error type ([`Zone::CustomRetrieveError`]) and return it using [`RetrieveRecordError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum RetrieveRecordError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that there is no record with the given ID.
#[error("the requested record was not found")]
NotFound,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

/// Represents a [`Zone`] that supports record creation.
Expand All @@ -324,19 +338,23 @@ pub trait CreateRecord: Zone {
///
/// Providers can provide a custom error type ([`CreateRecord::CustomCreateError`]) and return it using [`CreateRecordError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum CreateRecordError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that the DNS provider does not support the specified record type.
#[error("the DNS provider does not support the specified record type")]
UnsupportedType,

/// Indicates that the record value is invalid.
#[error("the given record value is invalid")]
InvalidRecord,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

/// Represents a [`Zone`] that supports record deletion.
Expand All @@ -356,14 +374,17 @@ pub trait DeleteRecord: Zone {
///
/// Providers can provide a custom error type ([`DeleteRecord::CustomDeleteError`]) and return it using [`DeleteRecordError::Custom`] to extend the pool of well-defined errors.
/// Refer to the provider's documentation for more information.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash, Error)]
pub enum DeleteRecordError<T> {
/// Indicates that the DNS provider is not authorized to execute this action.
#[error("the DNS provider is unauthorized")]
Unauthorized,

/// Indicates that there is no record with the given ID.
#[error("the requested record was not found")]
NotFound,

/// Provides a custom, provider-specific error of type `T`.
Custom(T),
#[error(transparent)]
Custom(#[from] T),
}

0 comments on commit 463b589

Please sign in to comment.