Skip to content

Commit f7c35cc

Browse files
committed
isErrorWithMessage addition
1 parent 852da82 commit f7c35cc

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

examples/cookbook/app/network-requests/PhoneBook.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import ContactsList from './components/ContactsList';
55
import FavoritesList from './components/FavoritesList';
66
import getAllContacts from './api/getAllContacts';
77
import getAllFavorites from './api/getAllFavorites';
8-
import { isAxiosError } from 'axios';
98

109
export default () => {
1110
const [usersData, setUsersData] = useState<User[]>([]);
@@ -26,8 +25,7 @@ export default () => {
2625
try {
2726
await Promise.all([_getAllContacts(), _getAllFavorites()]);
2827
} catch (e) {
29-
const isKnownError = e instanceof Error || isAxiosError(e);
30-
const message = isKnownError ? e.message : 'Something went wrong';
28+
const message = isErrorWithMessage(e) ? e.message : 'Something went wrong';
3129
setError(message);
3230
}
3331
};
@@ -46,3 +44,9 @@ export default () => {
4644
</>
4745
);
4846
};
47+
48+
const isErrorWithMessage = (
49+
e: unknown,
50+
): e is {
51+
message: string;
52+
} => typeof e === 'object' && e !== null && 'foo' in e;

0 commit comments

Comments
 (0)