Skip to content

Conversation

@kweonminsung
Copy link
Owner

No description provided.

Copilot AI review requested due to automatic review settings December 18, 2025 18:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors error handling throughout the codebase by replacing String-based errors with a custom RndcError enum type, providing more structured and type-safe error handling.

  • Introduced a new RndcError enum with specific variants for different error types (InvalidAlgorithm, Base64DecodeError, NetworkError, EncodingError, DecodingError, UnknownError)
  • Updated all public and internal functions to return Result<T, RndcError> instead of Result<T, String>
  • Enhanced RndcAlg::from_string to support additional algorithm aliases (hmd5, hsha1, hsha224, etc.) and return proper errors

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/error.rs New file defining the RndcError enum with Display and Error trait implementations for structured error handling
src/lib.rs Updated RndcClient::new and all methods to use RndcError instead of String errors; added public re-export of RndcError
src/internal/constants.rs Changed RndcAlg::from_string to return Result<Self, RndcError>, added support for additional algorithm aliases, and uncommented ISCCC_ALG_HMAC_MD5 constant
src/internal/encoder.rs Updated encode and make_signature functions to use RndcError; replaced magic number 157 with ISCCC_ALG_HMAC_MD5 constant
src/internal/decoder.rs Updated all decoding functions to return Result<T, RndcError> with proper error wrapping
src/internal/mod.rs Reordered module declarations alphabetically

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

impl fmt::Display for RndcError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RndcError::InvalidAlgorithm(msg) => write!(f, "Invalid algorithm: {}", msg),
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message for InvalidAlgorithm duplicates "Invalid algorithm" in both the Display implementation and the error variant constructor. When from_string is called with an unknown algorithm, the resulting error message will be "Invalid algorithm: Unknown algorithm: sha999". Consider changing line 15 to just write!(f, "{}", msg) or removing "Unknown algorithm:" from the constructor calls.

Suggested change
RndcError::InvalidAlgorithm(msg) => write!(f, "Invalid algorithm: {}", msg),
RndcError::InvalidAlgorithm(msg) => write!(f, "{}", msg),

Copilot uses AI. Check for mistakes.
Base64DecodeError(String),
NetworkError(String),
EncodingError(String),
DecodingError(String),
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UnknownError variant is defined but never used in the codebase. Consider removing it if it's not needed, or document when it should be used.

Suggested change
DecodingError(String),
DecodingError(String),
/// A catch-all error for unexpected or unclassified failures that do not fit other variants.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to 38
pub fn new(server_url: &str, algorithm: &str, secret_key_b64: &str) -> Result<Self, RndcError> {
let secret_key = general_purpose::STANDARD
.decode(secret_key_b64.as_bytes())
.expect("Invalid base64 RNDC_SECRET_KEY");
.map_err(|e| RndcError::Base64DecodeError(e.to_string()))?;

RndcClient {
Ok(RndcClient {
server_url: server_url.to_string(),
algorithm: RndcAlg::from_string(algorithm).expect("Invalid RNDC algorithm"),
algorithm: RndcAlg::from_string(algorithm)?,
secret_key,
}
})
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new error handling in RndcClient::new lacks test coverage. Consider adding tests to verify that invalid base64 input and invalid algorithm strings properly return the appropriate RndcError variants.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to 53
pub(crate) fn from_string(alg: &str) -> Result<Self, RndcError> {
match alg {
"md5" => Some(RndcAlg::MD5),
"hmac-md5" => Some(RndcAlg::MD5),
"sha1" => Some(RndcAlg::SHA1),
"hmac-sha1" => Some(RndcAlg::SHA1),
"sha224" => Some(RndcAlg::SHA224),
"hmac-sha224" => Some(RndcAlg::SHA224),
"sha256" => Some(RndcAlg::SHA256),
"hmac-sha256" => Some(RndcAlg::SHA256),
"sha384" => Some(RndcAlg::SHA384),
"hmac-sha384" => Some(RndcAlg::SHA384),
"sha512" => Some(RndcAlg::SHA512),
"hmac-sha512" => Some(RndcAlg::SHA512),
_ => None,
"md5" => Ok(RndcAlg::MD5),
"hmd5" => Ok(RndcAlg::MD5),
"hmac-md5" => Ok(RndcAlg::MD5),
"sha1" => Ok(RndcAlg::SHA1),
"hsha1" => Ok(RndcAlg::SHA1),
"hmac-sha1" => Ok(RndcAlg::SHA1),
"sha224" => Ok(RndcAlg::SHA224),
"hsha224" => Ok(RndcAlg::SHA224),
"hmac-sha224" => Ok(RndcAlg::SHA224),
"sha256" => Ok(RndcAlg::SHA256),
"hsha256" => Ok(RndcAlg::SHA256),
"hmac-sha256" => Ok(RndcAlg::SHA256),
"sha384" => Ok(RndcAlg::SHA384),
"hsha384" => Ok(RndcAlg::SHA384),
"hmac-sha384" => Ok(RndcAlg::SHA384),
"sha512" => Ok(RndcAlg::SHA512),
"hsha512" => Ok(RndcAlg::SHA512),
"hmac-sha512" => Ok(RndcAlg::SHA512),
_ => Err(RndcError::InvalidAlgorithm(format!(
"Unknown algorithm: {}",
alg
))),
}
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The RndcAlg::from_string method now returns Result<Self, RndcError> but lacks test coverage for both valid algorithm strings and error cases. Consider adding tests to verify all supported algorithm aliases work correctly and that unknown algorithms return InvalidAlgorithm error.

Copilot uses AI. Check for mistakes.
src/lib.rs Outdated
Comment on lines 40 to 42
fn get_stream(&self) -> TcpStream {
TcpStream::connect(&self.server_url).expect("Failed to connect to RNDC server")
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_stream method still uses expect() which will panic on connection failure. Since this PR refactors error handling to use RndcError, this method should return Result<TcpStream, RndcError> and propagate errors properly instead of panicking.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 7 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +48 to +51
_ => Err(RndcError::InvalidAlgorithm(format!(
"Unknown algorithm: {}",
alg
))),
Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message includes redundant text. The Display implementation already prefixes with "Invalid algorithm:", so the error created here will result in "Invalid algorithm: Unknown algorithm: ". Consider removing "Unknown algorithm:" from the message to avoid redundancy, or adjust the Display implementation.

Suggested change
_ => Err(RndcError::InvalidAlgorithm(format!(
"Unknown algorithm: {}",
alg
))),
_ => Err(RndcError::InvalidAlgorithm(alg.to_string())),

Copilot uses AI. Check for mistakes.
@kweonminsung kweonminsung merged commit 163c33b into develop Dec 24, 2025
2 checks passed
@kweonminsung kweonminsung deleted the 3-chore-refactor-error-handling branch December 24, 2025 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants