from typing import Union
from typing import Optional
import re


def get_error_code(error_field: Union[str, None]) -> Optional[int]:
    """Get the error code part of the `error\
    <https://docs.cometd.org/current/reference/#_code_error_code>`_, message \
    field

    :param error_field: `Error\
    <https://docs.cometd.org/current/reference/#_code_error_code>`_, message \
    field
    :return: The error code as an int if 3 digits can be matched at the \
    beginning of the error field, for all other cases (``None`` or invalid \
    error field) return ``None``
    """
    result = None
    if error_field is not None:
        match = re.search(r"^\d{3}", error_field)
        if match:
            result = int(match[0])
    return result