Handling Errors

When a class method encounters an error during its execution, the method returns False, and an ErrorInfo object is generated. You can get the ErrorInfo object from the error property of the corresponding class, for example MediaInfo.error or Transcoder.error.

The following example shows how you might handle an error if the MediaInfo.open method did not succeed:

with MediaInfo() as info:
    info.inputs[0].file = input_file

    if info.open():
        print_streams(info)

        info.close()
    else:
        print_error("MediaInfo open", info.error)

The ErrorInfo.facility, ErrorInfo.code, and the ErrorInfo.message properties return the component in which the error happened, the error code, and the error message. In addition for most errors the ErrorInfo.hint property may provide additional useful information.

The following example shows how you can use the ErrorInfo object:

def print_error(action: str, error) -> None:
    """Print error information."""
    if action:
        print(f"{action}: ", end="")

    if error.facility == ErrorFacility.Success:
        print("Success")
        return

    print(f"{error.message or ''}, facility:{error.facility} code:{error.code} hint:{error.hint or ''}")