Skip to content

[RTK] Preserve non-JSON error responses#704

Open
yi-nuo426 wants to merge 1 commit intomasterfrom
fix/rtk-non-json-error-handling
Open

[RTK] Preserve non-JSON error responses#704
yi-nuo426 wants to merge 1 commit intomasterfrom
fix/rtk-non-json-error-handling

Conversation

@yi-nuo426
Copy link
Copy Markdown
Contributor

Summary

  • add a shared RTK response handler for Meshery and cloud base queries
  • parse JSON when possible and return raw text otherwise
  • avoid PARSING_ERROR failures when APIs return non-JSON error bodies

Testing

  • npm run build

Use a shared response handler for Meshery and cloud RTK base queries that parses JSON when possible and otherwise returns the raw text body. This avoids PARSING_ERROR failures when servers return text or mislabeled content during error flows.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Lee Calcote <lee.calcote@layer5.io>
Copilot AI review requested due to automatic review settings April 6, 2026 22:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the shared RTK Query base queries for both Meshery and Cloud to preserve non-JSON response bodies (especially error bodies) by introducing a custom responseHandler that attempts JSON parsing and falls back to returning raw text.

Changes:

  • Added a shared parseBaseQueryResponse handler to parse JSON when possible and otherwise return raw response text.
  • Wired the custom responseHandler into both Cloud and Meshery fetchBaseQuery configurations.

Comment on lines +24 to +33

if (!text) {
return null;
}

try {
return JSON.parse(text);
} catch {
return text;
}
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseBaseQueryResponse attempts JSON.parse on every non-empty body, regardless of Content-Type. This will coerce valid JSON scalars like 0, true, or null into non-string values even when the server returns text/plain (your OpenAPI specs define several text/plain responses as type: string). Consider keying JSON parsing off response.headers.get('content-type') (e.g., application/json / +json) or at least a cheap heuristic like trim() + leading {/[ to avoid mis-parsing plain-text responses; also treat whitespace-only bodies as empty via trim().

Suggested change
if (!text) {
return null;
}
try {
return JSON.parse(text);
} catch {
return text;
}
const trimmedText = text.trim();
if (!trimmedText) {
return null;
}
const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
const isJsonResponse = contentType.includes("application/json") || contentType.includes("+json");
if (isJsonResponse) {
try {
return JSON.parse(text);
} catch {
return text;
}
}
return text;

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a custom response handler, parseBaseQueryResponse, to manage empty or non-JSON responses for the RTK Query base queries. The feedback suggests refining the error handling within this function to specifically catch SyntaxError during JSON parsing, ensuring that other potential runtime errors are not inadvertently suppressed.

Comment on lines +22 to +34
const parseBaseQueryResponse = async (response: Response) => {
const text = await response.text();

if (!text) {
return null;
}

try {
return JSON.parse(text);
} catch {
return text;
}
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The parseBaseQueryResponse function uses a generic try-catch block that catches all errors. It is better to specifically catch SyntaxError when parsing JSON to avoid masking other potential runtime errors.

const parseBaseQueryResponse = async (response: Response) => {
  const text = await response.text();

  if (!text) {
    return null;
  }

  try {
    return JSON.parse(text);
  } catch (e) {
    if (e instanceof SyntaxError) {
      return text;
    }
    throw e;
  }
};

@yi-nuo426 yi-nuo426 added pr/do-not-merge PRs not ready to be merged pr/on hold PR/Issue on hold labels Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr/do-not-merge PRs not ready to be merged pr/on hold PR/Issue on hold

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants