Skip to content

fix: improve error handling and logging in system mapping and unmapping#171

Open
OlegGitH wants to merge 11 commits into
mainfrom
task/Error-Variable-Shadowing-Causes-Silent-Failure
Open

fix: improve error handling and logging in system mapping and unmapping#171
OlegGitH wants to merge 11 commits into
mainfrom
task/Error-Variable-Shadowing-Causes-Silent-Failure

Conversation

@OlegGitH
Copy link
Copy Markdown

@OlegGitH OlegGitH commented May 14, 2026

Fixes a critical bug in UnmapSystemFromTenant and MapSystemToTenant where error variables were being shadowed/reused inside transaction callbacks. This caused validation errors from validateAndGetSystemForUnmap and isSystemTenantMapAllowed to be silently overwritten by subsequent r.Patch() or createSystem() calls, resulting in failed operations returning success responses to gRPC callers.
Additionally, both functions and their helper methods lacked any logging beyond the initial entry debug log. This made it impossible to diagnose failures in production — only "UnmapSystemFromTenant called" was visible, with no trace of what happened next.

Changes:

  • Use distinct error variable names (validateErr, patchErr, createErr, getTenantErr, activeErr, regionalErr, getSystemErr) to prevent shadowing
  • Add error/warn logging at every failure point in validateAndGetSystemForUnmap and isSystemTenantMapAllowed with full context (externalId, type, tenantId, status)
  • Add success logging (INFO) after completed map/unmap operations
  • Add debug logging for transaction lifecycle events

Copilot AI review requested due to automatic review settings May 14, 2026 07:30
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ed8fb2e1-49f1-4a14-9440-ac05f566191b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the mapping service’s map/unmap paths for system-to-tenant relationships, primarily improving error/log visibility around validation, repository operations, and successful completion.

Changes:

  • Renames local error variables in map/unmap transaction callbacks.
  • Adds contextual error, warning, debug, and success logs.
  • Updates helper comments for mapping validation functions.
Comments suppressed due to low confidence (2)

internal/service/mapping.go:99

  • Renaming the local error here does not change the behavior described in the PR: the previous code returned immediately from the transaction when map validation failed, so createSystem/Patch could not overwrite that validation error. Please add a regression test that reproduces the claimed success-on-validation-failure path or update the PR description to match the logging-only behavior change.
		system, found, validateErr := isSystemTenantMapAllowed(ctx, r, in)
		if validateErr != nil {
			slogctx.Error(ctx, "isSystemTenantMapAllowed failed", "error", validateErr)
			return validateErr

internal/service/mapping.go:226

  • Correct the grammar in the function comment: “Tenant exist” should be “Tenant exists”.
// It returns nil if the provided Tenant exist, the System is found and not linked, and HasL1KeyClaim is false.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/service/mapping.go
Comment thread internal/service/mapping.go Outdated
Comment thread internal/service/mapping.go Outdated
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings May 14, 2026 13:16
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (2)

internal/service/mapping.go:226

  • This comment is still misleading: the function also returns a nil error when the system is not found so the caller can create it, not only when the system is found and not linked.
// It returns nil if the provided Tenant exist, the System is found and not linked, and HasL1KeyClaim is false.

internal/service/mapping.go:117

  • This success log is emitted before the transaction has committed, so a later commit/rollback failure would still leave a misleading "successfully mapped" debug entry. Reserve success wording for after Transaction returns nil, or make this message explicitly about the patch step only.
		slogctx.Debug(ctx, "system successfully mapped in transaction")

Comment thread internal/service/mapping.go Outdated
Comment thread internal/service/mapping.go
Oleg Grishanovich added 2 commits May 14, 2026 15:22
…uses-Silent-Failure' into task/Error-Variable-Shadowing-Causes-Silent-Failure
Copilot AI review requested due to automatic review settings May 14, 2026 13:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread integration/mapping_test.go Outdated
Comment thread integration/mapping_test.go Outdated
Oleg Grishanovich added 2 commits May 14, 2026 15:59
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread integration/mapping_test.go
Copilot AI review requested due to automatic review settings May 18, 2026 15:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

Comment thread integration/mapping_test.go Outdated
Comment on lines +273 to +276
// Register system without tenant, then unmap, then set L1 key claim and try to map
systemID, systemType, region := registerRegionalSystem(t, ctx, sSubj, "", true, allowedSystemType, nil, nil)
defer cleanupSystem(t, ctx, sSubj, mSubj, systemID, "", systemType, region, true)

Comment thread integration/mapping_test.go Outdated
Comment on lines +136 to +137
systemID, systemType, region := registerRegionalSystem(t, ctx, sSubj, inactiveTenant.ID, false, allowedSystemType, nil, nil)
defer cleanupSystem(t, ctx, sSubj, mSubj, systemID, inactiveTenant.ID, systemType, region, false)
Comment thread integration/mapping_test.go Outdated
assert.Equal(t, status.Code(err), status.Code(service.ErrTenantUnavailable))
})
t.Run("system has active L1 key claim during map", func(t *testing.T) {
// Register system without tenant, then unmap, then set L1 key claim and try to map
Comment on lines +111 to 115
_, patchErr := r.Patch(ctx, system)
if patchErr != nil {
slogctx.Error(ctx, "failed to patch system during map", "error", patchErr)
return ErrSystemUpdate
}
@OlegGitH OlegGitH self-assigned this May 19, 2026
Copilot AI review requested due to automatic review settings May 19, 2026 07:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

internal/service/mapping.go:100

  • The helper already logs at each failure point, but this outer log will fire for every validation failure as an ERROR (including expected cases like not-found / failed-precondition). That produces duplicate log entries and may inflate error-level noise; consider removing this outer error log or logging only unexpected errors here and letting the helper logs stand on their own.
		system, found, validateErr := isSystemTenantMapAllowed(ctx, r, in)
		if validateErr != nil {
			slogctx.Error(ctx, "isSystemTenantMapAllowed failed", "error", validateErr)
			return validateErr
		}

Comment on lines 48 to +52
err := m.repo.Transaction(ctxTimeout, func(ctx context.Context, r repository.Repository) error {
system, err := validateAndGetSystemForUnmap(ctx, r, in)
if err != nil {
return err
system, validateErr := validateAndGetSystemForUnmap(ctx, r, in)
if validateErr != nil {
slogctx.Error(ctx, "validateAndGetSystemForUnmap failed", "error", validateErr)
return validateErr
return err
system, validateErr := validateAndGetSystemForUnmap(ctx, r, in)
if validateErr != nil {
slogctx.Error(ctx, "validateAndGetSystemForUnmap failed", "error", validateErr)
defer func() {
// Change tenant to active so cleanup can proceed
inactiveTenant.Status = model.TenantStatus(tenantgrpc.Status_STATUS_ACTIVE.String())
db.WithContext(ctx).Save(inactiveTenant)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants