Skip to content

feat: [TORCH-584] Reconstruct error-catalog errors from Test API errors - #769

Open
bsalomon-snyk wants to merge 3 commits into
mainfrom
feat/testapi-errors-as-snyk-errors
Open

bsalomon-snyk wants to merge 3 commits into
mainfrom
feat/testapi-errors-as-snyk-errors

Conversation

@bsalomon-snyk

@bsalomon-snyk bsalomon-snyk commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Description

Add ErrorsAsSnykErrors to reconstruct error-catalog errors from Test API errors

  • TestResult.GetErrors() may contain embedded errors while the Test Result fetch itself gives a successful response body. The existing non-2xx error parsing in handleUnexpectedResponse will not run for these, so callers need custom mapping.

  • ErrorsAsSnykErrors gives CLI extensions a way to extract these into Snyk Catalog errors. It reuses the parsing in snyk_errors.FromJSONAPIErrorBytes, so callers get real snyk_errors.Error values with ErrorCode/Title/Detail intact.

Checklist

  • Tests added and all succeed (make test)
  • Regenerated mocks, etc. (make generate)
  • Linted (make lint)
  • Test your changes work for the CLI
    1. Clone / pull the latest CLI main.
    2. Run go get github.com/snyk/go-application-framework@YOUR_LATEST_GAF_COMMIT in the cliv2 directory.
      • Tip: for local testing, you can uncomment the line near the bottom of the CLI's go.mod to point to your local GAF code.
    3. Run go mod tidy in the cliv2 directory.
    4. Run the CLI tests and do any required manual testing.
    5. Open a PR in the CLI repo now with the go.mod and go.sum changes.
    • Once this PR is merged, repeat these steps, but pointing to the latest GAF commit on main and update your CLI PR.

Note

Low Risk
Additive test API helper with focused tests; no changes to polling, auth, or existing error handling paths.

Overview
Adds ErrorsAsSnykErrors so callers can turn TestResult.GetErrors() payloads into snyk_errors.Error values using the same FromJSONAPIErrorBytes path as non-2xx HTTP responses. That matters when a finished test returns 200 with errors in the result body—handleUnexpectedResponse never runs in that case.

Conversion builds a safe JSON:API errors document via jsonAPIErrorPayload, copying code/title/detail/status/id and setting meta.level to error, while omitting Links so Links.About union shapes (string vs object) cannot break parsing. Empty or nil input returns (nil, nil).

New integration-style tests cover full field mapping (e.g. quota SNYK-0006), absent title, object-shaped links.about, and results with no errors.

Reviewed by Cursor Bugbot for commit 480aa54. Bugbot is set up for automated code reviews on this repo. Configure here.

@bsalomon-snyk
bsalomon-snyk requested review from a team as code owners September 16, 2026 12:59
@snyk-io

snyk-io Bot commented Sep 16, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues
Secrets 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@snyk-io

snyk-io Bot commented Sep 16, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@snyk-pr-review-bot

This comment has been minimized.

// GetWarnings()) as error-catalog snyk_errors.Error values, reusing the same JSON:API
// parsing applied to non-2xx responses elsewhere in this client. Returns (nil, nil)
// when errs is nil or empty.
func ErrorsAsSnykErrors(errs *[]IoSnykApiCommonError) ([]snyk_errors.Error, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: isn't this already working from the response middleware?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, unfortunately it only triggers on 2xx/3xx. Since Test API polls Test-API-Shim and gets back a 20x when the test ends, it doesn't process the upstream Snyk errors in errors[].

Comment thread pkg/apiclients/testapi/testapi.go Outdated
return nil, nil
}

body, err := json.Marshal(map[string]any{"errors": *errs})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From AI review:
"""
IoSnykApiCommonError.Links.About can be an object on the wire, but snyk_errors expects about as a string — re-marshaling the raw struct here will fail to parse (and drop all errors) if any error carries a links object. Consider stripping Links/Source before marshaling, or building a narrower local payload struct.
"""

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Updated code to unmarshal only the fields TestAPI cares about so we don't encounter Links.About.

Note that the finding also applies to CLI's middleware, so error-catalog should get a fix for it some day. If CLI encounters an object in Links.About, it silently drops the error instead of returning it.

… codes from test result errors

  - TestResult.GetErrors() can contain embedded errors while the Test Result fetch itself
    gives a successful response body.  The existing non-2xx error parsing in
    handleUnexpectedResponse will not run for these, so callers need custom mapping.

  - ErrorsAsSnykErrors reuses the parsing in snyk_errors.FromJSONAPIErrorBytes, so
    callers get real snyk_errors.Error values with ErrorCode/Title/Detail intact.
@bsalomon-snyk
bsalomon-snyk force-pushed the feat/testapi-errors-as-snyk-errors branch from a926b00 to 72b7dda Compare September 17, 2026 16:57
@snyk-pr-review-bot

This comment has been minimized.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 72b7dda. Configure here.

Comment thread pkg/apiclients/testapi/testapi.go
@bsalomon-snyk
bsalomon-snyk force-pushed the feat/testapi-errors-as-snyk-errors branch from 72b7dda to 80e5f20 Compare September 17, 2026 17:24
@snyk-pr-review-bot

This comment has been minimized.

… box label

  - populate meta.level so snyk_errors.FromJSONAPIErrorBytes can use it.
    This translates into the CLI's error box label.
@bsalomon-snyk
bsalomon-snyk force-pushed the feat/testapi-errors-as-snyk-errors branch from 80e5f20 to 480aa54 Compare September 17, 2026 18:16
@snyk-pr-review-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected
📚 Repository Context Analyzed

This review considered 7 relevant code sections from 5 files (average relevance: 0.89)

🤖 Repository instructions applied (from AGENTS.md)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants