apollo-errors 0.6.0

Structured error handling with automatic format conversion
Documentation
//! Tests for extension field handling

mod common;

use apollo_errors::Error as ErrorTrait;
use common::ErrorWithFields;
use insta::assert_json_snapshot;

#[test]
fn test_extension_fields_json() {
    let error = ErrorWithFields::InvalidPort {
        port: 3000,
        config_file: "/app/config.json".to_string(),
    };
    let json = error.to_json().unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "/app/config.json",
      "error": "config::invalid_port",
      "message": "Invalid port",
      "port": 3000
    }
    "#);
}

#[test]
fn test_multiple_fields_graphql() {
    let error = ErrorWithFields::InvalidPort {
        port: 443,
        config_file: "/etc/ssl/config.yaml".to_string(),
    };
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "config::invalid_port",
        "config_file": "/etc/ssl/config.yaml",
        "port": 443
      },
      "message": "Invalid port"
    }
    "#);
}

#[test]
fn test_single_field() {
    let error = ErrorWithFields::MissingConfig {
        expected_path: "/var/lib/app/settings.toml".to_string(),
    };
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "config::missing",
        "expected_path": "/var/lib/app/settings.toml"
      },
      "message": "Missing configuration"
    }
    "#);
}

#[test]
fn test_numeric_field() {
    let error = ErrorWithFields::InvalidPort {
        port: 65535,
        config_file: String::new(),
    };
    let json = error.to_json().unwrap();
    // Verify numeric fields are serialized as numbers, not strings
    assert_json_snapshot!(json, @r#"
    {
      "config_file": "",
      "error": "config::invalid_port",
      "message": "Invalid port",
      "port": 65535
    }
    "#);
}