Repository: Project-MONAI/MONAI

Issue:
[MONAI Bundle] Very long errors during config instantiation - Clean up raised errors
**Is your feature request related to a problem? Please describe.**
When there is an error in the monai bundle config during instantiation, and the config is large, the `RuntimeError` ends up being extremely long. 
This is counterproductive for debugging and pollutes the terminal with uninformative error messages.

The root cause of this looks to be the `RuntimeError` messages generated here:
- https://github.com/Project-MONAI/MONAI/blob/ff430286c37e78d7592372a5a97377f0cbb0219c/monai/bundle/config_item.py#L295
- https://github.com/Project-MONAI/MONAI/blob/ff430286c37e78d7592372a5a97377f0cbb0219c/monai/utils/module.py#L274

**To Reproduce**
Run any relatively large configuration with a wrongly configured argument, and this will occur. 
Attached is an example config and the error log below. 

The command run for the bundle to run is 
`python -m monai.bundle run system --config_file config.yaml`

[error_log.txt](https://github.com/Project-MONAI/MONAI/files/14202650/error_log.txt)
[config.json](https://github.com/Project-MONAI/MONAI/files/14202657/config.json) [YAML renamed to JSON as github doesn't allow uploading YAML]


**Describe the solution you'd like**
A clear and concise error message would be more informative. 

I have the following suggestions: 
1. Remove the `RuntimeError` raised in `ConfigComponent` as the instantiate call will already raise it. Remove this basically: https://github.com/Project-MONAI/MONAI/blob/ff430286c37e78d7592372a5a97377f0cbb0219c/monai/bundle/config_item.py#L295
2. It would be ideal if the RuntimeError raised in https://github.com/Project-MONAI/MONAI/blob/ff430286c37e78d7592372a5a97377f0cbb0219c/monai/utils/module.py#L274 shows only the `__path` and not the full `kwargs` by default. Maybe this could be moved into debug mode instead?





Discussion:
I'm happy to send a PR for this @KumoLiu 
Let me know
Hi @surajpaib, sure, welcome for the contribution. Thanks.

Resolving patch:
diff --git a/monai/bundle/config_item.py b/monai/bundle/config_item.py
--- a/monai/bundle/config_item.py
+++ b/monai/bundle/config_item.py
@@ -289,10 +289,7 @@ def instantiate(self, **kwargs: Any) -> object:
         mode = self.get_config().get("_mode_", CompInitMode.DEFAULT)
         args = self.resolve_args()
         args.update(kwargs)
-        try:
-            return instantiate(modname, mode, **args)
-        except Exception as e:
-            raise RuntimeError(f"Failed to instantiate {self}") from e
+        return instantiate(modname, mode, **args)
 
 
 class ConfigExpression(ConfigItem):
diff --git a/monai/utils/module.py b/monai/utils/module.py
--- a/monai/utils/module.py
+++ b/monai/utils/module.py
@@ -272,7 +272,7 @@ def instantiate(__path: str, __mode: str, **kwargs: Any) -> Any:
             return pdb.runcall(component, **kwargs)
     except Exception as e:
         raise RuntimeError(
-            f"Failed to instantiate component '{__path}' with kwargs: {kwargs}"
+            f"Failed to instantiate component '{__path}' with keywords: {','.join(kwargs.keys())}"
             f"\n set '_mode_={CompInitMode.DEBUG}' to enter the debugging mode."
         ) from e
 


Test patch: