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

98.13% Statements 105/107
81.81% Branches 18/22
100% Functions 8/8
98.13% Lines 105/107

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 1087x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 235x 235x 235x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 60x 60x 33x 33x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 24x 24x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 1x 1x 1x 1x 1x 2x     1x 1x 2x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 200x 200x 7x 7x 7x 7x 7x 7x 7x 7x 200x 200x 7x 7x  
/**
 * 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, mapGetters } = require( 'vuex' );
 
module.exports = exports = {
	data: function () {
		return {
			localErrors: []
		};
	},
	methods: Object.assign( {}, 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();
		},
 
		/**
		 * Given an error response of a failed call to the API,
		 * extract the error message per type of error.
		 *
		 * @param {Object} error
		 * @return {string | undefined}
		 */
		extractErrorMessage: function ( error ) {
			if ( !error.zerror ) {
				return undefined;
			}
 
			const errorMessage = error.zerror[ Constants.Z_ERROR_VALUE ][ Constants.Z_TYPED_OBJECT_ELEMENT_1 ];
			const errorType = error.zerror[ Constants.Z_ERROR_TYPE ];
			if ( !errorMessage || typeof errorMessage !== 'string' || errorType !== Constants.Z_GENERIC_ERROR ) {
				return;
			}
 
			return errorMessage;
		}
	} ),
	computed: Object.assign( {}, 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 );
		}
	} )
};