apollo-errors 0.7.0

Structured error handling with automatic format conversion
Documentation
//! Tests for diagnostic code format

mod common;

use apollo_errors::{CodeCase, Error as ErrorTrait, FormatConfig};
use common::{ErrorWithFields, SimpleError};
use insta::assert_json_snapshot;

#[test]
fn test_simple_code_format() {
    // Error codes use namespace::area::code format
    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_nested_code_format() {
    // Error codes use namespace::area::code format
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: String::new(),
    };
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "",
      "error": "config::invalid_port",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}

#[test]
fn test_code_in_graphql_extensions() {
    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_code_case_screaming_snake_in_graphql() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: String::new(),
    };
    let config = FormatConfig {
        code_case: CodeCase::ScreamingSnakeCase,
        ..FormatConfig::default()
    };
    let graphql = error.to_graphql(config).unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "CONFIG_INVALID_PORT",
        "config_file": "",
        "port": 8080
      },
      "message": "Invalid port"
    }
    "#);
}

#[test]
fn test_code_case_camel_in_graphql() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: String::new(),
    };
    let config = FormatConfig {
        code_case: CodeCase::CamelCase,
        ..FormatConfig::default()
    };
    let graphql = error.to_graphql(config).unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "configInvalidPort",
        "config_file": "",
        "port": 8080
      },
      "message": "Invalid port"
    }
    "#);
}

#[test]
fn test_code_case_pascal_in_json() {
    let error = SimpleError::Simple;
    let config = FormatConfig {
        code_case: CodeCase::PascalCase,
        ..FormatConfig::default()
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "ErrorsSimple",
      "message": "Something went wrong"
    }
    "#);
}

#[test]
fn test_code_case_kebab_in_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: String::new(),
    };
    let config = FormatConfig {
        code_case: CodeCase::KebabCase,
        ..FormatConfig::default()
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "",
      "error": "config-invalid-port",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}