from typing import Any


def parse_error(err: Any, raw: bool = True) -> dict:
    """
    Parse single error object (such as pydantic-based or fastapi-based) to dict

    :param err: Error object
    :param raw: Whether this is a raw error or wrapped pydantic error
    :return: dict with name of the field (or "__all__") and actual message
    """
    message = err.msg or ""
    if not raw:
        if len(err.loc) == 2:
            if str(err.loc[0]) == "body":
                name = err.loc[1]
            else:
                name = err.loc[0]
        elif len(err.loc) == 1:
            if str(err.loc[0]) == "body":
                name = "__all__"
            else:
                name = str(err.loc[0])
        else:
            name = "__all__"
    else:
        if len(err.loc) == 2:
            name = str(err.loc[0])
            message = f"{str(err.loc[1]).lower()}: {message}"
        elif len(err.loc) == 1:
            name = str(err.loc[0])
        else:
            name = "__all__"
    return {"name": name, "message": message.capitalize()}