Skip to content
Snippets Groups Projects
Select Git revision
  • 661bea8898da8e2778627154961f087e76dd1aba
  • main default protected
  • renovate/lock-file-maintenance
  • demo protected
  • person-select-custom
  • dbp-translation-component
  • icon-set-mapping
  • port-i18next-parser
  • remove-sentry
  • favorites-and-recent-files
  • revert-6c632dc6
  • lit2
  • advertisement
  • wc-part
  • automagic
  • publish
  • wip-cleanup
  • demo-file-handling
18 results

rollup.utils.js

Blame
    • Reiter, Christoph's avatar
      68a87639
      Use the rollup resolve plugin for finding package asssets · 68a87639
      Reiter, Christoph authored
      Instead of hardcoding the paths of the node_modules directory we use the node-resolve
      plugin of rollup to find the root of the corresponding JS package and calcucate a path from
      there.
      
      Resolving a package requires calling an async function, so we have to use await in the rollup config.
      Luckily rollup supports configs wrapped in a promise, so we just have to wrap it in a function which returns
      a promise.
      68a87639
      History
      Use the rollup resolve plugin for finding package asssets
      Reiter, Christoph authored
      Instead of hardcoding the paths of the node_modules directory we use the node-resolve
      plugin of rollup to find the root of the corresponding JS package and calcucate a path from
      there.
      
      Resolving a package requires calling an async function, so we have to use await in the rollup config.
      Luckily rollup supports configs wrapped in a promise, so we just have to wrap it in a function which returns
      a promise.
    error.js 4.51 KiB
    import {send as notify} from './notification';
    import {createInstance} from './src/i18n';
    
    /**
     * Escapes html
     *
     * @param string
     * @returns {string}
     */
    export const escapeHTML = (string) => {
        const pre = document.createElement('pre');
        const text = document.createTextNode(string);
        pre.appendChild(text);
    
        return pre.innerHTML;
    };
    
    /**
     * Strips html
     *
     * @param string
     * @returns {string}
     */
    export const stripHTML = (string) => {
        var div = document.createElement('div');
        div.innerHTML = string;
    
        return div.textContent || div.innerText || '';
    };
    
    /**
     * We need this mixin so we can use this.sendSetPropertyEvent to post analytics events
     */
    export const errorMixin = {
        /**
         * Error handling for XHR errors
         *
         * @param jqXHR
         * @param textStatus
         * @param errorThrown
         * @param icon
         * @param lang
         */
        handleXhrError(jqXHR, textStatus, errorThrown, icon = 'sad', lang = 'de') {
            // return if user aborted the request
            if (textStatus === 'abort') {
                return;
            }
    
            let body;
            const i18n = createInstance();
            i18n.changeLanguage(lang);
    
            if (
                jqXHR.responseJSON !== undefined &&
                jqXHR.responseJSON['hydra:description'] !== undefined
            ) {
                // response is a JSON-LD
                body = jqXHR.responseJSON['hydra:description'];
            } else if (jqXHR.responseJSON !== undefined && jqXHR.responseJSON['detail'] !== undefined) {
                // response is a plain JSON
                body = jqXHR.responseJSON['detail'];
            } else {
                // no description available
                body = textStatus;
    
                if (errorThrown) {
                    body += ' - ' + errorThrown;
                }
            }
    
            // if the server is not reachable at all
            if (jqXHR.status === 0) {
                body = i18n.t('error.connection-to-server-refused');
            }
    
            notify({
                summary: i18n.t('error.summary'),
                body: escapeHTML(stripHTML(body)),
                icon: icon,
                type: 'danger',
            });
    
            if (this.sendSetPropertyEvent !== undefined) {
                this.sendSetPropertyEvent('analytics-event', {category: 'XhrError', action: body});
            }
        },
    
        /**
         * Error handling for fetch errors
         *
         * @param error
         * @param summary
         * @param icon
         * @param lang
         */
        handleFetchError: async function (error, summary = '', icon = 'sad', lang = 'de') {
            // return if user aborted the request
            if (error.name === 'AbortError') {
                return;
            }
    
            let body;
            const i18n = createInstance();
            i18n.changeLanguage(lang);
    
            try {
                await error
                    .json()
                    .then((json) => {
                        if (json['hydra:description'] !== undefined) {
                            // response is a JSON-LD and possibly also contains HTML!
                            body = json['hydra:description'];
                        } else if (json['detail'] !== undefined) {
                            // response is a plain JSON
                            body = json['detail'];
                        } else {
                            // no description available
                            body = error.statusText;
                        }
                    })
                    .catch(() => {
                        body = error.statusText !== undefined ? error.statusText : error;
                    });
            } catch (e) {
                // a TypeError means the connection to the server was refused most of the times
                if (error.name === 'TypeError') {
                    body =
                        error.message !== ''
                            ? error.message
                            : i18n.t('error.connection-to-server-refused');
                }
            }
    
            notify({
                summary: summary === '' ? i18n.t('error.summary') : summary,
                body: escapeHTML(stripHTML(body)),
                icon: icon,
                type: 'danger',
            });
    
            if (this.sendSetPropertyEvent !== undefined) {
                this.sendSetPropertyEvent('analytics-event', {
                    category: 'FetchError',
                    action: summary === '' ? body : summary + ': ' + body,
                });
            }
        },
    };
    
    /**
     * Returns the stack trace as array
     *
     * @returns {string[]}
     */
    export const getStackTrace = () => {
        let stack = new Error().stack || '';
        stack = stack.split('\n').map(function (line) {
            return line.trim();
        });
        return stack.splice(stack[0] === 'Error' ? 2 : 1);
    };