Repository: ansible-collections/community.general

Issue:
yarn: Warnings are flagged as errors 
### Summary

When using `community.general.yarn` any warnings that are emitted during `yarn.list()` will cause the task to fail.

https://github.com/ansible-collections/community.general/blob/c0cb7958cb510610684088167b8c2e71987a6a17/plugins/modules/yarn.py#L245-L248

This scenario is being handled in the `yarn.list_outdated()`, so I think `yarn.list()` will need to be updated to use the same approach:
https://github.com/ansible-collections/community.general/blob/c0cb7958cb510610684088167b8c2e71987a6a17/plugins/modules/yarn.py#L285-L288

### Issue Type

Bug Report

### Component Name

yarn

### Ansible Version

```console (paste below)
$ ansible --version
ansible [core 2.14.3]
  config file = None
  configured module search path = ['/Users/johndaly/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /opt/homebrew/Cellar/ansible/7.3.0/libexec/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/johndaly/.ansible/collections:/usr/share/ansible/collections
  executable location = /opt/homebrew/bin/ansible
  python version = 3.11.2 (main, Feb 16 2023, 02:51:42) [Clang 14.0.0 (clang-1400.0.29.202)] (/opt/homebrew/Cellar/ansible/7.3.0/libexec/bin/python3.11)
  jinja version = 3.1.2
  libyaml = True
```


### Community.general Version

```console (paste below)
$ ansible-galaxy collection list community.general

# /opt/homebrew/Cellar/ansible/7.3.0/libexec/lib/python3.11/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 6.4.0
```


### Configuration

```console (paste below)
$ ansible-config dump --only-changed

```


### OS / Environment

_No response_

### Steps to Reproduce

1. In the directory where Yarn globals are installed, update the `package.json` to not include a `license` field (if it has one set).
2. Attempt to install a package globally with `community.general.yarn`

<!--- Paste example playbooks or commands between quotes below -->
```yaml (paste below)
- name: Install package with yarn global
  community.general.yarn:
    name: "{{ item.name }}"
    executable: "{{ yarn_path }}"
    global: true
    state: latest
  with_items:
    - { name: "ts-node" }
```


### Expected Results

I expect the task to complete, with the global packages being installed successfully. Any warnings that are emitted from Yarn should be ignored and not prevent installation

### Actual Results

```console (paste below)
TASK [Install package with yarn global] *******************************************************************
failed: [localhost] (item={'name': 'ts-node'}) =>
    ansible_loop_var: item
    changed: false
    item:
        name: 'ts-node'
    msg: |-
        {"type":"warning","data":"package.json: No license field"}
```


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct


Discussion:

Resolving patch:
diff --git a/plugins/modules/yarn.py b/plugins/modules/yarn.py
--- a/plugins/modules/yarn.py
+++ b/plugins/modules/yarn.py
@@ -230,6 +230,15 @@ def _exec(self, args, run_in_check_mode=False, check_rc=True, unsupported_with_g
 
         return None, None
 
+    def _process_yarn_error(self, err):
+        try:
+            # We need to filter for errors, since Yarn warnings are included in stderr
+            for line in err.splitlines():
+                if json.loads(line)['type'] == 'error':
+                    self.module.fail_json(msg=err)
+        except Exception:
+            self.module.fail_json(msg="Unexpected stderr output from Yarn: %s" % err, stderr=err)
+
     def list(self):
         cmd = ['list', '--depth=0', '--json']
 
@@ -244,8 +253,7 @@ def list(self):
         # because it only only lists binaries, but `yarn global add` can install libraries too.
         result, error = self._exec(cmd, run_in_check_mode=True, check_rc=False, unsupported_with_global=True)
 
-        if error:
-            self.module.fail_json(msg=error)
+        self._process_yarn_error(error)
 
         for json_line in result.strip().split('\n'):
             data = json.loads(json_line)
@@ -283,9 +291,7 @@ def list_outdated(self):
         cmd_result, err = self._exec(['outdated', '--json'], True, False, unsupported_with_global=True)
 
         # the package.json in the global dir is missing a license field, so warnings are expected on stderr
-        for line in err.splitlines():
-            if json.loads(line)['type'] == 'error':
-                self.module.fail_json(msg=err)
+        self._process_yarn_error(err)
 
         if not cmd_result:
             return outdated


Test patch: