Repository: conan-io/conan
Pull request: Fixes #6044 Improve error with malformed settings yml
Changelog: Fix: Raise a meaningful error when the `settings.yml` file is invalid
Docs: omit

Fixes #6044 


Problem statement:
[ux] [bug] Improve error raised when 'settings.yml' cannot be parsed
If the file `settings.yml` is invalid, Conan raises an ugly error with all the traceback.

---

How to reproduce:
 * Edit `settings.yml` and make it an invalid YAML
 * Run something like `conan create ....`:

```
⇒  conan create dep1 
Traceback (most recent call last):
  File "/Users/jgsogo/dev/conan/conan/conans/client/command.py", line 1911, in run
    method(args[0][1:])
  File "/Users/jgsogo/dev/conan/conan/conans/client/command.py", line 355, in create
    lockfile=args.lockfile)
 
.... lots of traceback  ...

yaml.scanner.ScannerError: while scanning a simple key
  in "<unicode string>", line 14, column 1:
    os dasf:ffs 
    ^
could not find expected ':'
  in "<unicode string>", line 15, column 12:
        Windows:
               ^

ERROR: while scanning a simple key
  in "<unicode string>", line 14, column 1:
    os dasf:ffs 
    ^
could not find expected ':'
  in "<unicode string>", line 15, column 12:
        Windows:
               ^
```

---

I would expect something easier like: `File 'settings.yml' has invalid YAML format/ cannot be parsed / error reading....` and, if possible, add the information with the line and column.

Hints from issue discussion:
I'll take a look.

Commit message: Fixes #6044 Improve error raised when 'settings.yml' cannot be parsed

Patch to review:
diff --git a/conans/model/settings.py b/conans/model/settings.py
index 2815d96635d..0835d7a8381 100644
--- a/conans/model/settings.py
+++ b/conans/model/settings.py
@@ -233,7 +233,10 @@ def copy_values(self):
 
     @staticmethod
     def loads(text):
-        return Settings(yaml.safe_load(text) or {})
+        try:
+            return Settings(yaml.safe_load(text) or {})
+        except yaml.YAMLError as ye:
+            raise ConanException("settings:invalid YAML format:%s" % str(ye))
 
     def validate(self):
         for field in self.fields:


Code review comments:
diff_hunk: @@ -233,7 +233,10 @@ def copy_values(self):
 
     @staticmethod
     def loads(text):
-        return Settings(yaml.safe_load(text) or {})
+        try:
+            return Settings(yaml.safe_load(text) or {})
+        except yaml.YAMLError as ye:
+            raise ConanException("settings:invalid YAML format:%s" % str(ye))
path: conans/model/settings.py
text: @user1:

```suggestion
            raise ConanException("Invalid settings.yml format:%s" % str(ye))
```

@user2:
Just an extra space to the previous suggestion

```suggestion
            raise ConanException("Invalid settings.yml format: %s" % str(ye))
```