StackStorm / StackStorm/st2

st2 CLI does not return errors as JSON / YAML

Open
#4,218 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug CLI
Dominant language
Python
Stars
6.5k
Forks
787
PR merge metrics
No merged PRs in 30d

Description

SUMMARY

When running the st2 CLI command with -j,--jso or -y,--yaml and an error occurs, the errors are printed as strings and not JSON or YAML.

ISSUE TYPE
  • Bug Report
STACKSTORM VERSION
$ st2 --version
st2 2.7.2, on Python 2.7
OS / ENVIRONMENT / INSTALL METHOD
$ cat /etc/redhat-release                                                                            
CentOS Linux release 7.5.1804 (Core) 

Install type = puppet-st2

STEPS TO REPRODUCE
$ st2 pack get --json fdsfdsfds
Traceback (most recent call last):
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/commands/resource.py", line 165, in get_resource_by_pk
    instance = self.manager.get_by_id(pk, **kwargs)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py", line 40, in decorate
    return func(*args, **kwargs)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py", line 204, in get_by_id
    self.handle_error(response)
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py", line 166, in handle_error
    response.raise_for_status()
  File "/opt/stackstorm/st2/lib/python2.7/site-packages/requests/models.py", line 928, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 401 Client Error: Unauthorized
MESSAGE: Unauthorized - One of Token or API key required. for url: http://127.0.0.1:9101/v1/packs/fdsfdsfds
ERROR: 401 Client Error: Unauthorized
MESSAGE: Unauthorized - One of Token or API key required. for url: http://127.0.0.1:9101/v1/packs/fdsfdsfds
$ st2 login -j -w st2admin
Password: 
Logged in as st2admin
$ st2 pack get --json fdsfsdfsd
Pack "fdsfsdfsd" is not found.
EXPECTED RESULTS

Error messages to be formated as JSON / YAML so they can be parsed programatically.

Example:

$ st2 pack get --json fdsfsdfsd
{
  "success": false.
  "failed": true,
  "error": {
    "type": "pack_not_found",
    "message": "Pack \"fdsfsdfsd\" is not found."
  }
}

Example:

$ st2 pack get --json fdsfsdfsd
{
  "success": false.
  "failed": true,
  "error": {
    "type": "unauthorized",
    "http_status": 401,
    "message": "Unauthorized - One of Token or API key required. for url: http://127.0.0.1:9101/v1/packs/fdsfdsfds"
    "exception_type": "requests.HTTPError"
    "traceback": [
      "Traceback (most recent call last):",
      "  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/commands/resource.py\", line 165, in get_resource_by_pk",
      "    instance = self.manager.get_by_id(pk, **kwargs)",
      "  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py\", line 40, in decorate",
      "    return func(*args, **kwargs)",
      "  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py\", line 204, in get_by_id",
      "    self.handle_error(response)",
      "  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/st2client/models/core.py\", line 166, in handle_error",
      "    response.raise_for_status()",
      "  File \"/opt/stackstorm/st2/lib/python2.7/site-packages/requests/models.py\", line 928, in raise_for_status",
      "    raise HTTPError(http_error_msg, response=self)"
    ]
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the st2 CLI error path shown in st2client/commands/resource.py and st2client/models/core.py, especially get_resource_by_pk, get_by_id, and handle_error. Reproduce the unauthorized and missing-pack commands with --json and --yaml, then verify that failures are emitted as parseable JSON or YAML with the requested error details.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.