Conversation
WalkthroughAdded 404-response handling to Changes
Sequence DiagramsequenceDiagram
participant Client
participant CloudFlare
participant RulesetAPI as Ruleset API
Client->>CloudFlare: getFirewallRules() / getRedirectRules()
CloudFlare->>RulesetAPI: GET ruleset
alt 200 OK
RulesetAPI-->>CloudFlare: 200 + existing ruleset data
CloudFlare-->>Client: return ruleset ID & rules
else 404 Not Found
CloudFlare->>RulesetAPI: POST create new ruleset
RulesetAPI-->>CloudFlare: Creation response (status + body)
rect rgb(220, 235, 255)
CloudFlare->>CloudFlare: parse response<br/>validate status (createStatusCode)<br/>extract ruleset ID & rules
end
CloudFlare-->>Client: return new ruleset ID & rules
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (1)
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: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
cloudflare.js (1)
384-392: Fix variable references and add defensive parsing.This code has multiple issues:
- Line 387: Uses
statusCodeinstead ofcreateStatusCodein the error message, which would incorrectly report "404" instead of the actual creation failure status.- Line 384: Missing try-catch for JSON parsing, unlike the consistent pattern in
getFirewallRules()(lines 233-237).- Line 392: References
responseinstead ofcreateResponsein the error message.Apply this diff to fix all issues:
- const createResponse = await createBody.json() + let createResponse + try { + createResponse = await createBody.json() + } catch (e) { + createResponse = await createBody.text() + } if (createStatusCode !== 200) { - throw new Error(`Could not create redirect ruleset: ${statusCode}, error: ${JSON.stringify(createResponse)}`) + throw new Error(`Could not create redirect ruleset: ${createStatusCode}, error: ${JSON.stringify(createResponse)}`) } const { id, rules } = createResponse?.result ?? {} if (!id) { - throw new Error(`Could not get redirect rules ruleset ID: got ${id}, received value: ${JSON.stringify(response)}`) + throw new Error(`Could not get redirect rules ruleset ID: got ${id}, received value: ${JSON.stringify(createResponse)}`) }
🧹 Nitpick comments (1)
cloudflare.js (1)
213-249: LGTM! Consider extracting common ruleset creation logic.The 404 handling correctly creates a new ruleset when none exists. The try-catch for JSON parsing and validation logic are well-implemented.
However, this logic is nearly identical to the 404 handling in
getRedirectRules()(lines 364-395). Consider extracting a common helper method to reduce duplication:async createRuleset(name, phase) { const url = CLOUDFLARE_API_URL + `zones/${this.zoneId}/rulesets`; const payload = { name, kind: 'zone', phase, rules: [] }; const { statusCode, body } = await this.requestWithDelay(url, { method: 'POST', headers: { ...this.authorizationHeaders, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); let response; try { response = await body.json(); } catch (e) { response = await body.text(); } if (statusCode !== 200) { throw new Error(`Could not create ${phase} ruleset: ${statusCode}, error: ${JSON.stringify(response)}`); } const { id, rules } = response?.result ?? {}; if (!id) { throw new Error(`Could not get ${phase} ruleset ID: got ${id}, received value: ${JSON.stringify(response)}`); } return { id, rules: rules ?? [] }; }Then use it in both methods:
if (statusCode === 404) { console.log('Firewall ruleset was not found. Initializing firewall ruleset creation...'); return await this.createRuleset('Custom firewall ruleset', 'http_request_firewall_custom'); }
Summary by CodeRabbit
New Features
Bug Fixes
Chores