0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-12-22 20:20:09 +09:00

Fix: error handling on 404

This commit is contained in:
yude 2021-09-24 17:43:07 +09:00
parent c957d791a6
commit a4982c7ef3
Signed by: yude
GPG Key ID: EB0FE5D925C4A968
3 changed files with 25 additions and 10 deletions

View File

@ -7,18 +7,31 @@ function App (props) {
useEffect(() => { useEffect(() => {
const fetchData = async () => { const fetchData = async () => {
const result = await axios( let result = null;
'https://api.ashcon.app/mojang/v2/user/' + uuid.uuid, try {
); result = await axios('https://api.ashcon.app/mojang/v2/user/' + uuid.uuid);
setData(result.data); setData(result.data);
} catch (err) {
result = 404;
setData(result);
}
}; };
fetchData(); fetchData();
}, []); }, []);
if (data === undefined){ if (data === undefined){
console.log("[Minecraft: UUID to player's name] データの取得に失敗しました。 / Failed to retrieve data.") console.log("[Minecraft: UUID to player's name] データの取得に失敗しました。 / Failed to retrieve data.")
return <></> return <>取得中...</>
}else { }else {
return <>{data.username}</> if (data === 404) {
return <p>そのようなプレイヤーは存在しません</p>
} else {
return (
<div className='has-tooltip'>
<span className='tooltip rounded shadow-lg p-1 bg-gray-100 text-red-500 -mt-8 font-mono text-sm'>UUID: {uuid.uuid}</span>
<p className="text-2xl">{data.username}</p>
</div>
)
}
}; };
} }

View File

@ -52,6 +52,9 @@ function App (props) {
if (data.toString() == "[object Object]") { if (data.toString() == "[object Object]") {
return <p>取得中です...</p> return <p>取得中です...</p>
} else { } else {
if (data.toString() == "not_found") {
return <></>
} else {
let dateTime = new Date(parseInt(data.toString()) * 1000); let dateTime = new Date(parseInt(data.toString()) * 1000);
return ( return (
<div className='has-tooltip'> <div className='has-tooltip'>
@ -60,6 +63,7 @@ function App (props) {
</div> </div>
) )
} }
}
}; };
} }

View File

@ -12,11 +12,9 @@ function App (props) {
</div> </div>
<div className="w-5"></div> <div className="w-5"></div>
<div> <div>
<p className="text-2xl text-mono"><GetName uuid={uuid.uuid} /></p> <p className="text-2xl text-mono"><GetName uuid={uuid.uuid} /></p>
<p className="text-xs">UUID: <span className="font-mono">{uuid.uuid}</span></p> <p><Online uuid={uuid.uuid} /></p>
<p><Online uuid={uuid.uuid} /></p>
</div> </div>
</div> </div>
) )
} }