test_error.py

Overview

The `test_error.py` file is a test suite designed to verify the behavior and error handling of JSON encoding and decoding operations, primarily when using the `orjson` library alongside Python's built-in `json` module. It focuses on confirming that errors are raised correctly, that error information is consistent between `json` and `orjson`, and that custom exceptions and edge cases in JSON serialization/deserialization are properly handled.

This file contains two main test classes:

Additional utility functions and classes are defined to facilitate testing of error propagation during encoding.


Classes and Functions

Class: TestJsonDecodeError

Tests the decoding (parsing) of JSON strings/bytes and ensures that decoding errors from `orjson` match those from Python’s standard `json` module.

Methods


Class: Custom

A dummy empty class used in encoding tests to simulate serialization of unsupported types.


Class: CustomException

A custom exception class used for testing exception chaining in encoding errors.


Functions for encoding error chaining tests

These functions simulate `default` callable handlers passed to `orjson.dumps` to test how exceptions propagate and chain.


Class: TestJsonEncodeError

Tests the behavior of `orjson.dumps` when encoding invalid input or when the `default` serialization handler raises exceptions.

Methods


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Example: Testing JSON decoding error positions

test = TestJsonDecodeError()
test._test('["unterminated string', {"pos": 17, "lineno": 1, "colno": 18})

This will verify that both `json.loads` and `orjson.loads` raise a `JSONDecodeError` at the same position.

Example: Testing encoding error chains

test = TestJsonEncodeError()
try:
    orjson.dumps(Custom(), default=default_typeerror)
except orjson.JSONEncodeError as e:
    print(f"Chained exception: {e.__cause__}")  # Outputs: TypeError

Diagram: Class Structure of test_error.py

classDiagram
    class TestJsonDecodeError {
        +_get_error_infos(json_decode_error_exc_info) dict
        +_test(data, expected_err_infos)
        +test_empty()
        +test_ascii()
        +test_latin1()
        +test_two_byte_str()
        +test_two_byte_bytes()
        +test_four_byte()
        +test_tab()
    }

    class TestJsonEncodeError {
        +test_dumps_arg()
        +test_dumps_chain_none()
        +test_dumps_chain_u64()
        +test_dumps_chain_default_typeerror()
        +test_dumps_chain_default_systemerror()
        +test_dumps_chain_default_importerror()
        +test_dumps_chain_default_customerror()
        +test_dumps_normalize_exception()
    }

    class Custom

    class CustomException

    TestJsonEncodeError ..> Custom : uses
    TestJsonEncodeError ..> CustomException : uses
    TestJsonEncodeError ..> default_typeerror : uses
    TestJsonEncodeError ..> default_systemerror : uses
    TestJsonEncodeError ..> default_importerror : uses
    TestJsonEncodeError ..> default_customerror : uses

Summary

The `test_error.py` file is a comprehensive test module validating JSON encoding and decoding error handling, focusing on consistency and correct exception chaining when using the `orjson` library. It includes tests for various malformed inputs, unicode edge cases, and propagating exceptions from custom serializers. The tests ensure robust error reporting and compatibility between `orjson` and Python’s standard `json` module, contributing to the reliability of JSON operations within the broader application or library ecosystem.