router icon indicating copy to clipboard operation
router copied to clipboard

Configuring a route with "redirect" property causes BlueBird warnings

Open ry8806 opened this issue 7 years ago • 23 comments

I'm submitting a bug report

  • Library Version: 1.0.1

Please tell us about your environment:

  • Operating System: Windows 10

  • Browser: all

  • Language: all

Current behavior: specifying "redirect" property in a route configuration, causes BlueBird warnings about "Warning: a promise was rejected with a non-error: [object Object]"

This comes from the router method:

  function _buildNavigationPlan(instruction, forceLifecycleMinimum) {
    var prev = instruction.previousInstruction;
    var config = instruction.config;
    var plan = {};

    if ('redirect' in config) {
      var redirectLocation = _resolveUrl(config.redirect, getInstructionBaseUrl(instruction));
      if (instruction.queryString) {
        redirectLocation += '?' + instruction.queryString;
      }

      return Promise.reject(new Redirect(redirectLocation));
    }

I tired creating a gist given the link in the Issue Template, however i can't replicate it in a Gist - i'm assuming the bluebird promises library isn't included in gists?

Expected/desired behavior: Not to create a warning in the console everytime that specific route is redirected

  • What is the motivation / use case for changing the behavior? Less warnings in the console

I understand that this is a BlueBird "quirk", however I'm posing the question as to whether Aurelia adjusts its use of BlueBird to stop these warnings? or if we need to go to BlueBird and propose a "don't warn me when a promise is rejected with a non-error" flag?

ry8806 avatar Mar 21 '17 11:03 ry8806

The same warning is thrown for:

config.mapUnknownRoutes({ redirect:'home'});

is there any way to suppress it ?

renathoaz avatar Apr 17 '17 13:04 renathoaz

If you don't want these to show then you need to disable them in your bluebird config:

If you're using Aurelia CLI you can change it within the main.js file.

Promise.config({
  warnings: false
});

jagonalez avatar Apr 19 '17 05:04 jagonalez

@jagonalez I'm aware of this, thanks

This "silence all warnings" flag isn't the solution, by disabling ALL warnings (for the issue mentioned above) I might be losing out on other warnings that it may be trying to warn me about

ry8806 avatar Apr 19 '17 07:04 ry8806

@ry8806 I agree - it's not ideal. In our app we're using environment variables to turn on warnings only for debugging.

Looking at the w3 specification Promise.reject should return an Error object:

The w3 specification for Promises recommends that reject return an instance of Error. https://www.w3.org/2001/tag/doc/promises-guide#reasons-should-be-errors

Promise rejection reasons should always be instances of the ECMAScript Error type, just like synchronously-thrown exceptions should always be instances of Error as well.

Bluebird's docs explain why: https://github.com/petkaantonov/bluebird/blob/master/docs/docs/warning-explanations.md

Due to a historic mistake in JavaScript, the throw statement is allowed to be used with any value, not just errors, and Promises/A+ choosing to inherit this mistake, it is possible to reject a promise with a value that is not an error.

The only way to stop the console warnings is to change the code to return an Error or not use Promise.reject for redirection.

@EisenbergEffect - What are your thoughts?

jagonalez avatar Apr 19 '17 19:04 jagonalez

Updates on this? This has been bugging me for a while now.

Is there really nothing else we can do except disable all warnings?

indfnzo avatar Jul 05 '17 04:07 indfnzo

I get why the warning is thrown, and valid, but I don't see why we'd reject a promise to do a redirect.

If we HAVE to use promise rejection, couldnt this issue be solved by inheriting redirects from Error? Or adding the needed property stubs (.stack and .message) to the redirect class?

cornillemichiel avatar Jul 18 '17 12:07 cornillemichiel

I get this is a low profile issue but could we get an official response on whether we should expect this to be handled or if the only workaround is to disable warning?

Kukks avatar Oct 16 '17 20:10 Kukks

I'm now hitting this problem, and would like to know if there's any status change on how to fix it?

eric-914 avatar Feb 05 '18 18:02 eric-914

@Kukks for the record, the problem is just a warning being logged to the console?

I think a good, viable long term fix would be to allow resolving a redirect as well. I agree that while the reject notation half makes sense, it half doesn't. PRs welcome if someone wants to take a stab at that.

davismj avatar Feb 06 '18 05:02 davismj

@davismj I've been digging into the router recently and noticed a few places where promises aren't being returned from one internal method to the next.

One example in the app-router, _queueInstruction calls _dequeueInstruction but doesn't return it.

Another one is registerViewPort which calls _dequeueInstruction at the bottom.

So what happens to the promise that's returned by _dequeueInstruction in both cases?

I can't help but wonder if this is intended for some kind of asynchrony, or if this was overlooked at some point? I'm fairly sure that this sort of thing is what causes the promise warnings.

fkleuver avatar Feb 16 '18 09:02 fkleuver

@fkleuver as I didn't write the code and there aren't a lot of comments, I can't say for sure whether or not the highly async architecture was the right way to go. I've noticed the same things and they seem like a liability to me as well, but they're (a) deep and (b) pervasive, so I'm not in any place to start messing with them.

That said, as far as I understand, this particular issue is caused when aurelia internals reject() Redirects to cancel navigation instead of resolving. This is wrong in the strictest interpretation of promises, in which reject() is the async version of a throw, and bluebird tells anyone who will listen.

davismj avatar Feb 19 '18 05:02 davismj

Can we change this? Or we can't? Should we?

Or just keep in mind for vNext?

Alexander-Taran avatar Mar 24 '18 22:03 Alexander-Taran

As davismj pointed out, it seems my earlier comment isn't applicable to this specific issue. I'd go with his suggestion to allow redirects to be resolved as being the point of action here. I seem to recall someone was working on that in another PR but I can't find it right now.

I might take a stab at it if no one else is working on it. Could you double check as you're the one who's on top of the issues? :)

fkleuver avatar Mar 24 '18 23:03 fkleuver

I remember too.. there were an issue where a question was asked.. do we really want to fix a warning? when nothing is really broken? Is it really that easy to distract developers?

Alexander-Taran avatar Mar 24 '18 23:03 Alexander-Taran

Technically it's an uncaught error, and that's certainly not a good thing.

fkleuver avatar Mar 25 '18 00:03 fkleuver

Technically-shmecnically It works? don't change it! (-: How's that for an argument?

Alexander-Taran avatar Mar 25 '18 00:03 Alexander-Taran

@fkleuver No one is working on it as far as I know.

davismj avatar Apr 08 '18 05:04 davismj

The fix introduced in 487c33d50fa938c9e725180877f6510ecae89db5 causes a much more serious error when redirecting in a sub-router config: TypeError: Cannot read property 'childRouter' of undefined is thrown because determineWhatToLoad(…) tries to interpret the Redirect plan as a regular plan because the rejection needs to be handled in more places than just the one checked in 487c33d50fa938c9e725180877f6510ecae89db5.

rluba avatar Jun 05 '18 18:06 rluba

I concur with @rluba , after doing a "clean" install this morning referencing [email protected] (since Feb 2018), I got [email protected], which is causing the TypeError: Cannot read property 'childRouter' of undefined error. Manually installing [email protected] fixed it though.

stefan505 avatar Jun 19 '18 08:06 stefan505

Same boat, broke our prodcution builds since we get rid of package lock json file due to other weird crazy errors from aurelia when we leave it in

On Tue, Jun 19, 2018 at 10:37 AM Stefan Buys [email protected] wrote:

I concur with @rluba https://github.com/rluba , after doing a "clean" install this morning referencing [email protected] (since Feb 2018), I got [email protected], which is causing the TypeError: Cannot read property 'childRouter' of undefined error. Manually installing [email protected] fixed it though.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aurelia/router/issues/480#issuecomment-398320707, or mute the thread https://github.com/notifications/unsubscribe-auth/ABu-_mjzjdrVBxsMedsiu28RsF3Guo-xks5t-Lg0gaJpZM4MjrVJ .

Kukks avatar Jun 19 '18 10:06 Kukks

Yep agreed, https://github.com/aurelia/router/pull/603 should be merged asap. Sorry for breaking your apps!

fkleuver avatar Jun 19 '18 11:06 fkleuver

It'll be getting released today.

davismj avatar Jun 19 '18 12:06 davismj

Shouldn't this be reopened since #597 was reverted?

ricardograca avatar Oct 08 '18 14:10 ricardograca