Quantcast
Channel: SG, Author at Mydons
Viewing all articles
Browse latest Browse all 41

Error handler with requirejs

$
0
0

Requirejs hello world example

Error handler with requireJs is very easy. As we all know the importance of error handling in application this topic helps you to understand the error handler concept in requireJs.

Error handler Method definition

By default in requireJs there are two methods which handles error.
  1. requireType
  2. requireModules
requireType:  This is a string value with a general classification, like "timeout", "nodefine", "scripterror". timeout generally mean the delayed response time for the script from the server. scripterror generally mean 404 errors. requireModules : This is used under the requireType to identify on which module error has occurred.

Steps to configure error handler

  1. In your application main.js or init.js you have to write code to configure the error handler with requireJs.
    require.config({   
    	catchError: true
    });
  2. onError handler method will catch all type of error with requireJs and handles in its way.
    requirejs.onError = function (err) {
        if (err.requireType == 'timeout' || err.requireType =='scripterror') {
            alert(err.requireModules)
        } else {
            throw err;
        }
    };
If you include above code in your application main.js or init.js on each error it will render onError method so that you can intimate the user about the error occurred in your application and on demand user can be requested to reload the application to fix the error. Hope this helps!

Viewing all articles
Browse latest Browse all 41

Trending Articles