0
0

【JavaScript】fetchでネットワークエラーが発生した場合の処理を記述する方法

Last updated at Posted at 2024-02-15

is-netwark-errorというライブラリを使用することでネットワークエラーが発生した場合の処理を記述できます。

npm install is-network-error
import isNetworkError from 'is-network-error';

async function getUnicorns() {
	try {
		const response = await fetch('unicorns.json');
		return await response.json();
	} catch (error) {
		if (isNetworkError(error)) {
			// ネットワークエラーの際の処理
		}

		throw error;
	}
}

console.log(await getUnicorns());

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0