def format_error_message()

in torchx/cli/cmd_status.py [0:0]


def format_error_message(msg: str, header: str, width: int = 80) -> str:
    assert len(header) < width

    match = re.search(_RPC_ERROR_MESSAGE_RE, msg)
    if match:
        start_pos, end_pos = match.span()
        msg = msg[start_pos:end_pos]

    match = re.search(_EMBEDDED_ERROR_MESSAGE_RE, msg)
    if match:
        msg = match.group("msg")

    length = 0
    lines = []
    for i in range(len(msg) + 1):
        if (i == (len(msg))) or (msg[i] == " " and length >= width):
            lines.append(f"{header}{msg[i - length: i]}")
            header = " " * len(header)
            length = 0
        length += 1
    return "\n".join(lines)