question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to hook into error events globally

See original GitHub issue

Right now there is no reliable way to hook into outgoing requests (for something like a loading animation, or disabling a save button) because it isn’t possible to hook into failed XHR which is required to update state when the request finishes, albeit with a failure.

I can do something like this (via angular):

.run(['DS', '$rootScope', function(DS, $rootScope){
    angular.extend(DS.defaults, {
      beforeCreate: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$creating = true;
          cb(null, instance);
        });
      },
      afterCreate: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$creating = false;
          cb(null, instance);
        });
      },
      beforeUpdate: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$updating = true;
          cb(null, instance);
        });
      },
      afterUpdate: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$updating = false;
          cb(null, instance);
        });
      },
      beforeDestroy: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$destroying = true;
          cb(null, instance);
        });
      },
      afterDestroy: function (Resource, instance, cb) {
        $rootScope.$evalAsync(function(){
          instance.$$destroying = false;
          cb(null, instance);
        });
      }
    });
  }])

But this doesn’t work if an error occurs, because the instance.$$<action> property doesn’t get set back to false when a request fails (like with a 401). Any thoughts on how we should implement this? I was thinking something along the lines of:

DS#afterCreateError DS#afterUpdateError DS#afterDestroyError

This would avoid breaking changes, that could POSSIBLY be made if we simply call DS#after<action> with errors, passing the error object in as a 4th param?

Let me know what you think @jmdobry . I am down to submit a PR for either case.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
zuzusikcommented, Sep 20, 2019

@crobinson42 first post in this issue has pretty much a detailed explanation, but to make it short, I would like to be able to do something like:

this.DS.getMapper('SomeMapper').on('createManyError', handler);

**note: ** after<Action> hooks don’t fire on errors

For now it can be worked around on Adapter level using it’s responseError like this:

new HttpAdapter({
  responseError: (err)  => {
    this.DS.getMapper(err.config.name).emit(`${err.config.op}Error`);

    return Promise.reject(err);
  }
});

This perfectly fits in what I need, but would be nice to have something like this out of the box in core functionality, probably on Mapper level.

0reactions
crobinson42commented, Sep 20, 2019

Do you have a code example or more details?

On Sep 20, 2019, at 12:32 PM, Viktor Zozuliak notifications@github.com wrote:

I am looking for something like this right now. Looks like this functionality got lost when implementing 3.0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript global event mechanism - Stack Overflow
I would like to get the name of the missing function called and based on presence of some string call my own function....
Read more >
Window: error event - Web APIs - MDN Web Docs - Mozilla
The error event is fired on a Window object when a resource failed to load or couldn't be used — for example if...
Read more >
Ability to subscribe to global events (like `onError`) in child ...
As far as I've understood, the only way to access global events (like onError ) is to configure it when creating the QueryCache...
Read more >
Review events and errors using Event Viewer | Microsoft Learn
Event ID Message Description 2 Microsoft Defender for Endpoint service shutdown. Occurs when the device is shut... 59 Starting command: %1 Starting response command ex... 60...
Read more >
Adding Global Error Handling and Logging in ASP.NET Core ...
In ASP.NET Core you can hook up a route to be your global exception handler. Simply add the ExceptionHandler middleware to your Startup.cs ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found