mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
fix(webapp): catch possibe errors on request geolocation (#8640)
* catch possibe errors on request geolocation * proper toast error * remove deprecated request package, use node fetch instead, set timeout --------- Co-authored-by: Max <maxharz@gmail.com> Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
This commit is contained in:
parent
a8de785783
commit
4eff0fb497
@ -83,7 +83,6 @@
|
|||||||
"nodemailer-html-to-text": "^3.2.0",
|
"nodemailer-html-to-text": "^3.2.0",
|
||||||
"preview-email": "^3.1.0",
|
"preview-email": "^3.1.0",
|
||||||
"pug": "^3.0.3",
|
"pug": "^3.0.3",
|
||||||
"request": "~2.88.2",
|
|
||||||
"sanitize-html": "~2.17.0",
|
"sanitize-html": "~2.17.0",
|
||||||
"slug": "~9.1.0",
|
"slug": "~9.1.0",
|
||||||
"trunc-html": "~1.1.2",
|
"trunc-html": "~1.1.2",
|
||||||
|
|||||||
@ -6,27 +6,15 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable promise/avoid-new */
|
/* eslint-disable n/no-unsupported-features/node-builtins */
|
||||||
/* eslint-disable promise/prefer-await-to-callbacks */
|
|
||||||
import { UserInputError } from 'apollo-server'
|
import { UserInputError } from 'apollo-server'
|
||||||
import request from 'request'
|
|
||||||
|
|
||||||
import CONFIG from '@config/index'
|
import CONFIG from '@config/index'
|
||||||
|
|
||||||
const fetch = (url) => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
request(url, function (error, response, body) {
|
|
||||||
if (error) {
|
|
||||||
reject(error)
|
|
||||||
} else {
|
|
||||||
resolve(JSON.parse(body))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const locales = ['en', 'de', 'fr', 'nl', 'it', 'es', 'pt', 'pl', 'ru']
|
const locales = ['en', 'de', 'fr', 'nl', 'it', 'es', 'pt', 'pl', 'ru']
|
||||||
|
|
||||||
|
const REQUEST_TIMEOUT = 3000
|
||||||
|
|
||||||
const createLocation = async (session, mapboxData) => {
|
const createLocation = async (session, mapboxData) => {
|
||||||
const data = {
|
const data = {
|
||||||
id: mapboxData.id + (mapboxData.address ? `-${mapboxData.address}` : ''),
|
id: mapboxData.id + (mapboxData.address ? `-${mapboxData.address}` : ''),
|
||||||
@ -78,15 +66,21 @@ export const createOrUpdateLocations = async (nodeLabel, nodeId, locationName, s
|
|||||||
|
|
||||||
let locationId
|
let locationId
|
||||||
|
|
||||||
|
try {
|
||||||
if (locationName !== null) {
|
if (locationName !== null) {
|
||||||
const res: any = await fetch(
|
const response: any = await fetch(
|
||||||
`https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(
|
`https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(
|
||||||
locationName,
|
locationName,
|
||||||
)}.json?access_token=${
|
)}.json?access_token=${
|
||||||
CONFIG.MAPBOX_TOKEN
|
CONFIG.MAPBOX_TOKEN
|
||||||
}&types=region,place,country,address&language=${locales.join(',')}`,
|
}&types=region,place,country,address&language=${locales.join(',')}`,
|
||||||
|
{
|
||||||
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const res = await response.json()
|
||||||
|
|
||||||
if (!res?.features?.[0]) {
|
if (!res?.features?.[0]) {
|
||||||
throw new UserInputError('locationName is invalid')
|
throw new UserInputError('locationName is invalid')
|
||||||
}
|
}
|
||||||
@ -157,15 +151,26 @@ export const createOrUpdateLocations = async (nodeLabel, nodeId, locationName, s
|
|||||||
{ nodeId, locationId },
|
{ nodeId, locationId },
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const queryLocations = async ({ place, lang }) => {
|
export const queryLocations = async ({ place, lang }) => {
|
||||||
|
try {
|
||||||
const res: any = await fetch(
|
const res: any = await fetch(
|
||||||
`https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?access_token=${CONFIG.MAPBOX_TOKEN}&types=region,place,country&language=${lang}`,
|
`https://api.mapbox.com/geocoding/v5/mapbox.places/${place}.json?access_token=${CONFIG.MAPBOX_TOKEN}&types=region,place,country&language=${lang}`,
|
||||||
|
{
|
||||||
|
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
const response = await res.json()
|
||||||
// Return empty array if no location found or error occurred
|
// Return empty array if no location found or error occurred
|
||||||
if (!res?.features) {
|
if (!response?.features) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
return res.features
|
return response.features
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -107,6 +107,8 @@ export default {
|
|||||||
this.cities = []
|
this.cities = []
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
this.loadingGeo = true
|
this.loadingGeo = true
|
||||||
|
|
||||||
const place = encodeURIComponent(value)
|
const place = encodeURIComponent(value)
|
||||||
@ -120,6 +122,11 @@ export default {
|
|||||||
this.loadingGeo = false
|
this.loadingGeo = false
|
||||||
|
|
||||||
return this.cities.find((city) => city.value === value)
|
return this.cities.find((city) => city.value === value)
|
||||||
|
} catch (error) {
|
||||||
|
this.$toast.error(error.message)
|
||||||
|
} finally {
|
||||||
|
this.loadingGeo = false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
clearLocationName(event) {
|
clearLocationName(event) {
|
||||||
event.target.value = ''
|
event.target.value = ''
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user