apollo-errors 0.6.0

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

mod common;

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

#[test]
fn test_simple_error_json() {
    let error = SimpleError::Simple;
    let json = error.to_json().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().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().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().unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "config::missing",
      "expected_path": "/app/config.yaml",
      "message": "Missing configuration"
    }
    "#);
}