Repository: pre-commit/pre-commit

Issue:
ParserError exception raised for invalid configuration
### search you tried in the issue tracker

ParserError and unicode

### describe your issue

I executed `pre-commit autoupdate` with an invalid configuration file (the second `- repo` is indented incorrectly) and got this error message:

````
$ pre-commit autoupdate
An unexpected error has occurred: ParserError: while parsing a block mapping
  in "<unicode string>", line 1, column 1
did not find expected key
  in "<unicode string>", line 7, column 1
Check the log at /home/carsten/.cache/pre-commit/pre-commit.log
````

This is an expected error and I would expect an error message like `Your configuration file "..." is wrongly formatted at <pos>. Please review the format of the content.'.

Thank you,
Carsten

### pre-commit --version

pre-commit 2.21.0

### .pre-commit-config.yaml

```yaml
repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: check-executables-have-shebangs

- repo: https://github.com/shellcheck-py/shellcheck-py
    rev: v0.9.0.2
    hooks:
    -   id: shellcheck
```


### ~/.cache/pre-commit/pre-commit.log (if present)

### version information

```
pre-commit version: 2.21.0
git --version: git version 2.35.3
sys.version:
    3.10.8 (main, Oct 28 2022, 17:28:32) [GCC]
sys.executable: /home/carsten/virtualenv/bin/python3.10
os.name: posix
sys.platform: linux
```

### error information

```
An unexpected error has occurred: ParserError: while parsing a block mapping
  in "<unicode string>", line 1, column 1
did not find expected key
  in "<unicode string>", line 7, column 1
```

```
Traceback (most recent call last):
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/error_handler.py", line 73, in error_handler
    yield
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/main.py", line 355, in main
    return autoupdate(
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/autoupdate.py", line 154, in autoupdate
    migrate_config(config_file, quiet=True)
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py", line 47, in migrate_config
    contents = _migrate_map(contents)
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/pre_commit/commands/migrate_config.py", line 16, in _migrate_map
    if isinstance(yaml_load(contents), list):
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/__init__.py", line 81, in load
    return loader.get_single_data()
  File "/home/carsten/virtualenv/lib64/python3.10/site-packages/yaml/constructor.py", line 49, in get_single_data
    node = self.get_single_node()
  File "yaml/_yaml.pyx", line 673, in yaml._yaml.CParser.get_single_node
  File "yaml/_yaml.pyx", line 687, in yaml._yaml.CParser._compose_document
  File "yaml/_yaml.pyx", line 731, in yaml._yaml.CParser._compose_node
  File "yaml/_yaml.pyx", line 847, in yaml._yaml.CParser._compose_mapping_node
  File "yaml/_yaml.pyx", line 860, in yaml._yaml.CParser._parse_next_event
yaml.parser.ParserError: while parsing a block mapping
  in "<unicode string>", line 1, column 1
did not find expected key
  in "<unicode string>", line 7, column 1
```


Discussion:

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
@@ -3,8 +3,10 @@
 import re
 import textwrap
 
+import cfgv
 import yaml
 
+from pre_commit.clientlib import InvalidConfigError
 from pre_commit.yaml import yaml_load
 
 
@@ -44,6 +46,13 @@ def migrate_config(config_file: str, quiet: bool = False) -> int:
     with open(config_file) as f:
         orig_contents = contents = f.read()
 
+    with cfgv.reraise_as(InvalidConfigError):
+        with cfgv.validate_context(f'File {config_file}'):
+            try:
+                yaml_load(orig_contents)
+            except Exception as e:
+                raise cfgv.ValidationError(str(e))
+
     contents = _migrate_map(contents)
     contents = _migrate_sha_to_rev(contents)
 


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,9 @@
 from __future__ import annotations
 
+import pytest
+
 import pre_commit.constants as C
+from pre_commit.clientlib import InvalidConfigError
 from pre_commit.commands.migrate_config import migrate_config
 
 
@@ -129,3 +132,13 @@ def test_migrate_config_sha_to_rev(tmpdir):
         '    rev: v1.2.0\n'
         '    hooks: []\n'
     )
+
+
+def test_migrate_config_invalid_yaml(tmpdir):
+    contents = '['
+    cfg = tmpdir.join(C.CONFIG_FILE)
+    cfg.write(contents)
+    with tmpdir.as_cwd(), pytest.raises(InvalidConfigError) as excinfo:
+        migrate_config(C.CONFIG_FILE)
+    expected = '\n==> File .pre-commit-config.yaml\n=====> '
+    assert str(excinfo.value).startswith(expected)