add context to error messages (#127)

This commit is contained in:
Adam Leventhal 2022-07-14 21:25:48 -07:00 committed by GitHub
parent 9f6f940705
commit 0c7cace799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 6 deletions

View File

@ -16,15 +16,15 @@ mod util;
#[derive(Error, Debug)]
pub enum Error {
#[error("unexpected value type")]
#[error("unexpected value type {0}: {1}")]
BadValue(String, serde_json::Value),
#[error("type error")]
#[error("type error {0}")]
TypeError(#[from] typify::Error),
#[error("unexpected or unhandled format in the OpenAPI document")]
#[error("unexpected or unhandled format in the OpenAPI document {0}")]
UnexpectedFormat(String),
#[error("invalid operation path")]
#[error("invalid operation path {0}")]
InvalidPath(String),
#[error("internal error")]
#[error("internal error {0}")]
InternalError(String),
}
@ -427,3 +427,42 @@ impl Generator {
&self.type_space
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::Error;
#[test]
fn test_bad_value() {
assert_eq!(
Error::BadValue("nope".to_string(), json! { "nope"},).to_string(),
"unexpected value type nope: \"nope\"",
);
}
#[test]
fn test_type_error() {
assert_eq!(
Error::UnexpectedFormat("nope".to_string()).to_string(),
"unexpected or unhandled format in the OpenAPI document nope",
);
}
#[test]
fn test_invalid_path() {
assert_eq!(
Error::InvalidPath("nope".to_string()).to_string(),
"invalid operation path nope",
);
}
#[test]
fn test_internal_error() {
assert_eq!(
Error::InternalError("nope".to_string()).to_string(),
"internal error nope",
);
}
}

View File

@ -156,7 +156,7 @@ impl ToString for PathTemplate {
}
#[cfg(test)]
mod test {
mod tests {
use std::collections::HashMap;
use super::{parse, Component, PathTemplate};