Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Masked API 400 error reason when streaming responses #346

Open
legraphista opened this issue Mar 22, 2025 · 0 comments
Open

Masked API 400 error reason when streaming responses #346

legraphista opened this issue Mar 22, 2025 · 0 comments
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@legraphista
Copy link

Environment details

  • Programming language: typescript
  • OS: ubuntu server 24.04
  • Language runtime version: 22.9.0
  • Package version: 0.4.0

Steps to reproduce

  1. send a malformed request in streaming mode

Overview

When handling the error, there's a check for application/json in throwErrorIfNotOK

if (response.headers.get('content-type')?.includes('application/json')) {

This check fails since the content type is text/event-stream when streaming, and the error body is not handled at all:

js-genai/src/_api_client.ts

Lines 624 to 632 in f2df6d7

} else {
errorBody = {
error: {
message: 'exception parsing response',
code: response.status,
status: response.statusText,
},
};
}

This masks the true error from being seen by the developer.

After applying the following patch, I could see what I was doing wrong:

async function throwErrorIfNotOK(response) {
    var _a;
    if (response === undefined) {
        throw new ServerError('response is undefined');
    }
    if (!response.ok) {
        const status = response.status;
        const statusText = response.statusText;
        let errorBody;
        if ((_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('application/json')) {
            errorBody = await response.json();
        }
        else {
-           errorBody = {
-               error: {
-                   message: 'exception parsing response',
-                   code: response.status,
-                   status: response.statusText,
-               },
-           };
+           const text = await response.text();
+           errorBody = {
+               error: {
+                   message: text,
+                   code: response.status,
+                   status: response.statusText,
+               },
+           };
        }
        const errorMessage = `got status: ${status} ${statusText}. ${JSON.stringify(errorBody)}`;
        if (status >= 400 && status < 500) {
            const clientError = new ClientError(errorMessage);
            throw clientError;
        }
        else if (status >= 500 && status < 600) {
            const serverError = new ServerError(errorMessage);
            throw serverError;
        }
        throw new Error(errorMessage);
    }
}
@legraphista legraphista added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 22, 2025
@legraphista legraphista changed the title Masked API 400 errors Masked API 400 error reason when streaming responses Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

1 participant