55 * @author Vaibhav Mehta <vaibhav@decodingweb.com>
66 * @copyright Copyright (c) 2016 Vaibhav Mehta <https://github.com/i-break-codes>
77 * @license http://www.opensource.org/licenses/mit-license.html MIT License
8- * @version 1.2 Stable
8+ * @version 1.2.1 Stable
99 */
1010
11- var Logerr = function ( ) {
12- 'use strict' ;
13-
11+ window . Logerr = ( function Logerr ( ) {
1412 var setConfig ;
1513
16- function init ( userConfig ) {
17- if ( ! userConfig ) userConfig = { } ;
14+ function errorData ( e ) {
15+ var filename = e . filename . lastIndexOf ( '/' ) ;
16+ var datetime = new Date ( ) . toString ( ) ;
1817
19- // Default configuration
20- var config = {
21- detailedErrors : true ,
22- remoteLogging : false ,
23- remoteSettings : {
24- url : null ,
25- additionalParams : null ,
26- successCallback : null ,
27- errorCallback : null
28- }
18+ /**
19+ * userAgent only for POST request purposes, not required in pretty print
20+ */
21+ return {
22+ type : e . type ,
23+ path : e . filename ,
24+ filename : e . filename . substring ( ++ filename ) ,
25+ line : e . lineno ,
26+ column : e . colno ,
27+ error : e . message ,
28+ stackTrace : ( ( e . error ) ? e . error . stack . toString ( ) . replace ( / ( \r \n | \n | \r ) / gm, '' ) : '' ) ,
29+ datetime : datetime ,
30+ userAgent : navigator . userAgent || window . navigator . userAgent
2931 } ;
30-
31- // Override with user config
32- setConfig = Object . assign ( config , userConfig ) ;
33-
34- //Remove current listener
35- window . removeEventListener ( 'error' , _listener ) ;
36-
37- // Listen to errors
38- window . addEventListener ( 'error' , _listener ) ;
39- }
40-
41- // NOTE: Private
42- function _listener ( e ) {
43- if ( setConfig . detailedErrors ) {
44- _detailedErrors ( e ) ;
45- }
46-
47- if ( setConfig . remoteLogging ) {
48- _remoteLogging ( e , setConfig . remoteSettings ) ;
49- }
5032 }
5133
52- function _detailedErrors ( e ) {
53- var i = _errorData ( e ) ;
54- var helpPath = encodeURI ( " https://stackoverflow.com/search?q=" + i . error . split ( ' ' ) . join ( '+' ) ) ;
34+ function detailedErrors ( e ) {
35+ var i = errorData ( e ) ;
36+ var helpPath = encodeURI ( ' https://stackoverflow.com/search?q=' + i . error . split ( ' ' ) . join ( '+' ) ) ;
5537
5638 var str = [
57- "%cType: %c" + i . type ,
58- "%cError: %c" + i . error ,
59- "%cStackTrace: %c" + i . stackTrace ,
60- "%cFile Name: %c" + i . filename ,
61- "%cPath: %c" + i . path ,
62- "%cLine: %c" + i . line ,
63- "%cColumn: %c" + i . column ,
64- "%cDate: %c" + i . datetime ,
65- "%cDebug : %c" + i . path + ':' + i . line ,
66- "%cGet Help: " + "%c" + helpPath
67- ] . join ( "\n" ) ;
68-
69- if ( window . chrome ) {
70- console . log ( str , "font-weight: bold;" , "color: #e74c3c;" , "font-weight: bold;" , "font-weight: normal; color: #e74c3c;" , "font-weight: bold;" , "font-weight: normal; color: #e74c3c;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal;" , "font-weight: bold;" , "font-weight: normal; color: #3498db;" ) ;
39+ '%cType: %c' + i . type ,
40+ '%cError: %c' + i . error ,
41+ '%cStackTrace: %c' + i . stackTrace ,
42+ '%cFile Name: %c' + i . filename ,
43+ '%cPath: %c' + i . path ,
44+ '%cLine: %c' + i . line ,
45+ '%cColumn: %c' + i . column ,
46+ '%cDate: %c' + i . datetime ,
47+ '%cDebug : %c' + i . path + ':' + i . line ,
48+ '%cGet Help: %c' + helpPath
49+ ] . join ( '\n' ) ;
50+
51+ if ( window . chrome ) {
52+ /* eslint-disable max-len */
53+ console . log ( str , 'font-weight: bold;' , 'color: #e74c3c;' , 'font-weight: bold;' , 'font-weight: normal; color: #e74c3c;' , 'font-weight: bold;' , 'font-weight: normal; color: #e74c3c;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal;' , 'font-weight: bold;' , 'font-weight: normal; color: #3498db;' ) ;
54+ /* eslint-enable max-len */
7155 } else {
7256 console . log ( str . replace ( / % c / gi, '' ) ) ;
7357 }
7458 }
7559
76- function _remoteLogging ( e , remoteSettings ) {
77- if ( ! remoteSettings . url ) {
60+ function serializeData ( params ) {
61+ function encodeKey ( k ) {
62+ return encodeURIComponent ( k ) + '=' + encodeURIComponent ( params [ k ] ) ;
63+ }
64+
65+ return Object . keys ( params ) . map ( encodeKey ) . join ( '&' ) ;
66+ }
67+
68+ function remoteLogging ( e , remoteSettings ) {
69+ if ( ! remoteSettings . url ) {
7870 throw new Error ( 'Provide remote URL to log errors remotely' ) ;
79- } else if ( remoteSettings . additionalParams && typeof remoteSettings . additionalParams !== 'object' ) {
71+ } else if (
72+ remoteSettings . additionalParams &&
73+ typeof remoteSettings . additionalParams !== 'object'
74+ ) {
8075 throw new Error ( 'Invalid data type, additionalParams should be a valid object' ) ;
8176 }
8277
8378 var http = new XMLHttpRequest ( ) ;
8479 var url = remoteSettings . url ;
85- var data = _errorData ( e ) ;
80+ var data = errorData ( e ) ;
8681 var setData = Object . assign ( data , remoteSettings . additionalParams ) ;
87- var params = _serializeData ( setData ) ;
82+ var params = serializeData ( setData ) ;
8883
89- http . open ( " POST" , url , true ) ;
90- http . setRequestHeader ( " Content-type" , " application/x-www-form-urlencoded" ) ;
84+ http . open ( ' POST' , url , true ) ;
85+ http . setRequestHeader ( ' Content-type' , ' application/x-www-form-urlencoded' ) ;
9186 http . send ( params ) ;
9287
93- http . onreadystatechange = function ( ) {
94- if ( http . readyState == 4 && http . status == 200 ) {
95- if ( http . readyState == XMLHttpRequest . DONE ) {
96- if ( remoteSettings . successCallback ) {
88+ http . onreadystatechange = function onreadystatechange ( ) {
89+ if ( http . readyState === 4 && http . status = == 200 ) {
90+ if ( http . readyState === XMLHttpRequest . DONE ) {
91+ if ( remoteSettings . successCallback ) {
9792 remoteSettings . successCallback ( ) ;
9893 }
9994 } else {
100- if ( remoteSettings . errorCallback ) {
95+ if ( remoteSettings . errorCallback ) {
10196 remoteSettings . errorCallback ( ) ;
10297 } else {
10398 throw new Error ( 'Remote error logging failed!' ) ;
@@ -107,57 +102,62 @@ var Logerr = function() {
107102 } ;
108103 }
109104
110- function _serializeData ( params ) {
111- return Object . keys ( params ) . map ( function ( k ) {
112- return encodeURIComponent ( k ) + "=" + encodeURIComponent ( params [ k ] ) ;
113- } ) . join ( '&' ) ;
114- }
115-
116- function _errorData ( e ) {
117- var filename = e . filename . lastIndexOf ( '/' ) ;
118- var datetime = new Date ( ) . toString ( ) ;
119-
120- /**
121- * userAgent only for POST request purposes, not required in pretty print
122- */
105+ function listener ( e ) {
106+ if ( setConfig . detailedErrors ) {
107+ detailedErrors ( e ) ;
108+ }
123109
124- return {
125- type : e . type ,
126- path : e . filename ,
127- filename : e . filename . substring ( ++ filename ) ,
128- line : e . lineno ,
129- column : e . colno ,
130- error : e . message ,
131- stackTrace : ( ( e . error ) ? e . error . stack . toString ( ) . replace ( / ( \r \n | \n | \r ) / gm, "" ) : "" ) ,
132- datetime : datetime ,
133- userAgent : navigator . userAgent || window . navigator . userAgent
134- } ;
110+ if ( setConfig . remoteLogging ) {
111+ remoteLogging ( e , setConfig . remoteSettings ) ;
112+ }
135113 }
136114
137- //Polyfill for Object.assign
138- if ( typeof Object . assign != 'function' ) {
139- Object . assign = function ( target ) {
115+ // Polyfill for Object.assign
116+ if ( typeof Object . assign !== 'function' ) {
117+ Object . assign = function assign ( target ) {
140118 if ( target === null ) {
141119 throw new TypeError ( 'Cannot convert undefined or null to object' ) ;
142120 }
143121
144- target = Object ( target ) ;
122+ var newTarget = Object ( target ) ;
145123 for ( var index = 1 ; index < arguments . length ; index ++ ) {
146124 var source = arguments [ index ] ;
147125 if ( source !== null ) {
148126 for ( var key in source ) {
149127 if ( Object . prototype . hasOwnProperty . call ( source , key ) ) {
150- target [ key ] = source [ key ] ;
128+ newTarget [ key ] = source [ key ] ;
151129 }
152130 }
153131 }
154132 }
155- return target ;
133+ return newTarget ;
156134 } ;
157135 }
158136
137+ function init ( userConfig ) {
138+ // Default configuration
139+ var config = {
140+ detailedErrors : true ,
141+ remoteLogging : false ,
142+ remoteSettings : {
143+ url : null ,
144+ additionalParams : null ,
145+ successCallback : null ,
146+ errorCallback : null
147+ }
148+ } ;
149+
150+ // Override with user config
151+ setConfig = Object . assign ( config , userConfig || { } ) ;
152+
153+ // Remove current listener
154+ window . removeEventListener ( 'error' , listener ) ;
155+
156+ // Listen to errors
157+ window . addEventListener ( 'error' , listener ) ;
158+ }
159+
159160 return {
160161 init : init
161162 } ;
162-
163- } ( ) ;
163+ } ( ) ) ;
0 commit comments