apollo-errors 0.6.0

Structured error handling with automatic format conversion
Documentation
//! Tests for ErrorExt trait (dynamic dispatch)

mod common;

use apollo_errors::ErrorExt;
use common::SimpleError;
use insta::assert_snapshot;
use insta::{assert_debug_snapshot, assert_json_snapshot};

#[test]
fn test_error_ext_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_error_ext_graphql() {
    let error = SimpleError::Simple;
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "errors::simple"
      },
      "message": "Something went wrong"
    }
    "#);
}

#[test]
fn test_error_ext_text() {
    let error = SimpleError::Simple;
    let text = error.to_text();
    assert_snapshot!(text, @"[errors::simple] Something went wrong");
}

#[test]
fn test_error_ext_html() {
    let error = SimpleError::Simple;
    let html = error.to_html();
    assert_snapshot!(html, @r#"
    <div class="error">
    <h3 class="error-code">errors::simple</h3>
    <p class="error-message">Something went wrong</p>

    </div>
    "#);
}

#[test]
fn test_error_ext_debug() {
    let error = SimpleError::Simple;
    assert_debug_snapshot!(error, @"Simple");
}

#[test]
fn test_fallback_for_unregistered_error() {
    // std::io::Error is not registered with apollo-errors
    let error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");

    let json = error.to_json().unwrap();
    assert_json_snapshot!(json, @r#"
    {
      "error": "UNKNOWN_ERROR",
      "message": "file not found"
    }
    "#);
}

#[test]
fn test_fallback_graphql_for_unregistered_error() {
    let error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "access denied");

    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "UNKNOWN_ERROR"
      },
      "message": "access denied"
    }
    "#);
}

#[test]
fn test_fallback_text_for_unregistered_error() {
    let error = std::io::Error::new(std::io::ErrorKind::TimedOut, "connection timed out");

    let text = error.to_text();
    assert_snapshot!(text, @"[UNKNOWN_ERROR] connection timed out");
}

#[test]
fn test_boxerror() {
    let error = Box::new(SimpleError::Simple);
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "errors::simple"
      },
      "message": "Something went wrong"
    }
    "#);
}

#[test]
fn test_arc_error() {
    use std::sync::Arc;
    let error = Arc::new(SimpleError::Simple);
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "errors::simple"
      },
      "message": "Something went wrong"
    }
    "#);
}

#[test]
fn test_box_arc_error() {
    use std::sync::Arc;
    let error = Box::new(Arc::new(SimpleError::Simple));
    let graphql = error.to_graphql().unwrap();
    assert_json_snapshot!(graphql, @r#"
    {
      "extensions": {
        "code": "errors::simple"
      },
      "message": "Something went wrong"
    }
    "#);
}