mod common;
use apollo_errors::{CodeCase, ErrorExt, FieldCase, FormatConfig};
use common::{ErrorWithFields, SimpleError};
use insta::assert_snapshot;
use insta::{assert_debug_snapshot, assert_json_snapshot};
#[test]
fn test_error_ext_json() {
let error = SimpleError::Simple;
let json = error.to_json(FormatConfig::default()).unwrap();
assert_json_snapshot!(json, @r#"
{
"error": "errors::simple",
"message": "Something went wrong"
}
"#);
}
#[test]
fn test_error_ext_graphql() {
let error = SimpleError::Simple;
let graphql = error.to_graphql(FormatConfig::default()).unwrap();
assert_json_snapshot!(graphql, @r#"
{
"extensions": {
"code": "errors::simple"
},
"message": "Something went wrong"
}
"#);
}
#[test]
fn test_error_ext_text() {
let error = SimpleError::Simple;
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[errors::simple] Something went wrong");
}
#[test]
fn test_error_ext_html() {
let error = SimpleError::Simple;
let html = error.to_html(FormatConfig::default());
assert_snapshot!(html, @r#"
<div class="error">
<h3 class="error-code">errors::simple</h3>
<p class="error-message">Something went wrong</p>
</div>
"#);
}
#[test]
fn test_error_ext_debug() {
let error = SimpleError::Simple;
assert_debug_snapshot!(error, @"Simple");
}
#[test]
fn test_fallback_for_unregistered_error() {
let error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let json = error.to_json(FormatConfig::default()).unwrap();
assert_json_snapshot!(json, @r#"
{
"error": "UNKNOWN_ERROR",
"message": "file not found"
}
"#);
}
#[test]
fn test_fallback_graphql_for_unregistered_error() {
let error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");
let graphql = error.to_graphql(FormatConfig::default()).unwrap();
assert_json_snapshot!(graphql, @r#"
{
"extensions": {
"code": "UNKNOWN_ERROR"
},
"message": "access denied"
}
"#);
}
#[test]
fn test_fallback_text_for_unregistered_error() {
let error = std::io::Error::new(std::io::ErrorKind::TimedOut, "connection timed out");
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[UNKNOWN_ERROR] connection timed out");
}
#[test]
fn test_boxerror() {
let error = Box::new(SimpleError::Simple);
let graphql = error.to_graphql(FormatConfig::default()).unwrap();
assert_json_snapshot!(graphql, @r#"
{
"extensions": {
"code": "errors::simple"
},
"message": "Something went wrong"
}
"#);
}
#[test]
fn test_arc_error() {
use std::sync::Arc;
let error = Arc::new(SimpleError::Simple);
let graphql = error.to_graphql(FormatConfig::default()).unwrap();
assert_json_snapshot!(graphql, @r#"
{
"extensions": {
"code": "errors::simple"
},
"message": "Something went wrong"
}
"#);
}
#[test]
fn test_box_arc_error() {
use std::sync::Arc;
let error = Box::new(Arc::new(SimpleError::Simple));
let graphql = error.to_graphql(FormatConfig::default()).unwrap();
assert_json_snapshot!(graphql, @r#"
{
"extensions": {
"code": "errors::simple"
},
"message": "Something went wrong"
}
"#);
}
#[test]
fn test_format_config_threads_through_box() {
let error = Box::new(ErrorWithFields::InvalidPort {
port: 8080,
config_file: "/etc/config.toml".to_string(),
});
let config = FormatConfig {
field_case: FieldCase::CamelCase,
code_case: CodeCase::KebabCase,
};
let json = error.to_json(config).unwrap();
assert_json_snapshot!(json, @r#"
{
"configFile": "/etc/config.toml",
"error": "config-invalid-port",
"message": "Invalid port",
"port": 8080
}
"#);
}