All files / ext.wikilambda.edit/mixins errorUtils.js

85.71% Statements 12/14
62.5% Branches 10/16
83.33% Functions 5/6
85.71% Lines 12/14

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 86 87 88                  38x 38x 38x   38x   152x                                                       91x 91x 65x                         5x                             311x 311x                 311x        
/**
 * WikiLambda Vue editor: Error utils mixin
 * Mixin with util functions to handle component errors
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../Constants.js' ),
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = {
	data: function () {
		return {
			localErrors: []
		};
	},
	methods: $.extend( {}, mapActions( [
		'clearErrors'
	] ), {
		/**
		 * Set a local error for the component.
		 *
		 * @param {Object} payload
		 */
		setLocalError: function ( payload ) {
			const error = {
				type: payload.type || Constants.errorTypes.ERROR,
				code: payload.code || undefined,
				message: payload.message || undefined
			};
			this.localErrors.push( error );
		},
 
		/**
		 * Clear local errors.
		 * If this.rowId is associated to a field (is defined
		 * and not zero), clear the errors associated to this
		 * component.
		 */
		clearFieldErrors: function () {
			this.localErrors = [];
			if ( this.rowId && ( this.rowId > 0 ) ) {
				this.clearErrors( this.rowId );
			}
		},
 
		/**
		 * Returns the translated message for a given error code.
		 * Error messages can have html tags.
		 *
		 * @param {Object} error
		 * @return {string}
		 */
		getErrorMessage: function ( error ) {
			// eslint-disable-next-line mediawiki/msg-doc
			return error.message || this.$i18n( error.code ).text();
		}
	} ),
	computed: $.extend( {}, mapGetters( [
		'getErrors'
	] ), {
		/**
		 * Returns the errors of the component rowId. This is
		 * used for field errors, not for page errors. For that
		 * reason, if rowId is 0 or not passed, it will return
		 * an empty array.
		 *
		 * @return {Array}
		 */
		fieldErrors: function () {
			const globalErrors = ( this.rowId && ( this.rowId > 0 ) ) ? this.getErrors( this.rowId ) : [];
			return globalErrors.concat( this.localErrors );
		},
 
		/**
		 * Returns whether the component is in an error state.
		 *
		 * @return {boolean}
		 */
		hasFieldErrors: function () {
			return ( this.fieldErrors.length > 0 );
		}
	} )
};