updated api-call error-handling

This commit is contained in:
2022-02-03 12:26:18 +01:00
parent c5d64fd196
commit 28d31a865c
8 changed files with 210 additions and 394 deletions

View File

@@ -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(() => {

View File

@@ -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(() => {

View File

@@ -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]) {

View File

@@ -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()

View File

@@ -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/<id>/track POST Track player <id> 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/<id> GET returns details for match <id>
@@ -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

View File

@@ -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) {

View File

@@ -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

View File

@@ -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)