Skip to content

Add AnalysisError type and wrap all analyzer error paths#4779

Open
johnelliott wants to merge 6 commits intomainfrom
enhance/analysis-errors
Open

Add AnalysisError type and wrap all analyzer error paths#4779
johnelliott wants to merge 6 commits intomainfrom
enhance/analysis-errors

Conversation

@johnelliott
Copy link
Copy Markdown
Contributor

@johnelliott johnelliott commented Feb 27, 2026

What this does

Adds a shared error type so analyzer failures carry structured metadata (analyzer type, operation, service). Currently only GCP errors have this; the other 44 analyzers return bare errors.New that get stored as "unknown" in the database.

How to review

Read one file, skip the other 44. There are only 2 things to look at:

1. The new error type (entire abstraction)

pkg/analyzer/analyzers/errors.go -- new file:

// Constants for structured error metadata
const (
    OperationValidateCredentials = "validate_credentials"
    OperationAnalyzePermissions  = "analyze_permissions"
)
const (
    ServiceAPI      = "API"
    ServiceConfig   = "config"
    ServiceDatabase = "Database"
    ServiceOAuth    = "OAuth"
    ServiceCrypto   = "crypto"
)

type AnalysisErrorInfo interface {
    error
    AnalyzerType() string
    Operation() string
    Service() string
    Resource() string
}

type AnalysisError struct {
    analyzerType, operation, service, resource string
    err error
}

func NewAnalysisError(analyzerType, operation, service, resource string, err error) *AnalysisError

2. The pattern every analyzer follows

Every analyzer diff looks like this (Airbrake shown):

// Before:
return nil, err

// After:
return nil, analyzers.NewAnalysisError(
    a.Type().String(),                      // dynamic from analyzer's own type
    analyzers.OperationAnalyzePermissions,  // constant
    analyzers.ServiceAPI,                   // constant
    "",
    err,
)

The original error is wrapped, not replaced (Unwrap() preserves the chain). Analyzer type is derived dynamically via a.Type().String(). Two operation constants are used: OperationValidateCredentials for bad input, OperationAnalyzePermissions for API/DB failures.

All 44 analyzer files are this same 1-3 line change. You can spot-check a few and skip the rest.

Part of a cross-repo change

Order Repo PR What
1 trufflehog this PR Define error type + constants, wrap 44 analyzers
2 integrations #118 Wrap 10 integrations analyzers
3 thog #5724 Scanner consumes the metadata via errors.As()

Test plan

  • go test ./pkg/analyzer/analyzers/... (includes errors_test.go)
  • go build ./pkg/analyzer/analyzers/...

Note

Medium Risk
Touches many analyzers’ Analyze error paths; while changes are mechanical, they alter the error types and messages that downstream consumers may depend on.

Overview
Adds a new analyzers.AnalysisError (and AnalysisErrorInfo interface) plus shared operation/service constants so analyzer failures can be reported with structured metadata while preserving the original error via Unwrap().

Updates a broad set of analyzers to wrap all Analyze-time failures (missing/invalid credential fields and API/DB/crypto permission checks) using analyzers.NewAnalysisError(...) instead of returning raw errors, and includes unit tests for the new error type.

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

@johnelliott johnelliott requested a review from a team February 27, 2026 20:54
@johnelliott johnelliott requested a review from a team as a code owner February 27, 2026 20:54
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 27, 2026

CLA assistant check
All committers have signed the CLA.

@johnelliott johnelliott force-pushed the enhance/analysis-errors branch from 6ef6651 to 409135f Compare March 4, 2026 23:39
@johnelliott johnelliott force-pushed the enhance/analysis-errors branch from 793a1b5 to b747fd9 Compare March 6, 2026 22:40
@johnelliott johnelliott force-pushed the enhance/analysis-errors branch from b747fd9 to 0f66014 Compare March 11, 2026 17:02
Introduce a shared error type that provides structured metadata
(analyzer type, operation, service, resource) for analysis failures.
This allows the scanner to extract context from errors without
depending on concrete types.
Batch A: Airbrake, Anthropic, Asana, DigitalOcean, DockerHub,
ElevenLabs, Fastly, Groq, HuggingFace, Mailchimp, Mailgun, Mux,
Netlify, Ngrok, Notion, OpenAI, Opsgenie, Posthog, Postman,
Sendgrid, Sourcegraph.

Wraps credential validation errors with operation
"validate_credentials" and AnalyzePermissions errors with
operation "analyze_permissions".
Batch B (OAuth/multi-credential): airtableoauth, airtablepat, datadog,
dropbox, figma, launchdarkly, plaid
Batch C (Complex): bitbucket, databricks, github, gitlab, jira, monday,
planetscale, shopify, slack, square, stripe, twilio
Batch D (Database): mysql, postgres (service: Database)
Batch E (PrivateKey): privatekey (service: crypto)
Address PR feedback: replace hardcoded analyzer type strings with
a.Type().String() and replace raw operation/service strings with
package-level constants (OperationValidateCredentials,
OperationAnalyzePermissions, ServiceAPI, ServiceConfig, etc.).
Conditionally include "(resource: ...)" only when non-empty,
avoiding cluttered messages like "... (resource: ): ..." that
appear for the majority of analyzers that don't set a resource.
@johnelliott johnelliott force-pushed the enhance/analysis-errors branch from 0f66014 to 9fe25fe Compare April 9, 2026 16:43
Copy link
Copy Markdown

@cursor cursor bot left a comment

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 and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

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 9fe25fe. Configure here.

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.

3 participants