-
Notifications
You must be signed in to change notification settings - Fork 25
Possible to send actual objects via callback.redirect? #129
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
Comments
ASR tries to follow a fundamental principle of browser routing: that users should be able to send anyone else a link to your current URL, or hard-refresh the page, and see essentially what they saw before. Generally speaking, if you want to preserve certain aspects of an error before redirecting to an error page, you need to decide on a way to serialize them to the URL, so that the principles of routing can continue to hold. But your choice is a bit more involved, first you have to choose – do you want users to refresh the page in order to try to reload whatever page they were trying to view before, or should the users hit the back button to try to go to the non-error page they were at before? Or said another way, what would you want to happen if someone who was sent to that error page copied the URL and sent it to someone else to open? |
@sw-clough does that make sense? Here's an example of serializing that kind of thing into the url: https://github.com/TehShrike/memorize-text-app/blob/master/client/index.js#L34 / https://github.com/TehShrike/memorize-text-app/blob/master/client/view/not-found/not-found.js#L6 |
Sorry, hadn't had time to review your response until now. Regarding the choice you presented, I hadn't thought about it. If you have an error page with parameters in the query string, then people could effectively send around joke error pages with custom URL strings. That doesn't seem ideal. So maybe leaving the URL the same, but showing an error page as the content, is the way to go. I get the fact that you should be able to copy-paste the URL and see the same page, and passing around objects shortcuts that. However, it wouldn't be hard to check for existence of the object in the activate function, and obtain if required. Of course, I could attach some sort of cache object to the data field as I create the state, and maybe that is answer, but I could see this being a function of this package too. I've come across another scenario that could benefit from passing an object, in regards to child states. Say I have the states |
So after thinking about it more, I like the idea of showing the error at the problem URL, without a redirect. So now I have implemented a helper function to render the Error template instead of the "good" template, like the following. It's not the ideal solution, but if you are reluctant to introduce a method to achieve this in a different way, I guess it will have to do.
|
That does seem like the ideal solution for now. I would be open to a PR that added a The behavior I'm imagining would, if a
The template would have to be rendered on the root element that was passed in when the router was created. |
That sounds like opt-in behaviour, so I guess it wouldn't be too bad, but it sounds a bit more application specific than I'd like in an abstract router, especially since (it looks like) you can just watch for those error events and do that yourself. What I do in a several apps is watch for error events, and then based on the error I may do some business logic, go to a different route, or pass the error to some other component: asr.on('stateChangeError', error => {
if (someBusinessLogic(error)) {
doSomething()
} else if (someOtherLogic(error) {
asr.go(someRoute, someOptions)
} else {
errorComponent.open(error)
}
}) It doesn't seem very likely that you'd want all errors handled exactly by rendering an error template--you'd want to watch for e.g. API auth errors (user logged out) or other things. Maybe I'm misunderstanding? As I understand it, I don't think I'd ever use that convenience method in any app that I'd build, so in that sense it feels like clutter. |
One limitation I do note, is that Maybe changing from |
@sw-clough is there anything that you would miss if you went with that kind of solution? asr.on('stateChangeError', error => {
new ErrorPage({
target,
data: {
error
}
})
}) This pattern should probably at least be mentioned in the readme |
Going to a different route is what I was doing, but that requires passing parameters in the URL, which I am trying to avoid. It also means the error doesn't get shown at the current URL. Regarding some other component, I'm not sure what this implies (maybe it is what TehStrike mentions in next comment?).
I don't see how this would work, due to the following issues;
|
You would use whatever root element you passed in when you instantiated the router.
The default parameters you passed to the renderer? You could use the renderer to instantiate the error page: asr.on('stateChangeError', error => {
renderer.render({
element: rootElement,
template: ErrorPage,
content: { error }
})
})
With ASR, state changes are driven by URL changes. The url changes to
I don't quite follow – the |
My app is using push state routing. In my resolve functions, I am doing some checks before returning a successful callback. For example, you visit
book/50
, and I do a check to ensure that book 50 exists, the user has permission, etc. If there is a problem, I am doing acallback.redirect()
to an error page, and sending an error object in the parameters. However, the object doesn't make it intact to the error state's resolve function, as it gets turned into a parameter on the query line. Is there a way to pass the actual error object to the error state's resolve function?The text was updated successfully, but these errors were encountered: