<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Custom Error Handler Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/custom-error-handler/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/custom-error-handler/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 12:46:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>How to log knockout.js errors</title>
		<link>https://blogit.create.pt/joaopais/2016/11/29/how-to-log-knockout-js-errors/</link>
					<comments>https://blogit.create.pt/joaopais/2016/11/29/how-to-log-knockout-js-errors/#comments</comments>
		
		<dc:creator><![CDATA[João Pais]]></dc:creator>
		<pubDate>Tue, 29 Nov 2016 17:52:09 +0000</pubDate>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Custom Binding Provider]]></category>
		<category><![CDATA[Custom Error Handler]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Knockout]]></category>
		<category><![CDATA[knockout.js]]></category>
		<category><![CDATA[Log]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/joaopais/?p=461</guid>

					<description><![CDATA[<p>How to log knockout.js errors? Right now I&#8217;m working in a banking area project. It&#8217;s a web project and we are using Knockout.JS framework. We (developers) don&#8217;t have access to the project front-end in Production Environment. On the server side we log all errors and debug information into a table in our database. Yes, we [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/joaopais/2016/11/29/how-to-log-knockout-js-errors/">How to log knockout.js errors</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>How to log knockout.js errors?</p>
<p>Right now I&#8217;m working in a banking area project. It&#8217;s a web project and we are using Knockout.JS framework.<br />
We (developers) don&#8217;t have access to the project front-end in Production Environment. On the server side we log all errors and debug information into a table in our database. Yes, we don&#8217;t have access to our project front-end but we are able to consult (and only with read privileges) our database.</p>
<p>So, when happens a client side error in Production Environment, it&#8217;s pretty hard to figure out what&#8217;s wrong. All we have is a printscreen from the user.</p>
<p>Sometimes it&#8217;s impossible to reproduce the Production Environment error because all the development environments (LOC-DEV-STA-PRE) have different data information and different code versions with different develepments in comparison with Production Environment. So, we really need to get detailed information about client-side errors.</p>
<p>In this post, I&#8217;m going to show you how to log <strong>only</strong> Knockout.JS errors. So, general javascript errors will not be discussed here.</p>
<p>First of all, we need to write an Error Handling Binding Provider. For more information about Custom Bindings Providers please read this <a target="_blank" href="http://www.knockmeout.net/2011/09/ko-13-preview-part-2-custom-binding.html">blog post</a>. Please be carefull because something may have changed because it&#8217;s based on an old knockout.JS version).</p>
<pre class="brush: jscript; title: ; notranslate">

// Custom Error Handler.
var MyErrorBindingProvider = function () {

	// Create a binding provider.
    var original = new ko.bindingProvider();

    // check if the element has bindings
    this.nodeHasBindings = original.nodeHasBindings;

    //return the bindings given a node and the bindingContext
    this.getBindingAccessors = function (node, bindingContext) {
        var result = {};

        // catch syntax errors when parsing binding 
        try {
            result = original.getBindingAccessors(node, bindingContext);
        }
		// Something wrong happened...
        catch (e) {
            // do whatever you want. (alert, debug, console.log, send it to server, etc)			
            if (console &amp;&amp; console.log) {
                console.log(&quot;Error occurred in binding syntax: &quot; + e.message, node);
            }
        }

        // catch errors when evaluating the value of a binding
        ko.utils.objectForEach(result, function (key, value) {
            result&#x5B;key] = function () {
                var result = null;

                try {
                    result = value();
                }
				// Something wrong happened...
                catch (e) {
					// do whatever you want. (alert, debug, console.log, send it to server, etc)
                    if (console &amp;&amp; console.log) {	
                        console.log(&quot;Error occurred in \&quot;&quot; + key + &quot;\&quot; binding: &quot; + e.message, node);
                    }
                }

                return result;
            };
        });
        return result;
    };
};
</pre>
<p>Now that we have our Custom Binding Provider, we need to instantiate it.<br />
Just add it in your document.ready function where you do your applyBindings and etc:</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function () {

	// my viewModel
	var viewModel = {
		firstName: ko.observable(&quot;Joao&quot;)
	};
	
	// Error Subscription while observables values changed
     ko.bindingProvider.instance = new MyErrorBindingProvider();
	
	// Apply Bindings.
	ko.applyBindings(viewModel);
});
</pre>
<p>As you have read in the begining of this post, I wrote that we don&#8217;t have access to Production environment, so we sent any knockout error to server side in order to be stored in a database table and then we are able to consult it. This can be done with a simple AJAX call. Just send what it&#8217;s been showed on console.log in this example.</p>
<p>Finally, here you have an HTML sample to try it by yourself:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Knockout.JS Error Handling&lt;/title&gt;

&lt;script type='text/javascript' src='https://code.jquery.com/jquery-3.1.0.min.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript' src='https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.3.0.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript' src='Index.js'&gt;&lt;/script&gt;


&lt;/head&gt;

&lt;body&gt;

&lt;div&gt;   
    &lt;p&gt;First name: &lt;input data-bind='textInput: firstName' /&gt;&lt;/p&gt;
	&lt;span&gt;Hello &lt;/span&gt;&lt;span data-bind='text: firstName'&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now, let&#8217;s see the difference between using our error handler and not using it.<br />
For instance, if you forget to close your first data-bind statement:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div&gt;   
    &lt;p&gt;First name: &lt;input data-bind='textInput: firstName /&gt;&lt;/p&gt;
	&lt;span&gt;Hello &lt;/span&gt;&lt;span data-bind='text: firstName'&gt;&lt;/span&gt;
&lt;/div&gt;
</pre>
<p><strong>With </strong>our error handler:<br />
<a target="_blank" href="http://blogit-create.com/wp-content/uploads/2016/11/1.png"><img decoding="async" src="http://blogit-create.com/wp-content/uploads/2016/11/1-300x42.png" alt="1" width="300" height="42" class="alignnone size-medium wp-image-221" /></a></p>
<p><strong>Without </strong>our error handler:<br />
<a target="_blank" href="http://blogit-create.com/wp-content/uploads/2016/11/3.png"><img fetchpriority="high" decoding="async" src="http://blogit-create.com/wp-content/uploads/2016/11/3-300x223.png" alt="3" width="300" height="223" class="alignnone size-medium wp-image-231" srcset="https://blogit.create.pt/wp-content/uploads/2016/11/3-300x223.png 300w, https://blogit.create.pt/wp-content/uploads/2016/11/3-80x60.png 80w, https://blogit.create.pt/wp-content/uploads/2016/11/3-265x198.png 265w, https://blogit.create.pt/wp-content/uploads/2016/11/3-566x420.png 566w, https://blogit.create.pt/wp-content/uploads/2016/11/3.png 567w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>For instance, if you are trying to data-bind an undefined observable:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div&gt;   
    &lt;p&gt;First name: &lt;input data-bind='textInput: firstName' /&gt;&lt;/p&gt;
	&lt;span&gt;Hello &lt;/span&gt;&lt;span data-bind='text: lastName'&gt;&lt;/span&gt;
&lt;/div&gt;
</pre>
<p><strong>With </strong>our error handler:<br />
<a target="_blank" href="http://blogit-create.com/wp-content/uploads/2016/11/2.png"><img decoding="async" src="http://blogit-create.com/wp-content/uploads/2016/11/2-300x24.png" alt="2" width="300" height="24" class="alignnone size-medium wp-image-241" /></a></p>
<p><strong>Without </strong>our error handler:<br />
<a target="_blank" href="http://blogit-create.com/wp-content/uploads/2016/11/4.png"><img decoding="async" src="http://blogit-create.com/wp-content/uploads/2016/11/4-300x212.png" alt="4" width="300" height="212" class="alignnone size-medium wp-image-251" srcset="https://blogit.create.pt/wp-content/uploads/2016/11/4-300x212.png 300w, https://blogit.create.pt/wp-content/uploads/2016/11/4-100x70.png 100w, https://blogit.create.pt/wp-content/uploads/2016/11/4.png 567w" sizes="(max-width: 300px) 100vw, 300px" /></a></p>
<p>That&#8217;s it. I hope you enjoy it and have learn something.</p>
<p>The post <a href="https://blogit.create.pt/joaopais/2016/11/29/how-to-log-knockout-js-errors/">How to log knockout.js errors</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/joaopais/2016/11/29/how-to-log-knockout-js-errors/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
