Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"gymValidDataLimit": 30,
"hideOldGyms": false,
"stopValidDataLimit": 30,
"hideOldPokestops": false
"hideOldPokestops": false,
"fetchTimeoutMs": 3000
},
"multiDomains": [],
"map": {
Expand Down
13 changes: 12 additions & 1 deletion server/src/services/api/fetchJson.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* eslint-disable no-console */
const fetch = require('node-fetch')
const { AbortError } = require('node-fetch')

module.exports = async function fetchJson(url, options = undefined, log = false) {
const controller = new AbortController()

const timeout = setTimeout(() => {
controller.abort()
}, 5000)

try {
if (log) console.log(url, options)
const response = await fetch(url, options)
Expand All @@ -10,11 +17,15 @@ module.exports = async function fetchJson(url, options = undefined, log = false)
}
return response.json()
} catch (e) {
if (log) {
if (e instanceof AbortError) {
console.log('Request to', url, 'timed out and was aborted')
} else if (log) {
console.warn(e)
} else {
console.warn(e.message, '\n', e.code, `\nUnable to fetch ${url}`)
}
return null
} finally {
clearTimeout(timeout)
}
}
16 changes: 15 additions & 1 deletion server/src/services/api/scannerApi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-console */
const fetch = require('node-fetch')
const { AbortError } = require('node-fetch')

const config = require('../config')

const scannerQueue = {
Expand All @@ -8,6 +10,12 @@ const scannerQueue = {
}

module.exports = async function scannerApi(category, method, data = null) {
const controller = new AbortController()

const timeout = setTimeout(() => {
controller.abort()
}, config.api.fetchTimeoutMs)

try {
const headers = {}
switch (config.scanner.backendConfig.platform) {
Expand Down Expand Up @@ -88,7 +96,13 @@ module.exports = async function scannerApi(category, method, data = null) {
return { status: 'error', message: 'scanner_error' }
}
} catch (e) {
console.log('[scannerApi] There was a problem processing that scanner request')
if (e instanceof AbortError) {
console.log('Request to the scanner timed out and was aborted')
} else {
console.log('[scannerApi] There was a problem processing that scanner request')
}
return { status: 'error', message: 'scanner_error' }
} finally {
clearTimeout(timeout)
}
}