#[derive(Debug, thiserror::Error)]
pub enum DeclarativeError {
#[error("invalid YAML: {0}")]
InvalidYaml(String),
#[error("{0}")]
InvalidDocument(String),
#[error("unsupported mcVersion: {0} (this build supports 2)")]
UnsupportedVersion(String),
#[error(
"mcVersion 1 is no longer supported; rewrite the file as mcVersion 2 with `markdown-contract migrate --write <file>` (the CLI of @osdlc/markdown-contract)"
)]
RetiredVersion,
#[error("kind must be \"contract\" or \"config\" (got {0})")]
InvalidKind(String),
#[error("{0}")]
InvalidSchema(String),
#[error("{0}")]
InvalidBody(String),
#[error("{0}")]
InvalidTextSpec(String),
#[error("{0}")]
InvalidConfig(String),
#[error(
"{path}: a contract ref must be a .yaml file (got '{target}'); referencing a code-authored .js/.ts contract is not supported"
)]
CodeContractRef {
path: String,
target: String,
},
#[error("failed to read contract ref '{target}': {reason}")]
ContractRefRead {
target: String,
reason: String,
},
#[error("failed to read '{path}': {reason}")]
FileRead {
path: String,
reason: String,
},
}
#[cfg(test)]
mod tests {
use super::DeclarativeError;
#[test]
fn hook_variants_render_their_messages() {
let e = DeclarativeError::UnsupportedVersion("3".into());
assert_eq!(
e.to_string(),
"unsupported mcVersion: 3 (this build supports 2)"
);
let e = DeclarativeError::RetiredVersion;
assert_eq!(
e.to_string(),
"mcVersion 1 is no longer supported; rewrite the file as mcVersion 2 with `markdown-contract migrate --write <file>` (the CLI of @osdlc/markdown-contract)"
);
let e = DeclarativeError::CodeContractRef {
path: "rules[0].contract".into(),
target: "./task.contract.ts".into(),
};
assert!(e.to_string().contains(".js/.ts contract is not supported"));
}
}