Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject $error$ in 1.0.0-beta.1 #2866

Closed
spencersteers opened this issue Jul 11, 2016 · 5 comments
Closed

How to inject $error$ in 1.0.0-beta.1 #2866

spencersteers opened this issue Jul 11, 2016 · 5 comments
Assignees
Milestone

Comments

@spencersteers
Copy link

In the last alpha I was able to access the error object in an onError hook through:

var redirect = ['$error$', function($error$) {
    if ($error$ && $error$.status === 404) {
        $state.go('assets');
    }
}];

Now that injections are done through the transition parameter I tried:

var redirect = function (transition) {
    var $error$ = transition.injector().get('$error$');
    if ($error$ && $error$.status === 404) {
        $state.go('assets');
    }
};

Which always produces unknown provider errors.

Is there a new way to get the error object?

@tianweiliu
Copy link

tianweiliu commented Jul 12, 2016

#2860 completely opposite of what you were trying to do, but i guess it is the event you are looking for.

@fasfsfgs
Copy link

fasfsfgs commented Jul 22, 2016

Not sure if this question belongs here, but I'll do it anyway.

How should I implement a default error handler?
I'm new to this transition concept. I (think I) did manage to fire onError events but I can't see how do I get the error thrown.

In my case I have a rejected promise in the resolve object that returns a string but I'm unable to get this string inside my $transitions.onError. Also tried $transitions.defaultErrorHandler with no luck.

Can you guys help me please?

Thanks!

Edit: Nevermind!! I just found out that defaultErrorHandler was moved from $transitions to $state.
So I just did:

$state.defaultErrorHandler(function(err) {
  // handle err
});

Keep up the good work! Can't wait for 1.0 release. =)

@christopherthielen christopherthielen self-assigned this Jul 22, 2016
@declspec
Copy link

declspec commented Jul 26, 2016

+1
I found the error embedded in transition.promise.$$state.value but it seems like there really should be a less hacky way of accessing an error in an onError method. Maybe I'm just setting up the error handler all wrong.

app.config(['$transitionsProvider', function($transitionsProvider) {
    $transitionsProvider.onError({...}, function(transition) {
        var error = transition.promise.$$state.value; // No idea if this is always going to be available at the time of invocation
        console.error(error);
    });
}]);

EDIT: Found a slightly less ugly workaround for now:

app.config(['$transitionsProvider', function($transitionsProvider) {
    $transitionsProvider.onError({...}, function(transition) {
        transition.promise.catch(function(error) {
            console.error(error);
        });
    });
}]);

@christopherthielen
Copy link
Contributor

I think this was an oversight in beta.1, sorry. in beta.2 we will expose the error as transition.error().

@christopherthielen
Copy link
Contributor

Note: a workaround:

var redirect = function (transition) {
  transition.promise.catch(function($error$) {
    if ($error$ && $error$.status === 404) {
        $state.go('assets');
    }
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants