diff --git a/docma-config.json b/docma-config.json index 54ca08ade9..fe84840012 100644 --- a/docma-config.json +++ b/docma-config.json @@ -167,6 +167,7 @@ "web/client/utils/index.jsdoc", "web/client/utils/CoordinatesUtils.js", + "web/client/utils/LocaleUtils.js", "web/client/utils/PluginsUtils.js", "web/client/utils/PrintUtils.js", "web/client/utils/ogc/Filter/FilterBuilder.js", diff --git a/web/client/actions/maps.js b/web/client/actions/maps.js index 92f96ab0c0..b2ff8402ab 100644 --- a/web/client/actions/maps.js +++ b/web/client/actions/maps.js @@ -11,6 +11,8 @@ const {updateCurrentMapPermissions, updateCurrentMapGroups} = require('./current const ConfigUtils = require('../utils/ConfigUtils'); const assign = require('object-assign'); const {get, findIndex} = require('lodash'); +const {error: notificationError, success: notificationSuccess} = require('./notifications'); +const {getErrorMessage} = require('../utils/LocaleUtils'); const MAPS_LIST_LOADED = 'MAPS_LIST_LOADED'; const MAPS_LIST_LOADING = 'MAPS_LIST_LOADING'; @@ -407,15 +409,25 @@ function loadAvailableGroups(user) { * @param {number} resourceId the id of the map to update * @param {object} content the new content * @param {object} [options] options for the request - * @return {thunk} dispatches mapUpdating or loadError + * @return {thunk} dispatches notificationSuccess or loadError and notificationError */ function updateMap(resourceId, content, options) { return (dispatch) => { dispatch(mapUpdating(resourceId, content)); GeoStoreApi.putResource(resourceId, content, options).then(() => { - // dispatch(mapUpdated(resourceId, content, "success")); // TODO wrong usage, use another action + dispatch(notificationSuccess({ + title: 'map.savedMapTitle', + message: 'map.savedMapMessage', + autoDismiss: 6, + position: 'tc' + })); }).catch((e) => { dispatch(loadError(e)); + dispatch(notificationError({ + ...getErrorMessage(e, 'geostore', 'mapsError'), + autoDismiss: 6, + position: 'tc' + })); }); }; } @@ -626,8 +638,19 @@ function createMap(metadata, content, thumbnail, options) { } dispatch(mapCreated(response.data, assign({id: response.data, canDelete: true, canEdit: true, canCopy: true}, metadata), content)); dispatch(onDisplayMetadataEdit(false)); + dispatch(notificationSuccess({ + title: 'map.savedMapTitle', + message: 'map.savedMapMessage', + autoDismiss: 6, + position: 'tc' + })); }).catch((e) => { dispatch(mapError(e)); + dispatch(notificationError({ + ...getErrorMessage(e, 'geostore', 'mapsError'), + autoDismiss: 6, + position: 'tc' + })); }); }; } diff --git a/web/client/api/GeoStoreDAO.js b/web/client/api/GeoStoreDAO.js index 1c5d254b32..c62cff6b9f 100644 --- a/web/client/api/GeoStoreDAO.js +++ b/web/client/api/GeoStoreDAO.js @@ -11,6 +11,7 @@ const assign = require('object-assign'); const uuidv1 = require('uuid/v1'); const ConfigUtils = require('../utils/ConfigUtils'); const {utfEncode} = require('../utils/EncodeUtils'); +const {registerErrorParser} = require('../utils/LocaleUtils'); let parseOptions = (opts) => opts; @@ -33,10 +34,32 @@ const encodeContent = function(content) { return utfEncode(content); }; +const errorParser = { + /** + * Returns localized message for geostore map errors + * @param {object} e error object + * @return {object} {title, message} + */ + mapsError: e => { + if (e.status === 403 || e.status === 404 || e.status === 409 || e.status === 500) { + return { + title: 'map.mapError.errorTitle', + message: 'map.mapError.error' + e.status + }; + } + return { + title: 'map.mapError.errorTitle', + message: 'map.mapError.errorDefault' + }; + } +}; + +registerErrorParser('geostore', {...errorParser}); + /** * API for local config */ -var Api = { +const Api = { authProviderName: "geostore", addBaseUrl: function(options) { return assign(options || {}, {baseURL: ConfigUtils.getDefaults().geoStoreUrl}); @@ -351,7 +374,8 @@ var Api = { postUser.attribute = postUser.attribute && postUser.attribute.length > 0 ? [...postUser.attribute, uuidAttr] : [uuidAttr]; return postUser; } - } + }, + errorParser }; module.exports = Api; diff --git a/web/client/api/__tests__/GeoStoreDAO-test.jsx b/web/client/api/__tests__/GeoStoreDAO-test.jsx index 95d6ddc0d3..81ab3e5b7d 100644 --- a/web/client/api/__tests__/GeoStoreDAO-test.jsx +++ b/web/client/api/__tests__/GeoStoreDAO-test.jsx @@ -40,4 +40,15 @@ describe('Test correctness of the GeoStore APIs', () => { const user2 = API.utils.initUser(originalUser2); expect(user2.attribute.length).toBe(2); }); + + it('test error parser', () => { + expect(API.errorParser.mapsError({status: 409})).toEqual({ + title: 'map.mapError.errorTitle', + message: 'map.mapError.error409' + }); + expect(API.errorParser.mapsError({status: 400})).toEqual({ + title: 'map.mapError.errorTitle', + message: 'map.mapError.errorDefault' + }); + }); }); diff --git a/web/client/translations/data.de-DE b/web/client/translations/data.de-DE index cf7c5cf27d..70c7d0ebbe 100644 --- a/web/client/translations/data.de-DE +++ b/web/client/translations/data.de-DE @@ -21,6 +21,8 @@ "enable": "Aktiviere", "layers": "Ebenen", "warning": "Warnung", + "errorTitleDefault": "Fehler", + "errorDefault": "Ein Fehler ist aufgetreten", "version": { "label": "Version" }, @@ -209,6 +211,8 @@ "errorFormat": "Unterstützte Formate: png/jpg", "errorSize": "Maximal zulässige Größe: 500kb", "error": "Das bereitgestellte Bild ist ungültig", + "savedMapTitle": "Gespeicherte Karte", + "savedMapMessage": "Karte wurde korrekt gespeichert", "thumbnailError": { "error403": "Du hast keine Berechtigung um Thumbnails hochzuladen", "error404": "Es gab einen Fehler während der Erstellung des Thumbnails", @@ -216,9 +220,11 @@ "errorDefault": "Netzwerk Fehler" }, "mapError": { + "errorTitle": "Aktuelle Karte kann nicht gespeichert werden", "error403": "Du hast keine Berechtigung die Karte zu aktualisieren", "error404": "Es gab einen Fehler während der Erstellung der Karte", "error409": "Eine Karte mit diesem Namen existiert bereits", + "error500": "Interner Serverfehler. Überprüfen Sie, ob die Größe der Kartenkonfigurationsdatei den festgelegten Grenzwert überschreitet", "errorDefault": "Netzwerk Fehler" }, "permissions": { diff --git a/web/client/translations/data.en-US b/web/client/translations/data.en-US index 8d367898f7..884363393a 100644 --- a/web/client/translations/data.en-US +++ b/web/client/translations/data.en-US @@ -21,6 +21,8 @@ "enable": "Enable", "layers": "Layers", "warning": "Warning", + "errorTitleDefault": "Error", + "errorDefault": "An error occurred", "version": { "label": "Version" }, @@ -209,6 +211,8 @@ "errorFormat": "Supported formats: png/jpg", "errorSize": "Max allowed size: 500kb", "error": "The provided image is invalid", + "savedMapTitle": "Saved Map", + "savedMapMessage": "Map has been saved correctly", "thumbnailError": { "error403": "You are not allowed to update the thumbnail", "error404": "An error occurred while creating the thumbnail", @@ -216,9 +220,11 @@ "errorDefault": "Network error" }, "mapError": { + "errorTitle": "Cannot save current map", "error403": "You are not allowed to update the map", "error404": "An error occurred while creating the map", "error409": "A map with this name already exists", + "error500": "Internal Server Error. Verify if map configuration file size exceeds fixed limit", "errorDefault": "Network error" }, "permissions": { diff --git a/web/client/translations/data.es-ES b/web/client/translations/data.es-ES index c2a0dc7843..5ffb157adf 100644 --- a/web/client/translations/data.es-ES +++ b/web/client/translations/data.es-ES @@ -21,6 +21,8 @@ "enable": "Activar", "layers": "Capas", "warning": "Atención", + "errorTitleDefault": "Error", + "errorDefault": "Ocurrió un error", "version": { "label": "Versión" }, @@ -209,6 +211,8 @@ "errorFormat": "Formatos soportados: png/jpg", "errorSize": "Tamaño máximo admitido: 500kb", "error": "La imagen proporcionada es inválida", + "savedMapTitle": "Mapa Guardado", + "savedMapMessage": "El mapa se ha guardado correctamente", "thumbnailError": { "error403": "Error al modificar la viñeta", "error404": "Error al crear la viñeta", @@ -216,9 +220,11 @@ "errorDefault": "Error de red" }, "mapError": { + "errorTitle": "No se puede guardar el mapa actual", "error403": "Error al modificar el mapa", "error404": "Error al crear el mapa", "error409": "Nombre del mapa repetido", + "error500": "Error de servidor interno. Verificar si el tamaño del archivo de configuración del mapa excede el límite fijado", "errorDefault": "Error de red" }, "permissions": { diff --git a/web/client/translations/data.fr-FR b/web/client/translations/data.fr-FR index 054c51d7db..6f28e4b135 100644 --- a/web/client/translations/data.fr-FR +++ b/web/client/translations/data.fr-FR @@ -22,6 +22,8 @@ "enable": "Activer", "layers": "Couches", "warning": "Attention", + "errorTitleDefault": "Erreur", + "errorDefault": "Une erreur est survenue", "version": { "label": "Version" }, @@ -210,6 +212,8 @@ "errorFormat": "Formats supportés: png/jpg", "errorSize": "taille maximum autorisée: 500kb", "error": "L'image fournie est pas valide", + "savedMapTitle": "Carte Sauvegardée", + "savedMapMessage": "La carte a été enregistrée correctement", "thumbnailError": { "error403": "Une erreur est survenue lors de la suppression de la vignette", "error404": "Une erreur est survenue lors de la création de la vignette", @@ -217,9 +221,11 @@ "errorDefault": "Une erreur est survenue sur le serveur" }, "mapError": { + "errorTitle": "Impossible d'enregistrer la carte actuelle", "error403": "Une erreur est survenue lors de la suppression de la carte", "error404": "Une erreur est survenue lors de la création de la carte", "error409": "Nom de la carte déjà choisie", + "error500": "Erreur Interne du Serveur. Vérifier si la taille du fichier de configuration de la carte dépasse la limite fixée", "errorDefault": "Une erreur est survenue sur le serveur" }, "permissions": { diff --git a/web/client/translations/data.it-IT b/web/client/translations/data.it-IT index 6f507c63b1..3c886231c4 100644 --- a/web/client/translations/data.it-IT +++ b/web/client/translations/data.it-IT @@ -21,6 +21,8 @@ "enable": "Abilita", "layers": "Livelli", "warning": "Attenzione", + "errorTitleDefault": "Errore", + "errorDefault": "Si è verificato un errore", "version": { "label": "Versione" }, @@ -209,6 +211,8 @@ "errorFormat": "Formati supportati: png/jpg", "errorSize": "Dimensione massima consentita: 500kb", "error": "L'immagine fornita non è valida", + "savedMapTitle": "Mappa Salvata", + "savedMapMessage": "La mappa è stata salvata correttamente", "thumbnailError": { "error403": "Non disponi dei permessi per aggiornare la thumbnail", "error404": "Non è stato possibile creare la thumbnail sul server", @@ -216,9 +220,11 @@ "errorDefault": "Errore di rete" }, "mapError": { + "errorTitle": "Non è stato possibile salvare la mappa", "error403": "Non disponi dei permessi per aggiornare la mappa", "error404": "Non è stato possibile creare la mappa sul server", "error409": "Una mappa con questo nome esiste già", + "error500": "Errore interno del server. Verificare se la dimensione della configurazione di mappa supera il limite fissato", "errorDefault": "Errore di rete" }, "permissions": { diff --git a/web/client/utils/LocaleUtils.js b/web/client/utils/LocaleUtils.js index 29210c266a..26d3659a27 100644 --- a/web/client/utils/LocaleUtils.js +++ b/web/client/utils/LocaleUtils.js @@ -51,6 +51,13 @@ const DATE_FORMATS = { "it-IT": "dd/MM/yyyy", "nl-NL": "dd/MM/yyyy" }; + +let errorParser = {}; + +/** + * Utilities for locales. + * @memberof utils + */ const LocaleUtils = { ensureIntl(callback) { require.ensure(['intl', 'intl/locale-data/jsonp/en.js', 'intl/locale-data/jsonp/it.js', 'intl/locale-data/jsonp/fr.js', 'intl/locale-data/jsonp/de.js', 'intl/locale-data/jsonp/es.js', 'intl/locale-data/jsonp/nl.js'], (require) => { @@ -107,6 +114,27 @@ const LocaleUtils = { message = message ? message[part] : null; }); return message; + }, + /** + * Registre a parser to translate error services + * @param type {string} name of the service + * @param parser {object} custom parser of the service + */ + registerErrorParser: (type, parser) => { + errorParser[type] = parser; + }, + /** + * Return localized id of error messages + * @param e {object} error + * @param service {string} service that thrown the error + * @param section {string} section where the error happens + * @return {object} {title, message} + */ + getErrorMessage: (e, service, section) => { + return service && section && errorParser[service] && errorParser[service][section] && errorParser[service][section](e) || { + title: 'errorTitleDefault', + message: 'errorDefault' + }; } }; diff --git a/web/client/utils/__tests__/LocaleUtils-test.js b/web/client/utils/__tests__/LocaleUtils-test.js index 8673e28c21..5fb9437ccb 100644 --- a/web/client/utils/__tests__/LocaleUtils-test.js +++ b/web/client/utils/__tests__/LocaleUtils-test.js @@ -34,4 +34,19 @@ describe('LocaleUtils', () => { it('getSupportedLocales', () => { expect(LocaleUtils.getSupportedLocales()).toExist(); }); + + it('getErrorMessage', () => { + expect(LocaleUtils.getErrorMessage({status: 409}, 'geostore', 'mapsError')).toEqual({ + title: 'map.mapError.errorTitle', + message: 'map.mapError.error409' + }); + expect(LocaleUtils.getErrorMessage({status: 0}, 'geostore', 'mapsError')).toEqual({ + title: 'map.mapError.errorTitle', + message: 'map.mapError.errorDefault' + }); + expect(LocaleUtils.getErrorMessage()).toEqual({ + title: 'errorTitleDefault', + message: 'errorDefault' + }); + }); });