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

Redirecting on rejected resolve #3331

Closed
cadilhac opened this issue Feb 9, 2017 · 8 comments
Closed

Redirecting on rejected resolve #3331

cadilhac opened this issue Feb 9, 2017 · 8 comments

Comments

@cadilhac
Copy link

cadilhac commented Feb 9, 2017

Hi,

Using ui-router 1.0rc1, I can't find a way, in my state declaration (component-routed), to redirect to another state when a resolve is rejected (my server sends back a 400 status code when a resource can't be found). Is a hook necessary? I hope there is a solution that can be local to each state declaration instead of in a global hook...
I don't think calling $state.go() in a rejection handler inside my resolve function is the thing to do. It seems to work but I noticed that other resolves are called (and one of them depends on the rejected one).

Thanks

@kalbert312
Copy link

You could set this up in your app's run block:

$state.defaultErrorHandler(function(error) {
    // ... 
   $state.go('someErrorState', { errorMessage: error }); // careful not to create an infinite loop here
});

The reject result of the resolve should be passed to the error handler.

@cadilhac
Copy link
Author

I want to avoid having a global place with if statements. I would like to stay in my state definition.

@christopherthielen
Copy link
Contributor

You raise a really good point.

Resolves are not considered to be "transition hooks" so they cannot explicitly redirect the transition. The reason your other resolves are still processing is likely because you need to return the rejection from the failed resolve. Something like this:

.state('foo', {
  resolve: {
    fails: ($q, $http, $state) => 
      $http.get('/404').catch(err => $state.go('recover') && $q.reject(err))
  }
});

That should cause the resolve to be rejected (erroring the first transition). Then the second transition (state.go) can proceed

@cadilhac
Copy link
Author

Thanks, I can try that. But do you feel it as a woraround or something that integrates well with the framework?

@christopherthielen
Copy link
Contributor

I feel it's a workaround.

I think you've raised a good point about a possible enhancement to the framework.

@cadilhac
Copy link
Author

cadilhac commented Feb 11, 2017

The workaround worked well. But note that I had to "return" $q.reject(err) to ensure that the rejection is taken into account so that the other resolves are not attempted.

@cadilhac
Copy link
Author

cadilhac commented Nov 8, 2018

Revisiting my code and I had a link to this thread in my comments.
Is there anything new related to this issue?
Thanks.

@stale
Copy link

stale bot commented Jan 24, 2020

This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues
may be reopened.

Thank you for your contributions.

@stale stale bot added the stale label Jan 24, 2020
@stale stale bot closed this as completed Feb 7, 2020
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

3 participants