<commit_before>// dom.postable

(function(window) {
    "use strict";

    var axios   = window.axios;
    var dom     = window.dom;
    var Fn      = window.Fn;
    var get     = Fn.get;

    // Define

    var matches = dom.matches('.postable, [postable]');

    function toSelector(str) {
		return '[name="' + str + '"]';
	}

    function flattenErrors(object) {
		var errors = [];

		// Flatten errors into a list
		for (name in object) {
			errors.push.apply(errors,
				object[name].map(function(text) {
					return {
						name: name,
						text: text,
                        selector: toSelector(name)
					};
				})
			);
		}

		return errors;
	}

    // Functions
    dom.events('submit', document)
        .filter(function(event){
            return matches(event.target);
        })
        .tap(dom.preventDefault)
        .map(get('target'))
        .each(function(form){
            var action = form.getAttribute('action');
            var post_data = new FormData(form);

            axios
            .post(action, post_data)
            .then(function(response){
                if(response.status < 300) {
                    dom.events.trigger(form, 'dom-posted', {detail: response.data});
                }
                else {
                    var error = new Error(response.statusText);
                    error.response = response;
                    dom.events.trigger(form, 'dom-error', {detail: error});

                    if (typeof response.errors === 'object') {

                        flattenErrors(response.errors)
                        .forEach(function(input) {
                            var input = dom.find(error.selector, form);

                            if (!input) {
                                console.warn('Error given for non-existent field name="' + error.name + '"', error);
                                return;
                            }

                            input.setCustomValidity(error.text);
                        });

                        form.checkValidity();
                    }
                }
            })
            .catch(function(error_response){
                dom.events.trigger(form, 'dom-error', {detail: error_response})
            });
        });

})(this);<commit_msg>refactored error handling in postable<commit_after>// dom.postable

(function(window) {
    "use strict";

    var axios   = window.axios;
    var dom     = window.dom;
    var Fn      = window.Fn;
    var get     = Fn.get;

    // Define

    var matches = dom.matches('.postable, [postable]');

    function toSelector(str) {
		return '[name="' + str + '"]';
	}

    function flattenErrors(object) {
		var errors = [];

		// Flatten errors into a list
		for (name in object) {
			errors.push.apply(errors,
				object[name].map(function(text) {
					return {
						name: name,
						text: text,
                        selector: toSelector(name)
					};
				})
			);
		}

		return errors;
	}

    function handlerErrors(response, form) {
        flattenErrors(response.errors)
        .forEach(function(error) {
            var input = dom.find(error.selector, form);

            if (!input) {
                console.warn('Error given for non-existent field name="' + error.name + '"', error);
                return;
            }

            input.setCustomValidity(error.text);
        });

        form.checkValidity();
    }

    // Functions
    dom.events('submit', document)
        .filter(function(event){
            return matches(event.target);
        })
        .tap(dom.preventDefault)
        .map(get('target'))
        .each(function(form){
            var action = form.getAttribute('action');
            var post_data = new FormData(form);

            axios
            .post(action, post_data)
            .then(function(response){
                if(response.status < 300) {
                    dom.events.trigger(form, 'dom-posted', {detail: response.data});
                }
                else {
                    var error = new Error(response.statusText);
                    error.response = response;
                    dom.events.trigger(form, 'dom-error', {detail: error});

                    if (typeof response.errors === 'object') {
                        handlerErrors(response, form);
                    }
                }
            })
            .catch(function(error_response){
                dom.events.trigger(form, 'dom-error', {detail: error_response});
                if (error_response.response && error_response.response.data && typeof error_response.response.data.errors === 'object') {
                    handlerErrors(error_response.response.data, form);
                }
            });
        });

})(this);
{'fraction_non_alphanumeric': 0.12526227444397817, 'fraction_numerical': 0.001258917331095258, 'mean_word_length': 1.448382126348228, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3697019', 'n_tokens_mistral': 1384, 'n_tokens_neox': 1235, 'n_words': 250}