7

I have converted jade to html and I show the preview in a popup in my AngularJS app using jade.js. The jade can be edited in a textarea and I have a preview button too.

The problem is when the user misses something in jade console error is shown with proper message and line number.

I want to show this error in popup and not in console.

I have tried something like below in controller but it does not seem to work:

$scope.emailShowPreview = function() {

    $scope.locals = $scope.variables_for_email_preview;

    window.onerror = function myErrorHandler(err, url, line) {
        alert("Error", err);
        return false; // so you still log errors into console
    }

    try {
        var jadeFunc = jade.compile($scope.editFileds.body);
    } catch {
        window.onerror();
    }

    // compile jade template to html
    // console error happens below
    $scope.templateFunc = jade.compile($scope.editFileds.body);
    $scope.compiledHtml = $scope.templateFunc($scope.locals);

    var modalInstance = $modal.open({
        scope: $scope,
        templateUrl: '../../views/email-preview-popup.html',
        backdrop: 'static',
    });
    $scope.close = function() {
        modalInstance.dismiss('cancel');
    };
};

I referred below page too but a bit confused :-

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror

tanmay
  • 7,761
  • 2
  • 19
  • 38
  • take a look at this SO post [window.onerror](https://stackoverflow.com/a/10556743/2417602). – vikscool Aug 23 '18 at 06:39
  • @vikscool how to call that function in my code ? or does it get called on browser console error automatically ? – Warren Buffet Aug 23 '18 at 06:43
  • add the `window.onerror` implementation in your parent js (which is called on every page and not in the angular side) or you can add the function to the HTML page it will be called automatically whenever the browser encounters an error and it will act as a global error handler. – vikscool Aug 23 '18 at 06:47

0 Answers0