|
1 | 1 | # Get-Error |
2 | 2 |
|
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
3 | 9 | Safely parse anything into an Error instance |
4 | 10 |
|
5 | 11 | ## Getting Started |
|
21 | 27 | } |
22 | 28 | ``` |
23 | 29 |
|
24 | | -#### Handy in building User Interaces |
25 | | - |
26 | | -```jsx |
27 | | -import React from "react"; |
28 | | -import { useQuery } from "@tanstack/react-query"; |
29 | | - |
30 | | -function App() { |
31 | | - const { data, isError, error } = useQuery({ |
32 | | - queryKey: ["data"], |
33 | | - queryFn: async () => { |
34 | | - return Promise.reject(new Response(null, { status: 404 })); |
35 | | - }, |
36 | | - }); |
37 | | - |
38 | | - if (isError) { |
39 | | - // return <p>{error.message}</p> // ❌ NOT safe! |
40 | | - return <p>{getError(error).message}</p>; // ✅ Safe! |
41 | | - } |
42 | | - return <div>{data}</div>; |
43 | | -} |
44 | | -``` |
| 30 | +That's it! |
45 | 31 |
|
46 | 32 | ## Motivation |
47 | 33 |
|
|
90 | 76 | console.log(error.message); // "Method not found" |
91 | 77 | } |
92 | 78 | ``` |
| 79 | + |
| 80 | +### Handy in building User Interaces |
| 81 | + |
| 82 | +```jsx |
| 83 | +import React from "react"; |
| 84 | +import { useQuery } from "@tanstack/react-query"; |
| 85 | + |
| 86 | +function App() { |
| 87 | + const { data, isError, error } = useQuery({ |
| 88 | + queryKey: ["data"], |
| 89 | + queryFn: async () => { |
| 90 | + return Promise.reject(new Response(null, { status: 404 })); |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + if (isError) { |
| 95 | + // return <p>{error.message}</p> // ❌ NOT safe! |
| 96 | + return <p>{getError(error).message}</p>; // ✅ Safe! |
| 97 | + } |
| 98 | + return <div>{data}</div>; |
| 99 | +} |
| 100 | +``` |
0 commit comments