feat(cli): Improve error message when authentication is required (#80)#1378
feat(cli): Improve error message when authentication is required (#80)#1378bhardwajparth51 wants to merge 1 commit intoappwrite:masterfrom
Conversation
📝 WalkthroughWalkthroughAdds a runtime check in the CLI client to detect 401 JSON errors of type Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 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 |
|
@ChiragAgg5k would appreciate a review when you get a chance! |
ChiragAgg5k
left a comment
There was a problem hiding this comment.
@bhardwajparth51 please check the comments + would really appreciate a screenshot of the error message working
6a1aa67 to
83367a7
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
templates/cli/lib/client.ts (1)
244-248: Add a regression test to catch upstream message wording changes.Since Appwrite doesn't expose a machine-readable field to distinguish
general_unauthorized_scopecaused by guest/expired session, the regex check onjson.messageis the only documented approach. However, this creates a fragility: any wording change upstream silently breaks the guest error path and resurfaces the raw scope error. Lock this in with a test that verifies the matcher catches the expected message format.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@templates/cli/lib/client.ts` around lines 244 - 248, Add a regression test that ensures the isUnauthorized matcher (the code building the isUnauthorized boolean in templates/cli/lib/client.ts using json.code, json.type, and the regex /role:\s*guests/i against json.message) still matches the expected Appwrite guest/expired-session message format; create a unit test that constructs JSON responses with realistic message strings (including variations in spacing and casing) and asserts isUnauthorized === true, and include one negative case to ensure other messages do not match so future upstream wording changes fail the test rather than silently breaking the guest error path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@templates/cli/lib/client.ts`:
- Around line 244-248: Add a regression test that ensures the isUnauthorized
matcher (the code building the isUnauthorized boolean in
templates/cli/lib/client.ts using json.code, json.type, and the regex
/role:\s*guests/i against json.message) still matches the expected Appwrite
guest/expired-session message format; create a unit test that constructs JSON
responses with realistic message strings (including variations in spacing and
casing) and asserts isUnauthorized === true, and include one negative case to
ensure other messages do not match so future upstream wording changes fail the
test rather than silently breaking the guest error path.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: d86ed1b1-e5e6-466a-a142-d53db914b4d5
📒 Files selected for processing (1)
templates/cli/lib/client.ts

What does this PR do?
When a user's session cookie is invalid or expired, the Appwrite CLI currently shows a raw API error:
This PR intercepts
401 general_unauthorized_scopeerrors where the role isguestsdirectly insideClient.call()intemplates/cli/lib/client.ts, and replaces the cryptic error with a clear, actionable message:This applies generically to any API endpoint (
/teams,/databases,/functions, etc.) — not just/projects— since the root cause is always the same: an invalid or expired session.Test Plan
docker run --rm -v $(pwd):/app -w /app php:8.3-cli php example.php cliRelated PRs and Issues
Have you read the Contributing Guidelines?
Yes.
Summary by CodeRabbit