apollo-errors 0.6.0

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

mod common;

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

#[test]
fn test_custom_code_with_extension_field() {
    let error = JsonRpcError::MethodNotFound {
        method: "eth_call".to_string(),
    };
    let jsonrpc = error.to_jsonrpc().unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32601,
      "data": {
        "diagnostic_code": "jsonrpc::method_not_found",
        "method": "eth_call"
      },
      "message": "Method not found: eth_call"
    }
    "#);
}

#[test]
fn test_default_code() {
    // When no jsonrpc_code is specified, defaults to -32000
    let error = JsonRpcError::DefaultCode;
    let jsonrpc = error.to_jsonrpc().unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32000,
      "data": {
        "diagnostic_code": "jsonrpc::default_code"
      },
      "message": "No code specified, uses default"
    }
    "#);
}

#[test]
fn test_struct_error_with_extensions() {
    let error = RpcValidationError {
        field: "amount".to_string(),
        reason: "must be positive".to_string(),
    };
    let jsonrpc = error.to_jsonrpc().unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32602,
      "data": {
        "diagnostic_code": "rpc::validation_error",
        "field": "amount",
        "reason": "must be positive"
      },
      "message": "RPC validation failed for field amount"
    }
    "#);
}

#[test]
fn test_error_without_jsonrpc_attribute() {
    // SimpleError doesn't have jsonrpc_code, should use default -32000
    let error = SimpleError::Simple;
    let jsonrpc = error.to_jsonrpc().unwrap();
    assert_json_snapshot!(jsonrpc, @r#"
    {
      "code": -32000,
      "data": {
        "diagnostic_code": "errors::simple"
      },
      "message": "Something went wrong"
    }
    "#);
}