chore: ServerRequest returns an explicit error if any
This commit is contained in:
parent
c50be398ab
commit
b947134fa2
6 changed files with 172 additions and 125 deletions
|
@ -17,15 +17,15 @@ const BaseRequest = Evented.extend({
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this._onError(error)
|
this._onError(error)
|
||||||
this.fire('dataload', { id: id })
|
this.fire('dataload', { id: id })
|
||||||
return
|
return null
|
||||||
}
|
}
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
this.onNok(response.status, await response.text())
|
this.fire('dataload', { id: id })
|
||||||
|
return this.onNok(response.status, response)
|
||||||
}
|
}
|
||||||
// TODO
|
// TODO
|
||||||
// - error handling
|
// - error handling
|
||||||
// - UI connection / events
|
// - UI connection / events
|
||||||
// - preflight mode in CORS ?
|
|
||||||
|
|
||||||
this.fire('dataload', { id: id })
|
this.fire('dataload', { id: id })
|
||||||
return response
|
return response
|
||||||
|
@ -44,7 +44,9 @@ const BaseRequest = Evented.extend({
|
||||||
this.onError(error)
|
this.onError(error)
|
||||||
},
|
},
|
||||||
onError: function (error) {},
|
onError: function (error) {},
|
||||||
onNok: function (status) {},
|
onNok: function (status, reponse) {
|
||||||
|
return response
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export const Request = BaseRequest.extend({
|
export const Request = BaseRequest.extend({
|
||||||
|
@ -55,8 +57,9 @@ export const Request = BaseRequest.extend({
|
||||||
console.error(error)
|
console.error(error)
|
||||||
this.ui.alert({ content: L._('Problem in the response'), level: 'error' })
|
this.ui.alert({ content: L._('Problem in the response'), level: 'error' })
|
||||||
},
|
},
|
||||||
onNok: function (status, message) {
|
onNok: function (status, response) {
|
||||||
this.onError(message)
|
this.onError(message)
|
||||||
|
return response
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -81,26 +84,30 @@ export const ServerRequest = Request.extend({
|
||||||
headers['X-CSRFToken'] = token
|
headers['X-CSRFToken'] = token
|
||||||
}
|
}
|
||||||
const response = await Request.prototype.post.call(this, uri, headers, data)
|
const response = await Request.prototype.post.call(this, uri, headers, data)
|
||||||
return await this._handle_json_response(response)
|
return await this._as_json(response)
|
||||||
},
|
},
|
||||||
|
|
||||||
get: async function (uri, headers) {
|
get: async function (uri, headers) {
|
||||||
const response = await Request.prototype.get.call(this, uri, headers)
|
const response = await Request.prototype.get.call(this, uri, headers)
|
||||||
return await this._handle_json_response(response)
|
return await this._as_json(response)
|
||||||
},
|
},
|
||||||
|
|
||||||
_handle_json_response: async function (response) {
|
_as_json: async function (response) {
|
||||||
|
if (!response) return [{}, null, new Error("Undefined error")]
|
||||||
try {
|
try {
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
this._handle_server_instructions(data)
|
if (this._handle_server_instructions(data) !== false) {
|
||||||
return [data, response]
|
return [{}, null]
|
||||||
|
}
|
||||||
|
return [data, response, null]
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this._onError(error)
|
this._onError(error)
|
||||||
|
return [{}, null, error]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_handle_server_instructions: function (data) {
|
_handle_server_instructions: function (data) {
|
||||||
// In some case, the response contains instructions
|
// Generic cases, let's deal with them once
|
||||||
if (data.redirect) {
|
if (data.redirect) {
|
||||||
const newPath = data.redirect
|
const newPath = data.redirect
|
||||||
if (window.location.pathname == newPath) {
|
if (window.location.pathname == newPath) {
|
||||||
|
@ -122,6 +129,9 @@ export const ServerRequest = Request.extend({
|
||||||
this.fire('login')
|
this.fire('login')
|
||||||
win.close()
|
win.close()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Nothing to do, we can let the response proceed
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -55,10 +55,16 @@ L.U.DataLayerPermissions = L.Class.extend({
|
||||||
if (!this.isDirty) return this.datalayer.map.continueSaving()
|
if (!this.isDirty) return this.datalayer.map.continueSaving()
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
formData.append('edit_status', this.options.edit_status)
|
formData.append('edit_status', this.options.edit_status)
|
||||||
await this.datalayer.map.server.post(this.getUrl(), {}, formData)
|
const [data, response, error] = await this.datalayer.map.server.post(
|
||||||
|
this.getUrl(),
|
||||||
|
{},
|
||||||
|
formData
|
||||||
|
)
|
||||||
|
if (!error) {
|
||||||
this.commit()
|
this.commit()
|
||||||
this.isDirty = false
|
this.isDirty = false
|
||||||
this.datalayer.map.continueSaving()
|
this.datalayer.map.continueSaving()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
commit: function () {
|
commit: function () {
|
||||||
|
|
|
@ -695,12 +695,14 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
|
||||||
if (this.pictogram_list) {
|
if (this.pictogram_list) {
|
||||||
this.buildSymbolsList()
|
this.buildSymbolsList()
|
||||||
} else {
|
} else {
|
||||||
const [{ pictogram_list }, response] = await this.builder.map.server.get(
|
const [{ pictogram_list }, response, error] = await this.builder.map.server.get(
|
||||||
this.builder.map.options.urls.pictogram_list_json
|
this.builder.map.options.urls.pictogram_list_json
|
||||||
)
|
)
|
||||||
|
if (!error) {
|
||||||
this.pictogram_list = pictogram_list
|
this.pictogram_list = pictogram_list
|
||||||
this.buildSymbolsList()
|
this.buildSymbolsList()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
showCharsTab: function () {
|
showCharsTab: function () {
|
||||||
|
|
|
@ -34,7 +34,7 @@ L.Map.mergeOptions({
|
||||||
// we cannot rely on this because of the y is overriden by Leaflet
|
// we cannot rely on this because of the y is overriden by Leaflet
|
||||||
// See https://github.com/Leaflet/Leaflet/pull/9201
|
// See https://github.com/Leaflet/Leaflet/pull/9201
|
||||||
// And let's remove this -y when this PR is merged and released.
|
// And let's remove this -y when this PR is merged and released.
|
||||||
demoTileInfos: { s: 'a', z: 9, x: 265, y: 181, '-y': 181, r: '' },
|
demoTileInfos: { 's': 'a', 'z': 9, 'x': 265, 'y': 181, '-y': 181, 'r': '' },
|
||||||
licences: [],
|
licences: [],
|
||||||
licence: '',
|
licence: '',
|
||||||
enableMarkerDraw: true,
|
enableMarkerDraw: true,
|
||||||
|
@ -838,7 +838,10 @@ L.U.Map.include({
|
||||||
self.isDirty = true
|
self.isDirty = true
|
||||||
}
|
}
|
||||||
if (this._controls.tilelayersChooser)
|
if (this._controls.tilelayersChooser)
|
||||||
this._controls.tilelayersChooser.openSwitcher({ callback: callback, className: 'dark' })
|
this._controls.tilelayersChooser.openSwitcher({
|
||||||
|
callback: callback,
|
||||||
|
className: 'dark',
|
||||||
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
manageDatalayers: function () {
|
manageDatalayers: function () {
|
||||||
|
@ -1097,7 +1100,8 @@ L.U.Map.include({
|
||||||
formData.append('center', JSON.stringify(this.geometry()))
|
formData.append('center', JSON.stringify(this.geometry()))
|
||||||
formData.append('settings', JSON.stringify(geojson))
|
formData.append('settings', JSON.stringify(geojson))
|
||||||
const uri = this.urls.get('map_save', { map_id: this.options.umap_id })
|
const uri = this.urls.get('map_save', { map_id: this.options.umap_id })
|
||||||
const [data, response] = await this.server.post(uri, {}, formData)
|
const [data, response, error] = await this.server.post(uri, {}, formData)
|
||||||
|
if (!error) {
|
||||||
let duration = 3000,
|
let duration = 3000,
|
||||||
alert = { content: L._('Map has been saved!'), level: 'info' }
|
alert = { content: L._('Map has been saved!'), level: 'info' }
|
||||||
if (!this.options.umap_id) {
|
if (!this.options.umap_id) {
|
||||||
|
@ -1150,6 +1154,7 @@ L.U.Map.include({
|
||||||
this.once('saved', () => this.ui.alert(alert))
|
this.once('saved', () => this.ui.alert(alert))
|
||||||
this.ui.closePanel()
|
this.ui.closePanel()
|
||||||
this.permissions.save()
|
this.permissions.save()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
save: function () {
|
save: function () {
|
||||||
|
|
|
@ -674,7 +674,8 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
if (!this.umap_id) return
|
if (!this.umap_id) return
|
||||||
if (this._loading) return
|
if (this._loading) return
|
||||||
this._loading = true
|
this._loading = true
|
||||||
const [geojson, response] = await this.map.server.get(this._dataUrl())
|
const [geojson, response, error] = await this.map.server.get(this._dataUrl())
|
||||||
|
if (!error) {
|
||||||
this._last_modified = response.headers['Last-Modified']
|
this._last_modified = response.headers['Last-Modified']
|
||||||
// FIXME: for now this property is set dynamically from backend
|
// FIXME: for now this property is set dynamically from backend
|
||||||
// And thus it's not in the geojson file in the server
|
// And thus it's not in the geojson file in the server
|
||||||
|
@ -690,6 +691,7 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
this.backupOptions()
|
this.backupOptions()
|
||||||
this.fire('loaded')
|
this.fire('loaded')
|
||||||
this._loading = false
|
this._loading = false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
fromGeoJSON: function (geojson) {
|
fromGeoJSON: function (geojson) {
|
||||||
|
@ -749,8 +751,10 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
url = this.map.proxyUrl(url, this.options.remoteData.ttl)
|
url = this.map.proxyUrl(url, this.options.remoteData.ttl)
|
||||||
const response = await this.map.request.get(url)
|
const response = await this.map.request.get(url)
|
||||||
this.clear()
|
this.clear()
|
||||||
this.rawToGeoJSON(await response.text(), this.options.remoteData.format, (geojson) =>
|
this.rawToGeoJSON(
|
||||||
this.fromGeoJSON(geojson)
|
await response.text(),
|
||||||
|
this.options.remoteData.format,
|
||||||
|
(geojson) => this.fromGeoJSON(geojson)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -1391,8 +1395,10 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
|
|
||||||
const versionsContainer = L.DomUtil.createFieldset(container, L._('Versions'), {
|
const versionsContainer = L.DomUtil.createFieldset(container, L._('Versions'), {
|
||||||
callback: async function () {
|
callback: async function () {
|
||||||
const [{versions}, response] = await this.map.server.get(this.getVersionsUrl())
|
const [{ versions }, response, error] = await this.map.server.get(
|
||||||
versions.forEach(appendVersion)
|
this.getVersionsUrl()
|
||||||
|
)
|
||||||
|
if (!error) versions.forEach(appendVersion)
|
||||||
},
|
},
|
||||||
context: this,
|
context: this,
|
||||||
})
|
})
|
||||||
|
@ -1401,13 +1407,17 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
restore: async function (version) {
|
restore: async function (version) {
|
||||||
if (!this.map.editEnabled) return
|
if (!this.map.editEnabled) return
|
||||||
if (!confirm(L._('Are you sure you want to restore this version?'))) return
|
if (!confirm(L._('Are you sure you want to restore this version?'))) return
|
||||||
const [geojson, response] = await this.map.server.get(this.getVersionUrl(version))
|
const [geojson, response, error] = await this.map.server.get(
|
||||||
|
this.getVersionUrl(version)
|
||||||
|
)
|
||||||
|
if (!error) {
|
||||||
if (geojson._storage) geojson._umap_options = geojson._storage // Retrocompat.
|
if (geojson._storage) geojson._umap_options = geojson._storage // Retrocompat.
|
||||||
if (geojson._umap_options) this.setOptions(geojson._umap_options)
|
if (geojson._umap_options) this.setOptions(geojson._umap_options)
|
||||||
this.empty()
|
this.empty()
|
||||||
if (this.isRemoteLayer()) this.fetchRemoteData()
|
if (this.isRemoteLayer()) this.fetchRemoteData()
|
||||||
else this.addData(geojson)
|
else this.addData(geojson)
|
||||||
this.isDirty = true
|
this.isDirty = true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
featuresToGeoJSON: function () {
|
featuresToGeoJSON: function () {
|
||||||
|
@ -1547,7 +1557,12 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
const headers = this._last_modified
|
const headers = this._last_modified
|
||||||
? { 'If-Unmodified-Since': this._last_modified }
|
? { 'If-Unmodified-Since': this._last_modified }
|
||||||
: {}
|
: {}
|
||||||
const [data, response] = await this.map.server.post(saveUrl, headers, formData)
|
const [data, response, error] = await this.map.server.post(
|
||||||
|
saveUrl,
|
||||||
|
headers,
|
||||||
|
formData
|
||||||
|
)
|
||||||
|
if (!error) {
|
||||||
// Response contains geojson only if save has conflicted and conflicts have
|
// Response contains geojson only if save has conflicted and conflicts have
|
||||||
// been resolved. So we need to reload to get extra data (saved from someone else)
|
// been resolved. So we need to reload to get extra data (saved from someone else)
|
||||||
if (data.geojson) {
|
if (data.geojson) {
|
||||||
|
@ -1565,6 +1580,7 @@ L.U.DataLayer = L.Evented.extend({
|
||||||
this.redraw() // Needed for reordering features
|
this.redraw() // Needed for reordering features
|
||||||
this.isDirty = false
|
this.isDirty = false
|
||||||
this.permissions.save()
|
this.permissions.save()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
saveDelete: async function () {
|
saveDelete: async function () {
|
||||||
|
|
|
@ -132,13 +132,15 @@ L.U.MapPermissions = L.Class.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
attach: async function () {
|
attach: async function () {
|
||||||
await this.map.server.post(this.getAttachUrl())
|
const [data, response, error] = await this.map.server.post(this.getAttachUrl())
|
||||||
|
if (!error) {
|
||||||
this.options.owner = this.map.options.user
|
this.options.owner = this.map.options.user
|
||||||
this.map.ui.alert({
|
this.map.ui.alert({
|
||||||
content: L._('Map has been attached to your account'),
|
content: L._('Map has been attached to your account'),
|
||||||
level: 'info',
|
level: 'info',
|
||||||
})
|
})
|
||||||
this.map.ui.closePanel()
|
this.map.ui.closePanel()
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
save: async function () {
|
save: async function () {
|
||||||
|
@ -155,11 +157,17 @@ L.U.MapPermissions = L.Class.extend({
|
||||||
formData.append('owner', this.options.owner && this.options.owner.id)
|
formData.append('owner', this.options.owner && this.options.owner.id)
|
||||||
formData.append('share_status', this.options.share_status)
|
formData.append('share_status', this.options.share_status)
|
||||||
}
|
}
|
||||||
await this.map.server.post(this.getUrl(), {}, formData)
|
const [data, response, error] = await this.map.server.post(
|
||||||
|
this.getUrl(),
|
||||||
|
{},
|
||||||
|
formData
|
||||||
|
)
|
||||||
|
if (!error) {
|
||||||
this.commit()
|
this.commit()
|
||||||
this.isDirty = false
|
this.isDirty = false
|
||||||
this.map.continueSaving()
|
this.map.continueSaving()
|
||||||
this.map.fire('postsync')
|
this.map.fire('postsync')
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getUrl: function () {
|
getUrl: function () {
|
||||||
|
|
Loading…
Reference in a new issue