emberjs/ember-qunit

onerror ember.testing call is happening to component file instead of app.js file in route

sandeep49m opened this issue · 3 comments

I have implemented onerror call in my app.js file and inside it I am checking the ‘if’ condition for ‘ember.testing’ in route:

let onerror = function() { if (ember.testing) { throw error; }

but there is same condition i have in my component.js file. So, when error is triggered it call the component.js file , whereas i expected it should call my app.js file condition.

Please can anyone of you suggest the same?

example:

try { if(x.trim() == “”) throw “empty”; if(isNaN(x)) throw “not a number”; x = Number(x); if(x < 5) throw “too low”; if(x > 10) throw “too high”; } catch(err) { throw new err; }

I expect the err will call onerror on route/app.js file

Ember.onerror = function (err) { pushError(err) }

But it is going to node_module/customPlugin.js Ember.onerror = function (err) { pushError(err) }

What i am expecting it should go to app.js file is not achievable. Please can anyone tell me how to restrict the node_module/customPlugin.js to trigger?

You can only have a single replacement for the top-level Ember.onerror handler, and the syntax you're using should tell you this: you're assigning a function to that value on the Ember object. It's exactly like doing this:

const MyObject = {
  handler() {
    // by default, do nothing;
  }
};

class SomeRoute {
  constructor() {
    MyObject.handler = function() {
      console.log("This is the one from the route!")
    }
  }
}

class SomeComponent {
  constructor() {
    MyObject.handler = function() {
      console.log("This is the one from the component!")
    }
  }
}

// Replaces the default handler with the one from the component.
let someComponent = new SomeComponent();
MyObject.handler(); // logs "This is the one from the component!"

// Replaces the handler with the one from the route.
let someRoute = new SomeRoute();
MyObject.handler(); // logs "This is the one from the route!"

// Replaces the handler with the one from the new component!
let anotherComponent = new SomeComponent();
MyObject.handler(); // logs "This is the one from the component!"

Fundamentally, Ember.onerror is not designed to have multiple different handlers. Although I can think of janky ways to work around that, you should not. Instead, you should have your top-level error handler be a "last resort" which just does something which makes sense no matter where the error comes from. For handling errors more locally to your components, you can try/catch on the operations you perform and handle them locally.

Thank you Chris that helped. :)