From 28d31a865c063f847c3ad12c6083764f6351bd36 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Thu, 3 Feb 2022 12:26:18 +0100 Subject: [PATCH] updated api-call error-handling --- src/components/DamageSite.vue | 6 +- src/components/Details.vue | 4 +- src/components/EqValueGraph.vue | 2 + src/components/Nav.vue | 2 +- src/utils/ApiRequests.js | 509 ++++++++++---------------------- src/views/Explore.vue | 5 +- src/views/Match.vue | 2 +- src/views/Player.vue | 74 +++-- 8 files changed, 210 insertions(+), 394 deletions(-) diff --git a/src/components/DamageSite.vue b/src/components/DamageSite.vue index 1096fa8..c89f005 100644 --- a/src/components/DamageSite.vue +++ b/src/components/DamageSite.vue @@ -31,8 +31,10 @@ export default { const getWeaponDamage = async () => { const resData = await GetWeaponDmg(store, store.state.matchDetails.match_id) - data.equipment_map = resData.equipment_map - data.stats = resData.stats + if (resData !== null) { + data.equipment_map = resData.equipment_map + data.stats = resData.stats + } } onMounted(() => { diff --git a/src/components/Details.vue b/src/components/Details.vue index 12a4b28..6983e8d 100644 --- a/src/components/Details.vue +++ b/src/components/Details.vue @@ -30,7 +30,9 @@ export default { const getWeaponDamage = async () => { const resData = await GetWeaponDmg(store, store.state.matchDetails.match_id) - data.spray = resData.spray + if (resData !== null) { + data.spray = resData.spray + } } onMounted(() => { diff --git a/src/components/EqValueGraph.vue b/src/components/EqValueGraph.vue index ed757dc..3c53cb1 100644 --- a/src/components/EqValueGraph.vue +++ b/src/components/EqValueGraph.vue @@ -55,6 +55,8 @@ export default { const parseObject = async () => { data.rounds = await GetPlayerValue(store, store.state.matchDetails.match_id) + if (data.rounds === null) + data.rounds = {} for (const round in data.rounds) { for (const player in data.rounds[round]) { diff --git a/src/components/Nav.vue b/src/components/Nav.vue index e57e1b7..d7c6386 100644 --- a/src/components/Nav.vue +++ b/src/components/Nav.vue @@ -122,7 +122,7 @@ export default { if (store.state.id64 !== '' || store.state.vanityUrl !== '') { const resData = await GetUser(store, store.state.vanityUrl || store.state.id64) - if (resData) { + if (resData !== null) { data.searchInput = '' document.activeElement.blur() diff --git a/src/utils/ApiRequests.js b/src/utils/ApiRequests.js index 74b3dc0..c59fcfb 100644 --- a/src/utils/ApiRequests.js +++ b/src/utils/ApiRequests.js @@ -15,47 +15,29 @@ export const GetUser = async (store, id) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Player not found', - type: 'error' - } - }) + message = 'Player not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get meta-stats or player', - type: 'error' - } - }) + message = 'Unable to get meta-stats or player' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -72,47 +54,29 @@ export const GetPlayerMeta = async (store, player_id, limit = 4) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Player not found', - type: 'error' - } - }) + message = 'Player not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get player meta', - type: 'error' - } - }) + message = 'Unable to get player meta' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -129,47 +93,29 @@ export const LoadMoreMatches = async (store, player_id, date) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Player not found', - type: 'error' - } - }) + message = 'Player not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get meta-stats or player', - type: 'error' - } - }) + message = 'Unable to get meta-stats or player' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -177,118 +123,63 @@ export const LoadMoreMatches = async (store, player_id, date) => { // /player//track POST Track player FORM_DATA: authcode, [sharecode] export const TrackMe = async (store, id64, authcode, sharecode = '') => { + let status = null + let message = '' + if (sharecode !== '' && !SHARECODE_REGEX.test(sharecode)) { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.IM_A_TEAPOT, - message: 'Sharecode is invalid or missing', - type: 'error' - } - }) - return STATUS.IM_A_TEAPOT + status = STATUS.IM_A_TEAPOT + message = 'Sharecode is invalid' } if (authcode === '' || !AUTHCODE_REGEX.test(authcode.toUpperCase())) { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.IM_A_TEAPOT, - message: 'Authcode is invalid or missing', - type: 'error' - } - }) - return STATUS.IM_A_TEAPOT + status = STATUS.IM_A_TEAPOT + message = 'Authcode is invalid' } - await axios - .post(`${API_URL}/player/${id64}/track`, `authcode=${authcode.toUpperCase()}&sharecode=${sharecode}`) - .then((res) => { - if (res.status === STATUS.ACCEPTED) { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.ACCEPTED, - message: 'Tracking successful', - type: 'success' - } - }) - return STATUS.ACCEPTED - } - }) - .catch((err) => { - switch (err.response.status) { - case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Invalid arguments', - type: 'error' - } - }) - return STATUS.BAD_REQUEST - case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Player not found', - type: 'error' - } - }) - return STATUS.NOT_FOUND - case STATUS.SERVICE_UNAVAILABLE: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.SERVICE_UNAVAILABLE, - message: 'Service currently unavailable - Please try again later', - type: 'error' - } - }) - return STATUS.SERVICE_UNAVAILABLE - case STATUS.UNAUTHORIZED: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.UNAUTHORIZED, - message: 'Authcode is invalid', - type: 'error' - } - }) - return STATUS.UNAUTHORIZED - case STATUS.PRECONDITION_FAILED: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.PRECONDITION_FAILED, - message: 'Sharecode is invalid or missing', - type: 'error' - } - }) - return STATUS.PRECONDITION_FAILED - case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Service currently unavailable - please try again later', - type: 'error' - } - }) - return STATUS.INTERNAL_SERVER_ERROR - default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) - return err.response.status - } - }) + if (status === null && message === '') { + await axios + .post(`${API_URL}/player/${id64}/track`, `authcode=${authcode.toUpperCase()}&sharecode=${sharecode}`) + .then((res) => { + if (res.status === STATUS.ACCEPTED) { + status = STATUS.ACCEPTED + message = 'Tracking successful' + } + }) + .catch((err) => { + switch (err.response.status) { + case STATUS.BAD_REQUEST: + message = 'Invalid arguments' + break + case STATUS.NOT_FOUND: + message = 'Player not found' + break + case STATUS.SERVICE_UNAVAILABLE: + message = 'Service currently unavailable - Please try again later' + break + case STATUS.UNAUTHORIZED: + message = 'Authcode is invalid' + break + case STATUS.PRECONDITION_FAILED: + message = 'Sharecode is invalid or missing' + break + case STATUS.INTERNAL_SERVER_ERROR: + message = 'Service is currently unavailable - Please try again later' + break + default: + message = 'An unknown error occurred' + } + status = err.response.status + }) + } + + store.commit({ + type: 'changeInfoState', + data: { + statuscode: status, + message: message, + type: 'error' + } + }) + return status } // /match/ GET returns details for match @@ -302,47 +193,29 @@ export const GetMatchDetails = async (store, match_id) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Error parsing matchID', - type: 'error' - } - }) + message = 'Error parsing matchID' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Match not found', - type: 'error' - } - }) + message = 'Match not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get match data', - type: 'error' - } - }) + message = 'Unable to get match data' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -359,47 +232,29 @@ export const GetPlayerValue = async (store, match_id) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Error parsing matchID', - type: 'error' - } - }) + message = 'Error parsing matchID' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Match not found', - type: 'error' - } - }) + message = 'Match not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get match data', - type: 'error' - } - }) + message = 'Unable to get match data' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -416,47 +271,29 @@ export const GetWeaponDmg = async (store, match_id) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.NOT_FOUND: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.NOT_FOUND, - message: 'Weapon damage not found', - type: 'error' - } - }) + message = 'Weapon damage not found' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to get weapon damage', - type: 'error' - } - }) + message = 'Unable to get weapon damage' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -473,37 +310,26 @@ export const GetMatches = async (store) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to marshal JSON', - type: 'error' - } - }) + message = 'Unable to marshal JSON' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response @@ -520,37 +346,26 @@ export const LoadMoreMatchesExplore = async (store, date) => { response = res.data }) .catch((err) => { + let message = '' + switch (err.response.status) { case STATUS.BAD_REQUEST: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.BAD_REQUEST, - message: 'Bad request', - type: 'error' - } - }) + message = 'Bad request' break case STATUS.INTERNAL_SERVER_ERROR: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.INTERNAL_SERVER_ERROR, - message: 'Unable to load more matches', - type: 'error' - } - }) + message = 'Unable to load more matches' break default: - store.commit({ - type: 'changeInfoState', - data: { - statuscode: err.response.status, - message: 'An unknown error occured', - type: 'error' - } - }) + message = 'An unknown error occurred' } + store.commit({ + type: 'changeInfoState', + data: { + statuscode: err.response.status, + message: message, + type: 'error' + } + }) }) return response diff --git a/src/views/Explore.vue b/src/views/Explore.vue index 38fa989..4a113a1 100644 --- a/src/views/Explore.vue +++ b/src/views/Explore.vue @@ -45,7 +45,8 @@ export default { const setMoreMatches = async () => { const res = await LoadMoreMatchesExplore(store, data.matches[data.matches.length - 1].date) - res.forEach(e => data.matches.push(e)) + if (res !== null) + res.forEach(e => data.matches.push(e)) scrollToPos(window.scrollY) @@ -55,7 +56,7 @@ export default { onMounted(async () => { data.matches = await GetMatches(store) - if (data.matches) { + if (data.matches !== null) { if (data.matches[0].map) { await LoadImage(data.matches[0].map) } else if (!data.matches[0].map && MatchNotParsedTime(data.matches[0].date) && data.matches[1].map) { diff --git a/src/views/Match.vue b/src/views/Match.vue index adbc687..acff8f7 100644 --- a/src/views/Match.vue +++ b/src/views/Match.vue @@ -196,7 +196,7 @@ export default { if (matchIdPattern.test(props.match_id)) { const res = await GetMatchDetails(store, props.match_id) - if (res) { + if (res !== null) { if (res.map) document.title = `${FixMapName(res.map)} ► ${res.score[0]} : ${res.score[1]} ◄ ${DateTime.fromSeconds(res.date).toLocaleString(DateTime.DATETIME_SHORT)} | csgoWTF` else diff --git a/src/views/Player.vue b/src/views/Player.vue index 5846272..8a9b35e 100644 --- a/src/views/Player.vue +++ b/src/views/Player.vue @@ -165,7 +165,7 @@ import {FOOTER_HEIGHT, NAV_HEIGHT} from "@/constants"; import MatchesTable from "@/components/MatchesTable"; import router from "@/router"; import PlayerSideInfo from "@/components/PlayerSideInfo"; -import {StatusCodes as STATUS} from "http-status-codes/build/cjs/status-codes"; +import {StatusCodes as STATUS} from "http-status-codes"; export default { name: 'Player', @@ -258,7 +258,7 @@ export default { if (props.id) { const resData = await GetUser(store, props.id) - if (resData) { + if (resData !== null) { if (resData.steamid64 !== store.state.playerDetails.steamid64 || reset) { resData.name = ProcessName(resData.name) @@ -277,7 +277,8 @@ export default { const setMoreMatches = async () => { const res = await LoadMoreMatches(store, store.state.playerDetails.steamid64, data.matches[data.matches.length - 1].date) - await res.matches.forEach(e => data.matches.push(e)) + if (res !== null) + await res.matches.forEach(e => data.matches.push(e)) scrollToPos(window.scrollY) @@ -302,58 +303,49 @@ export default { }) data.playerMeta = await GetPlayerMeta(store, props.id, displayCounter) + if (data.playerMeta === null) + data.playerMeta = {} } const TrackPlayer = async () => { - let res + let message = '' - if (data.matches === []) { - if (data.userData.sharecode !== '') { - if (data.userData.authcode !== '') { - res = await TrackMe(store, store.state.playerDetails.steamid64, data.userData.authcode, data.userData.sharecode) - } else { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.IM_A_TEAPOT, - message: 'Authcode is invalid or missing', - type: 'error' - } - }) - } - } else { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.IM_A_TEAPOT, - message: 'Sharecode is invalid or missing', - type: 'error' - } - }) + if (data.matches.length === 0) { + if (data.userData.sharecode === '') { + message = 'Sharecode is missing' + } + if (data.userData.authcode === '') { + message = 'Authcode is missing' } } else { - if (data.userData.authcode !== '') { - res = await TrackMe(store, store.state.playerDetails.steamid64, data.userData.authcode, data.userData.sharecode) - } else { - store.commit({ - type: 'changeInfoState', - data: { - statuscode: STATUS.IM_A_TEAPOT, - message: 'Authcode is invalid or missing', - type: 'error' - } - }) + if (data.userData.authcode === '') { + message = 'Authcode is missing' } } - if (res === 202) { - location.reload() + if (message !== '') { + store.commit({ + type: 'changeInfoState', + data: { + statuscode: STATUS.IM_A_TEAPOT, + message: message, + type: 'error' + } + }) + } else { + const res = await TrackMe(store, store.state.playerDetails.steamid64, data.userData.authcode, data.userData.sharecode) + + if (res !== null && res === STATUS.ACCEPTED) { + location.reload() + } } } watch(() => props.id, async () => { await GetPlayer() data.playerMeta = await GetPlayerMeta(store, props.id, displayCounter) + if (data.playerMeta === null) + data.playerMeta = {} }) // watch(() => data.playerMeta, () => { @@ -367,6 +359,8 @@ export default { await GetPlayer() data.playerMeta = await GetPlayerMeta(store, props.id, displayCounter) + if (data.playerMeta === null) + data.playerMeta = {} scrollToPos(store.state.scroll_state)