Skip to content

Commit

Permalink
WIP: refactor validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
karatakis committed Oct 25, 2024
1 parent a889fd8 commit f37ee5f
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 72 deletions.
84 changes: 84 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 @@ -171,6 +171,7 @@ derive_more = { workspace = true }
strum = "0.26.2"
dashmap = "6.1.0"
urlencoding = "2.1.3"
miette = { version = "7.2.0", features = ["fancy"] }

[dev-dependencies]
datatest-stable = "0.2.9"
Expand Down
13 changes: 11 additions & 2 deletions src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::Result;
use async_graphql::parser::types::ServiceDocument;
use derive_setters::Setters;
use indexmap::IndexMap;
use miette::{diagnostic, MietteDiagnostic};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -424,11 +425,19 @@ impl Config {
Ok(serde_yaml::from_str(yaml)?)
}

pub fn from_sdl(sdl: &str) -> Valid<Self, String> {
pub fn from_sdl(sdl: &str) -> Valid<Self, MietteDiagnostic> {
let doc = async_graphql::parser::parse_schema(sdl);
match doc {
Ok(doc) => from_document(doc),
Err(e) => Valid::fail(e.to_string()),
Err(e) => Valid::fail(
// TODO: ERROR
diagnostic!(
code = "config::config::from_sdl",
help = "Check the graphql syntax",

Check warning on line 436 in src/core/config/config.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/config/config.rs
"{}",
e
)
),
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/core/config/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use std::collections::HashMap;
use async_graphql::parser::types::ConstDirective;
use async_graphql::Name;
use indexmap::IndexMap;
use miette::{diagnostic, MietteDiagnostic};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::core::valid::{Valid, ValidationError, Validator};
use crate::core::valid::{Valid, Validator};
use crate::core::{is_default, pos};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, schemars::JsonSchema)]
Expand All @@ -17,18 +18,23 @@ pub struct Directive {
pub arguments: IndexMap<String, Value>,
}

pub fn to_const_directive(directive: &Directive) -> Valid<ConstDirective, String> {
pub fn to_const_directive(directive: &Directive) -> Valid<ConstDirective, MietteDiagnostic> {
Valid::from_iter(directive.arguments.iter(), |(k, v)| {
let name = pos(Name::new(k.clone()));
Valid::from(serde_json::from_value(v.clone()).map(pos).map_err(|e| {
ValidationError::new(e.to_string()).trace(format!("@{}", directive.name).as_str())
diagnostic!(
code = "config::directive::to_const_directive",
help = "Check the json values provided to the directive",
"{}",
e
)
}))
.map(|value| (name, value))
})
.map(|arguments| ConstDirective { name: pos(Name::new(directive.name.clone())), arguments })
}

pub fn to_directive(const_directive: ConstDirective) -> Valid<Directive, String> {
pub fn to_directive(const_directive: ConstDirective) -> Valid<Directive, MietteDiagnostic> {
const_directive
.arguments
.into_iter()
Expand All @@ -38,7 +44,14 @@ pub fn to_directive(const_directive: ConstDirective) -> Valid<Directive, String>
value.map(|value| (k.node.to_string(), value))
})
.collect::<Result<_, _>>()
.map_err(|e| ValidationError::new(e.to_string()))
.map_err(|e| {
diagnostic!(
code = "config::directive::to_directive",
help = "Check the json values provided to the directive",
"{}",
e
)
})
.map(|arguments| Directive { name: const_directive.name.node.to_string(), arguments })
.into()
}
Expand Down
Loading

0 comments on commit f37ee5f

Please sign in to comment.