fix: dont insert into resource_variable table if no variables set on resource#880
Conversation
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughReplaced inline error-message normalization with a new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant API as API Handler
participant DB as Database (transaction)
participant Queue as Release Enqueuer
Client->>API: upsertResourceByIdentifier / updateVariablesForResource
API->>DB: begin transaction
alt variables present
API->>DB: insert resourceVariable entries
else no variables
DB-->>API: skip insert
end
DB-->>API: commit
API->>Queue: enqueueReleaseTargetsForResource
API-->>Client: 202 Accepted
alt error occurs
API-->>Client: 500 { error, message: extractMessageFromError(error) }
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/api/src/routes/v1/workspaces/resources.ts`:
- Around line 219-252: The try/catch around the update-resource-variables
handler is catching ApiError (from findResource) and turning it into a 500;
change the catch to re-throw ApiError so its status code is preserved: inside
the catch check if (error instanceof ApiError) throw error; otherwise keep the
existing res.status(500).json({ error: "...", message:
extractMessageFromError(error) }); this preserves behavior like other handlers
(e.g., getVariablesForResource, deleteResourceByIdentifier) while keeping
enqueueReleaseTargetsForResource and the transactional insert logic intact.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 4ff62082-2585-485b-9132-ec5787a618f8
📒 Files selected for processing (2)
apps/api/src/routes/v1/workspaces/resources.tsapps/api/src/routes/v1/workspaces/utils.ts
| try { | ||
| const { workspaceId, identifier } = req.params; | ||
| const { body } = req; | ||
|
|
||
| const resource = await findResource(workspaceId, identifier); | ||
| const { id: resourceId } = resource; | ||
|
|
||
| await db.transaction(async (tx) => { | ||
| await tx | ||
| .delete(schema.resourceVariable) | ||
| .where(eq(schema.resourceVariable.resourceId, resource.id)); | ||
| const entries = Object.entries(body); | ||
| if (entries.length > 0) | ||
| await tx.insert(schema.resourceVariable).values( | ||
| entries.map(([key, value]) => ({ | ||
| resourceId, | ||
| key, | ||
| value, | ||
| })), | ||
| ); | ||
| }); | ||
|
|
||
| enqueueReleaseTargetsForResource(db, workspaceId, resourceId); | ||
| enqueueReleaseTargetsForResource(db, workspaceId, resourceId); | ||
|
|
||
| res.status(202).json({ | ||
| id: resource.id, | ||
| message: "Resource variables update requested", | ||
| }); | ||
| res.status(202).json({ | ||
| id: resource.id, | ||
| message: "Resource variables update requested", | ||
| }); | ||
| } catch (error) { | ||
| res.status(500).json({ | ||
| error: "Failed to update resource variables", | ||
| message: extractMessageFromError(error), | ||
| }); | ||
| } |
There was a problem hiding this comment.
The conditional insert correctly fixes #878, but the try/catch swallows ApiError status codes.
The entries.length > 0 check correctly prevents the empty .values() call. However, wrapping findResource inside the try/catch causes ApiError exceptions (e.g., 404 Not Found) to be caught and returned as 500 errors.
Other handlers in this file (e.g., getVariablesForResource, deleteResourceByIdentifier) let ApiError propagate to asyncHandler which preserves the correct status code.
🛠️ Proposed fix: Re-throw ApiError to preserve status codes
} catch (error) {
+ if (error instanceof ApiError) throw error;
res.status(500).json({
error: "Failed to update resource variables",
message: extractMessageFromError(error),
});
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@apps/api/src/routes/v1/workspaces/resources.ts` around lines 219 - 252, The
try/catch around the update-resource-variables handler is catching ApiError
(from findResource) and turning it into a 500; change the catch to re-throw
ApiError so its status code is preserved: inside the catch check if (error
instanceof ApiError) throw error; otherwise keep the existing
res.status(500).json({ error: "...", message: extractMessageFromError(error) });
this preserves behavior like other handlers (e.g., getVariablesForResource,
deleteResourceByIdentifier) while keeping enqueueReleaseTargetsForResource and
the transactional insert logic intact.
Fixes #878
Summary by CodeRabbit
Bug Fixes
Refactor