Pages

Saturday, March 31, 2012

replace all double quotes in a string javascript

I decided to post this. I always forget syntax for replacing double quotes in javascript.
There are tons of posts for replacing doubles quotes but still tough to find when i need urgently.

//code sample
// this is string with lots of double quotes 
var err = "blahahahah a string with lots of double quotes ";
alert (err);  //  this will alert string with double quotes.

err = err.replace(/\"/g, "");                      
alert (err);  //  this will alert string without  double quotes.



This is a simple regex for replacing all double quotes in javascript.  "/g" is for replacing all double quotes in a string.

Friday, March 16, 2012

error handling in javascript/jquery

Error Handler:

This is a simple plugin for handling error in javascript or in jquery.

I am not sure how much this can be used efficiently everywhere but it has all basic setters for error, methods and callbacks for handling errors. You have to wrap all of your code in try  and catch block and then need to call this plugin to write your definition for handling error.

I understand error handling means logging error, notifying error and handling error for business users.
logging: logging your error
notifying: notifying your team for error
handle: displaying a business message to users as per module and requirements OR do required as per your application. 

how to use this plugin

It has three sections. 
1. settings - Here you set application name, module name, sub module name and any of your variable as settings
2. error report:  Here you set errorName, errorNumber,  error description or any variable you want in error report
3. call backs  Here plugin gives you callback for logging error, notifying team and handling error. In callback methods you get your all assigned error variables and settings as input. Input for these callbacks are same variables that you set in settings or in error reports.

Please comment if you find something that I can improve on.

How To Use:   

    // WAY 1:  Call Plugin and write your definition in log, notify and handle callbacks
    try {
        //do your code
    }
    catch {
         var errorReport =  {
                'errorName': 'js error',
               'errorNumber': '100'
         };


         $.fn.errorHandler({
         settings : {appName: 'Reggie', moduleName: 'module name'},
         errorReport:  errorReport,      
         log :  function (error){
            alert (error.errorName);          
         },
         notify :  function (error){
            alert (error.errorName);          
         },
         handle :  function (error){
             alert (error.errorName);          
         }
 
         });      
    }

    Way 2:
    $.fn.errorHandler('createReport', {errorReport:{'errorName': 'js error1','errorNumber': '1001'}      });
    $.fn.errorHandler({
        settings : {appName: 'Reggie', moduleName: 'module name'},        
        log : function (error){
            alert (error.errorName);          
        }
    });      
    
*/
===============================CODE ==============================
ERRORHANDLER.JS
===================================================================

/*
plugin use: it can be used for custom error handling. you can write your own functions for all supplied methods
how to use:
*    Way 1:
    var errorReport =  {
        'errorName': 'js error',
        'errorNumber': '100'
    };
   
    $.fn.errorHandler({
        settings : {appName: 'Reggie', moduleName: 'module name'},
        errorReport: errorReport,       
        log : function (error){
            alert (error.errorName);           
        }
    });       
   
    Way 2:
    $.fn.errorHandler('createReport', {errorReport:{'errorName': 'js error1','errorNumber': '1001'}      });
    $.fn.errorHandler({
        settings : {appName: 'Reggie', moduleName: 'module name'},         
        log : function (error){
            alert (error.errorName);           
        }
    });       
   
*/

(function( $ ){
    var globals = {
        'settings': {
            'appName':        '',
              'moduleName':     '',
              'subModuleName': ''
        },
        'log':function ()    {},
        'notify':function (){},
        'handle':function (){},   
        'errorReport':{                      
              'errorName':     '',
              'errorNumber':    '',
              'errorDesc':    '',
              'errorType':    '',
              'errorSeverity':''                         
          }
    };
       
    //public methods of plugin
    var publicMethods = {
            init : function( method ) {
                   // no code        
            },
            destroy : function() {    //destroy init of plugin by removing its init data in body dom
                $('body').removeData("ifinitializedErrorHandler");                                                  
            },
            createReport : function(param) { // getter and setter                                            
                if ($.isPlainObject(param)){                               
                    globals.errorReport = $.extend(true, globals.errorReport, param.errorReport);                                   
                }else{                                   
                    //return globals.errorReport;
                    return $.extend(true, globals.errorReport, globals.settings)
                }               
            }
    };
   
    $.fn.errorHandler = function( option ) {                                     
        data = $('body').data("ifinitializedErrorHandler");                                   
               // If the plugin hasn't been initialized yet
             if ( ! data) {                
                 $('body').data('ifinitializedErrorHandler', "true");            
                 globals = $.extend(true, globals, option);                                                         
             }
               else{                                     
                 globals = $.extend(true, globals, option);                                            
             }       
                   
        $.each( "log notify handle".split( " " ), function( i, o ){       
            if (typeof option[o] == 'function' ){               
                //globals[o].apply(this, new Array( globals.errorReport));
                globals[o].apply(this, new Array( $.extend(true, globals.errorReport, globals.settings)    ));                       
            }            
        });
       
       
       
        if ( publicMethods[option] ) {
              return publicMethods[option].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof option === 'object' || ! option ) { 
              return publicMethods.init.apply( this, arguments );
        } else {
            alert ('method: [' + option + '] is not defined in error handler plugin');                               
        }   
         
                       
    };
})( jQuery );