fix(node): surface API errors from redirect() - #1900
Conversation
redirect() threw "Invalid redirect" for any response other than a 301 or 302 and dropped the body. Read 4xx and 5xx bodies the way call() does so the exception carries the server's message, type and response.
|
| if (400 <= response.status) { | ||
| const responseText = await response.text(); | ||
| let data: any = { message: responseText }; | ||
| if ( | ||
| response.headers.get('content-type')?.includes('application/json') | ||
| ) { | ||
| data = JSONbig.parse(responseText); | ||
| } | ||
| throw new {{spec.info.title | caseUcfirst}}Exception( | ||
| data?.message, | ||
| response.status, | ||
| data?.type, | ||
| responseText, | ||
| ); | ||
| } |
There was a problem hiding this comment.
The new redirect error behavior has no behavioral coverage. Existing redirect tests exercise only successful 301/302 responses, while existing 4xx/5xx tests use call(). Add observable-behavior tests proving that JSON and plain-text redirect failures preserve the expected message, type, status, and response; otherwise this fix can regress undetected.
Prompt To Fix With AI
This is a comment left during a code review.
Path: templates/node/src/client.ts.twig
Line: 621-635
Comment:
**Redirect errors lack coverage**
The new redirect error behavior has no behavioral coverage. Existing redirect tests exercise only successful 301/302 responses, while existing 4xx/5xx tests use `call()`. Add observable-behavior tests proving that JSON and plain-text redirect failures preserve the expected message, type, status, and response; otherwise this fix can regress undetected.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Fall back to the raw body as the exception message when a 4xx/5xx response is labelled JSON but does not parse, and wrap the content-type check to satisfy Prettier.
Summary
Client.redirect()in the Node SDK threwInvalid redirectfor any response other than a 301/302 and dropped the body, so a failingcreateOAuth2Token(invalid key, unknown project, missing scope) gave no hint of the cause. It now reads 4xx/5xx bodies the waycall()does, so the exception carries the server'smessage,typeandresponse. Other non-redirect statuses still throwInvalid redirect.The dotnet, kotlin, python and php clients already surface the body on this path.
Part of appwrite/appwrite#8090.
Verification
createOAuth2Tokenagainst a local Appwrite with a real project and an invalid API key. The patched client is node-appwrite 29.0.0 with the same change applied todist/client.js.Invalid redirect, type""The current user is not authorized to perform the requested action., typeuser_unauthorized