Module for translating JSON decoding errors into user-friendly Russian messages.

This module provides a utility class that converts Python's JSONDecodeError exceptions into more descriptive and readable error messages in Russian. It is useful for improving the end-user experience when parsing JSON data.

JSONDecodeErrorTranslator

Translates JSONDecodeError messages into user-friendly Russian descriptions.

This class provides a method to convert Python's JSONDecodeError exceptions into more readable error messages for end users.

translate classmethod

translate(
    error: JSONDecodeError,
    *,
    msg: str = BASE_JSON_ERROR_MESSAGE,
    line_number: int | None = None,
) -> str

Translate a JSONDecodeError into a human-readable message in Russian.

Parameters:
  • error (JSONDecodeError) –

    The JSONDecodeError instance to translate.

  • msg (str, default: BASE_JSON_ERROR_MESSAGE ) –

    Optional base message to prepend. Defaults to BASE_JSON_ERROR_MESSAGE.

  • line_number (int | None, default: None ) –

    Optional line number for better context.

Returns:
  • str –

    A string containing the translated error message(position and description).

Raises:
  • TypeError –

    If 'error' is not an instance of JSONDecodeError.

Example
try:
    json.loads('{"name": "Alice",}')
except JSONDecodeError as e:
    print(DecodeErrorTranslator.translate(e))

#> Ошибка конвертации в формате JSON.
#> Позиция: 16.
#> Описание: не правильно используются двойные кавычки.