All files / web/bundles/pimui/js error.js

44% Statements 11/25
25% Branches 3/12
33.33% Functions 2/6
44% Lines 11/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86910x       910x                   910x           910x     910x 1088x 1088x 1038x     1088x     910x                                                                                                     910x    
define(['underscore', 'backbone', 'routing', 'oro/app'],
function (_, Backbone, routing, app) {
    'use strict';
 
    var defaults = {
        header: 'Server error',
        message: 'Error! Incorrect server response.',
        forbiddenAccess: 'You don\'t have the permission to open this page'
    };
 
    /**
     * @export oro/error
     * @name oro.error
     */
    var error = {
        dispatch: function (model, xhr, options) {
            var self = error.dispatch;
            self.init(model, xhr, _.extend({}, defaults, options));
        }
    };
    var sync = Backbone.sync;
 
    // Override default Backbone.sync
    Backbone.sync = function (method, model, options) {
        options = options || {};
        if (!_.has(options, 'error')) {
            options.error = error.dispatch;
        }
 
        sync.call(Backbone, method, model, options);
    };
 
    _.extend(error.dispatch, {
        /**
         * Error dispatch
         *
         * @param {Object} model
         * @param {Object} xhr
         * @param {Object} options
         */
        init: function (model, xhr, options) {
            if (xhr.status === 401) {
                this._processRedirect();
            } else if (xhr.readyState === 4) {
                if (xhr.status === 403) {
                    options.message = options.forbiddenAccess;
                }
                this._processModal(xhr, options);
            }
        },
 
        /**
         * Shows modal window
         * @param {Object} xhr
         * @param {Object} options
         * @private
         */
        _processModal: function (xhr, options) {
            var modal;
            var message = options.message;
            if (app.debug) {
                message += '<br><b>Debug:</b>' + xhr.responseText;
            }
 
            modal = new Backbone.BootstrapModal({
                title: options.header,
                content: message,
                buttonClass: 'AknButton--important',
                cancelText: '',
            });
 
            modal.open();
        },
 
        /**
         * Redirects to login
         * @private
         */
        _processRedirect: function () {
            document.location.href = routing.generate('pim_user_security_login');
        }
    });
 
    return error;
});