code: def test_error_handling():
      (
          model,
          inputs,
          torch_input_values,
          coreml_input_values,
      ) = get_test_model_and_data()
      torchmodel = torch.jit.trace(model, torch_input_values)
      mlmodel = ct.convert(
          torchmodel, inputs=inputs, convert_to="mlprogram"
      )
  
      # Test invalid mode for affine quantization
      expected_err_str = (
          "supported for weight affine quantization. Got mode"
      )
      with pytest.raises(ValueError, match=expected_err_str):
          linear_quantize_weights(mlmodel, mode="invalid_mode")
  
      # Test invalid dtype for affine quantization
      expected_err_str = "is unsupported for affine_quantize_weight"
      with pytest.raises(ValueError, match=expected_err_str):
          linear_quantize_weights(mlmodel, dtype=np.int32)
  
      expected_err_str = (
          "'dtype' must be \<class 'type'\> \(got 'int32'"
      )
      with pytest.raises(TypeError, match=expected_err_str):
          linear_quantize_weights(mlmodel, dtype="int32")
  
      # Test invalid threshold for weight sparsification
      expected_err_str = 'Invalid value of "threshold": \-1.0. Needs to be in \[0, inf\)'
      with pytest.raises(ValueError, match=expected_err_str):
          prune_weights(mlmodel, mode="threshold_based", threshold=-1.0)
  
      # Test invalid percentile for weight sparsification
      expected_err_str = 'Invalid value of "target_sparsity": 1.2. Needs to be in \[0, 1\]'
      with pytest.raises(ValueError, match=expected_err_str):
          prune_weights(
              mlmodel, mode="percentile_based", target_sparsity=1.2
          )
  
      # Test invalid mode for weight palettization
      expected_err_str = (
          'supported for weight palettization. Got "mode"'
      )
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="invalid_mode")
  
      # Test nbits must be provided for kmeans, uniform mode for weight palettization
      expected_err_str = '"nbits" must be provided for'
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="kmeans")
  
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="uniform")
  
      # Test nbits must not be provided for unique, custom mode for weight palettization
      expected_err_str = '"nbits" must NOT be provided for'
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="unique", nbits=2)
  
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="custom", nbits=2)
  
      # Test lut_function must be provided for custom mode, and must not be provided otherwise
      with pytest.raises(
          ValueError,
          match='"lut_function" can not be None, if "mode" is "custom".',
      ):
          palettize_weights(mlmodel, mode="custom")
      with pytest.raises(
          ValueError,
          match='"lut_function" must be None, if "mode" is not "custom".',
      ):
          palettize_weights(
              mlmodel, mode="unique", lut_function=lambda op: True
          )
  
      # Test lut_function must be a function object
      expected_err_str = (
          'A function object must be provided as "lut_function"'
      )
      with pytest.raises(ValueError, match=expected_err_str):
          palettize_weights(mlmodel, mode="custom", lut_function=1)
  
imports: import itertools
  import numpy
  import pytest
  import torch
  import coremltools
  import coremltools.optimize
  from coremltools._deps import _HAS_SKLEARN
  from coremltools.converters.mil.mil import Builder
  from coremltools.converters.mil.mil import types
  from coremltools.converters.mil.testing_utils import get_op_types_in_program
  from coremltools.optimize.coreml._post_training_quantization import CoreMLWeightMetaData
  from coremltools.test.ml_program.test_compression import get_test_model_and_data
label_desc: Use image node_id 1 for calling the TestErrorHandling obj's underlying member method code with example usage: obj.test_error_handling() without return types