From 5de2a2b2dd2fce9cfbb8bd27111bcf32334b8021 Mon Sep 17 00:00:00 2001 From: Andrew Simonson Date: Mon, 23 Jan 2023 18:59:05 -0600 Subject: [PATCH] handle chess.com req failure --- src/componets/ChessBed.js | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/componets/ChessBed.js b/src/componets/ChessBed.js index cdbd5d7..12929e3 100644 --- a/src/componets/ChessBed.js +++ b/src/componets/ChessBed.js @@ -16,6 +16,7 @@ export default class ChessBed extends React.Component { bullet: "Loading", tactics: "Loading", }, + loaded: "hidden" }; this.diamonds = { background: "black", @@ -28,10 +29,24 @@ export default class ChessBed extends React.Component { } async addChessEmbed(username) { - let user = await fetch(`https://api.chess.com/pub/player/${username}`); - let stats = await fetch( - `https://api.chess.com/pub/player/${username}/stats` - ); + let user, stats; + try { + user = await fetch(`https://api.chess.com/pub/player/${username}`); + stats = await fetch( + `https://api.chess.com/pub/player/${username}/stats` + ); + } catch (e) { + this.setState({ + name: "Chess.com request failed", + ratings: { + rapid: "Site may", + blitz: "be blocked", + bullet: "by client.\n\n", + tactics: "not my fault :(", + }, + }); + return; + } if (user.status === 200) { user = await user.json(); stats = await stats.json(); @@ -44,10 +59,16 @@ export default class ChessBed extends React.Component { bullet: stats.chess_bullet.last.rating, tactics: stats.tactics.highest.rating, }, + loaded: '' + }); + } else if (user === null || user.status === 403 || user.status === null) { + this.setState({ + name: "Chess.com request failed" }); } else { this.setState({ name: "User Not Found", + loaded: '' }); } } @@ -79,19 +100,19 @@ export default class ChessBed extends React.Component {
-
+

{this.state.ratings.rapid}

-
+

{this.state.ratings.blitz}

-
+

{this.state.ratings.bullet}

-
+

{this.state.ratings.tactics}