Skip to content
Snippets Groups Projects
Select Git revision
  • 22fbe664abcf77ae428200c7b8a312a7b63bd057
  • 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

utils.js

Blame
  • 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;
                }
            }