Unverified Commit 6fb3993f authored by Joseph's avatar Joseph Committed by GitHub

fix: Check if the GET comments response was ok, otherwise throw (#62041)

Fixes #61941 example blog-with-comment.

The root issue is that the SWR fetcher does not check the `ok` status of the response, and that way we end up putting the error message object into the SWR data. And then `.map` is invoked in the object.

Since this is a learning kind of example, I think perhaps the presented change is just about enough to understand what's going on. Whether or not the people using the example want to gain access to the message from the server is up to them.
parent 7744cc91
...@@ -3,7 +3,14 @@ import React, { useState } from "react"; ...@@ -3,7 +3,14 @@ import React, { useState } from "react";
import useSWR from "swr"; import useSWR from "swr";
import { useAuth0 } from "@auth0/auth0-react"; import { useAuth0 } from "@auth0/auth0-react";
const fetcher = (url) => fetch(url).then((res) => res.json()); const fetcher = (url) =>
fetch(url).then((res) => {
if (res.ok) {
return res.json();
}
throw new Error(`${res.status} ${res.statusText} while fetching: ${url}`);
});
export default function useComments() { export default function useComments() {
const { getAccessTokenSilently } = useAuth0(); const { getAccessTokenSilently } = useAuth0();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment