Repository: pre-commit/pre-commit

Issue:
Unhandled yaml.scanner.ScannerError when trying autoupdate with a malformed pre-commit config
In migrate_config.py we catch `yaml.YAMLError` on [lines 31-36](https://github.com/pre-commit/pre-commit/blob/master/pre_commit/commands/migrate_config.py#L31-L36) (of which `yaml.scanner.ScannerError` is a subclass), but when the exception is raised on line 28, it is unhandled.

```console
$ pre-commit autoupdate
An unexpected error has occurred: ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
Check the log at /home/ryan/.cache/pre-commit/pre-commit.log
```

### version information

```
pre-commit version: 2.3.0
sys.version:
    3.8.2 (default, Apr  8 2020, 14:31:25) 
    [GCC 9.3.0]
sys.executable: /home/ryan/.local/pipx/venvs/pre-commit/bin/python
os.name: posix
sys.platform: linux
```

### error information

```
An unexpected error has occurred: ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
```

```
Traceback (most recent call last):
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/error_handler.py", line 56, in error_handler
    yield
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/main.py", line 354, in main
    return autoupdate(
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/autoupdate.py", line 141, in autoupdate
    migrate_config(config_file, quiet=True)
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py", line 49, in migrate_config
    contents = _migrate_map(contents)
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/pre_commit/commands/migrate_config.py", line 28, in _migrate_map
    if isinstance(yaml_load(contents), list):
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/__init__.py", line 114, in load
    return loader.get_single_data()
  File "/home/ryan/.local/pipx/venvs/pre-commit/lib/python3.8/site-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "ext/_yaml.pyx", line 707, in _yaml.CParser.get_single_node
  File "ext/_yaml.pyx", line 726, in _yaml.CParser._compose_document
  File "ext/_yaml.pyx", line 905, in _yaml.CParser._parse_next_event
yaml.scanner.ScannerError: mapping values are not allowed in this context
  in "<unicode string>", line 2, column 6
```



Discussion:
If this is desired behavior, please close the issue (naturally). The stack trace I found in the log was plenty informative to locate the problem with my config.
do you have a sample configuration that triggers this, I believe there's a better error message we can produce in this case

Resolving patch:
diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py
--- a/pre_commit/commands/migrate_config.py
+++ b/pre_commit/commands/migrate_config.py
@@ -2,6 +2,7 @@
 
 import yaml
 
+from pre_commit.clientlib import load_config
 from pre_commit.util import yaml_load
 
 
@@ -43,6 +44,9 @@ def _migrate_sha_to_rev(contents: str) -> str:
 
 
 def migrate_config(config_file: str, quiet: bool = False) -> int:
+    # ensure that the configuration is a valid pre-commit configuration
+    load_config(config_file)
+
     with open(config_file) as f:
         orig_contents = contents = f.read()
 


Test patch:
diff --git a/tests/commands/migrate_config_test.py b/tests/commands/migrate_config_test.py
--- a/tests/commands/migrate_config_test.py
+++ b/tests/commands/migrate_config_test.py
@@ -1,6 +1,7 @@
 import pytest
 
 import pre_commit.constants as C
+from pre_commit.clientlib import InvalidConfigError
 from pre_commit.commands.migrate_config import _indent
 from pre_commit.commands.migrate_config import migrate_config
 
@@ -147,10 +148,10 @@ def test_migrate_config_sha_to_rev(tmpdir):
 
 
 @pytest.mark.parametrize('contents', ('', '\n'))
-def test_empty_configuration_file_user_error(tmpdir, contents):
+def test_migrate_config_invalid_configuration(tmpdir, contents):
     cfg = tmpdir.join(C.CONFIG_FILE)
     cfg.write(contents)
-    with tmpdir.as_cwd():
-        assert not migrate_config(C.CONFIG_FILE)
+    with tmpdir.as_cwd(), pytest.raises(InvalidConfigError):
+        migrate_config(C.CONFIG_FILE)
     # even though the config is invalid, this should be a noop
     assert cfg.read() == contents