apollo-errors 0.7.0

Structured error handling with automatic format conversion
Documentation
//! Tests for JSON format output (to_json)

mod common;

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

#[test]
fn test_simple_error_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_another_error_json() {
    let error = SimpleError::Another;
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "errors::another",
      "message": "Another error occurred"
    }
    "#);
}

#[test]
fn test_error_with_fields_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: "/etc/config.toml".to_string(),
    };
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "/etc/config.toml",
      "error": "config::invalid_port",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}

#[test]
fn test_error_with_single_field_json() {
    let error = ErrorWithFields::MissingConfig {
        expected_path: "/app/config.yaml".to_string(),
    };
    let json = error.to_json(FormatConfig::default()).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "config::missing",
      "expected_path": "/app/config.yaml",
      "message": "Missing configuration"
    }
    "#);
}

#[test]
fn test_field_case_kebab_code_case_screaming_snake_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: "/etc/config.toml".to_string(),
    };
    let config = FormatConfig {
        field_case: FieldCase::KebabCase,
        code_case: CodeCase::ScreamingSnakeCase,
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config-file": "/etc/config.toml",
      "error": "CONFIG_INVALID_PORT",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}

#[test]
fn test_field_case_screaming_snake_code_case_default_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: "/etc/config.toml".to_string(),
    };
    let config = FormatConfig {
        code_case: CodeCase::ScreamingSnakeCase,
        ..FormatConfig::default()
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "/etc/config.toml",
      "error": "CONFIG_INVALID_PORT",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}

#[test]
fn test_default_field_case_code_case_screaming_snake_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 8080,
        config_file: "/etc/config.toml".to_string(),
    };
    let config = FormatConfig {
        code_case: CodeCase::ScreamingSnakeCase,
        ..FormatConfig::default()
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "/etc/config.toml",
      "error": "CONFIG_INVALID_PORT",
      "message": "Invalid port",
      "port": 8080
    }
    "#);
}

#[test]
fn test_renamed_field_pascal_case_screaming_snake_code_json() {
    let error = ErrorWithRenamedField::WithRename {
        my_field: "value".to_string(),
    };
    let config = FormatConfig {
        field_case: FieldCase::PascalCase,
        code_case: CodeCase::ScreamingSnakeCase,
    };
    let json = error.to_json(config).unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "MyRenamedField": "value",
      "error": "TEST_RENAMED_FIELD",
      "message": "Field was renamed"
    }
    "#);
}