chore: ServerRequest returns an explicit error if any

This commit is contained in:
Yohan Boniface 2024-01-24 13:27:33 +01:00
parent c50be398ab
commit b947134fa2
6 changed files with 172 additions and 125 deletions

View file

@ -17,15 +17,15 @@ const BaseRequest = Evented.extend({
} catch (error) {
this._onError(error)
this.fire('dataload', { id: id })
return
return null
}
if (!response.ok) {
this.onNok(response.status, await response.text())
this.fire('dataload', { id: id })
return this.onNok(response.status, response)
}
// TODO
// - error handling
// - UI connection / events
// - preflight mode in CORS ?
this.fire('dataload', { id: id })
return response
@ -44,7 +44,9 @@ const BaseRequest = Evented.extend({
this.onError(error)
},
onError: function (error) {},
onNok: function (status) {},
onNok: function (status, reponse) {
return response
},
})
export const Request = BaseRequest.extend({
@ -55,8 +57,9 @@ export const Request = BaseRequest.extend({
console.error(error)
this.ui.alert({ content: L._('Problem in the response'), level: 'error' })
},
onNok: function (status, message) {
onNok: function (status, response) {
this.onError(message)
return response
},
})
@ -81,26 +84,30 @@ export const ServerRequest = Request.extend({
headers['X-CSRFToken'] = token
}
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) {
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 {
const data = await response.json()
this._handle_server_instructions(data)
return [data, response]
if (this._handle_server_instructions(data) !== false) {
return [{}, null]
}
return [data, response, null]
} catch (error) {
this._onError(error)
return [{}, null, error]
}
},
_handle_server_instructions: function (data) {
// In some case, the response contains instructions
// Generic cases, let's deal with them once
if (data.redirect) {
const newPath = data.redirect
if (window.location.pathname == newPath) {
@ -122,6 +129,9 @@ export const ServerRequest = Request.extend({
this.fire('login')
win.close()
}
} else {
// Nothing to do, we can let the response proceed
return false
}
},

View file

@ -55,10 +55,16 @@ L.U.DataLayerPermissions = L.Class.extend({
if (!this.isDirty) return this.datalayer.map.continueSaving()
const formData = new FormData()
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.isDirty = false
this.datalayer.map.continueSaving()
}
},
commit: function () {

View file

@ -695,12 +695,14 @@ L.FormBuilder.IconUrl = L.FormBuilder.BlurInput.extend({
if (this.pictogram_list) {
this.buildSymbolsList()
} 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
)
if (!error) {
this.pictogram_list = pictogram_list
this.buildSymbolsList()
}
}
},
showCharsTab: function () {

View file

@ -34,7 +34,7 @@ L.Map.mergeOptions({
// we cannot rely on this because of the y is overriden by Leaflet
// See https://github.com/Leaflet/Leaflet/pull/9201
// 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: [],
licence: '',
enableMarkerDraw: true,
@ -838,7 +838,10 @@ L.U.Map.include({
self.isDirty = true
}
if (this._controls.tilelayersChooser)
this._controls.tilelayersChooser.openSwitcher({ callback: callback, className: 'dark' })
this._controls.tilelayersChooser.openSwitcher({
callback: callback,
className: 'dark',
})
},
manageDatalayers: function () {
@ -1097,7 +1100,8 @@ L.U.Map.include({
formData.append('center', JSON.stringify(this.geometry()))
formData.append('settings', JSON.stringify(geojson))
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,
alert = { content: L._('Map has been saved!'), level: 'info' }
if (!this.options.umap_id) {
@ -1150,6 +1154,7 @@ L.U.Map.include({
this.once('saved', () => this.ui.alert(alert))
this.ui.closePanel()
this.permissions.save()
}
},
save: function () {

View file

@ -674,7 +674,8 @@ L.U.DataLayer = L.Evented.extend({
if (!this.umap_id) return
if (this._loading) return
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']
// FIXME: for now this property is set dynamically from backend
// 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.fire('loaded')
this._loading = false
}
},
fromGeoJSON: function (geojson) {
@ -749,8 +751,10 @@ L.U.DataLayer = L.Evented.extend({
url = this.map.proxyUrl(url, this.options.remoteData.ttl)
const response = await this.map.request.get(url)
this.clear()
this.rawToGeoJSON(await response.text(), this.options.remoteData.format, (geojson) =>
this.fromGeoJSON(geojson)
this.rawToGeoJSON(
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'), {
callback: async function () {
const [{versions}, response] = await this.map.server.get(this.getVersionsUrl())
versions.forEach(appendVersion)
const [{ versions }, response, error] = await this.map.server.get(
this.getVersionsUrl()
)
if (!error) versions.forEach(appendVersion)
},
context: this,
})
@ -1401,13 +1407,17 @@ L.U.DataLayer = L.Evented.extend({
restore: async function (version) {
if (!this.map.editEnabled) 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._umap_options) this.setOptions(geojson._umap_options)
this.empty()
if (this.isRemoteLayer()) this.fetchRemoteData()
else this.addData(geojson)
this.isDirty = true
}
},
featuresToGeoJSON: function () {
@ -1547,7 +1557,12 @@ L.U.DataLayer = L.Evented.extend({
const headers = 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
// been resolved. So we need to reload to get extra data (saved from someone else)
if (data.geojson) {
@ -1565,6 +1580,7 @@ L.U.DataLayer = L.Evented.extend({
this.redraw() // Needed for reordering features
this.isDirty = false
this.permissions.save()
}
},
saveDelete: async function () {

View file

@ -132,13 +132,15 @@ L.U.MapPermissions = L.Class.extend({
},
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.map.ui.alert({
content: L._('Map has been attached to your account'),
level: 'info',
})
this.map.ui.closePanel()
}
},
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('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.isDirty = false
this.map.continueSaving()
this.map.fire('postsync')
}
},
getUrl: function () {