-
Notifications
You must be signed in to change notification settings - Fork 212
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
Fix "Cannot read property 'new' of undefined" init error #91
Conversation
This needs a test and a CHANGELOG entry, please? |
@@ -145,6 +145,14 @@ alexa.request = function(json) { | |||
return null; | |||
} | |||
}; | |||
// session is not included for AudioPlayer or PlaybackController requests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we merge this - since the Alexa requests don't include session objects for these audio ones, maybe we shouldn't be making a session object to work around that, but being more defensive? Otherwise we're creating something not supported by the platform, no? |
Actually I agree with you, we shouldn't provide session interface in case of its absence. We can set |
Maybe we should throw an error when you access |
Deny using session when the Alexa requests don't include session objects
} else { | ||
this.response.sessionAttributes[key] = val; | ||
throw "NO_SESSION"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a pattern we use elsewhere? Maybe throw a more helpful message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh scratch that, I see it below.
@@ -180,6 +204,9 @@ alexa.app = function(name, endpoint) { | |||
"NO_LAUNCH_FUNCTION": "Try telling the application what to do instead of opening it", | |||
// When a request type was not recognized | |||
"INVALID_REQUEST_TYPE": "Error: not a valid request", | |||
// When a request and response don't contain session object | |||
// https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference#request-body-parameters | |||
"NO_SESSION": "There is not session to store or get values", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe "this request doesn't support session attributes" or something like that?
Sorry I didn't write tests, dunno how to do it. The idea of my changes: |
@@ -3,15 +3,20 @@ var AlexaUtterances = require("alexa-utterances"); | |||
var SSML = require("./to-ssml"); | |||
var alexa = {}; | |||
|
|||
alexa.response = function() { | |||
alexa.response = function(hasSession) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a cleaner way to do this. The response could be aware of the session instead of whether the request "has a session". So the session would be constructed in the request object and the response would get the actual session here. This way sessionAttributes
moves the new session object as attributes
and can abstracts away a bunch of functions such as the implementation inside this.session
here making things better organized and a lot cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see your idea:
request.session = {
this.get = function(key) {...};
this.set = function(key, value) {...};
this.clear = function() {...};
this.details = {...};
this.attributes = {...};
this.isNew = bool;
}
Just don't understand why we need to store the same data in this.sessionDetails
and in properties of this
.
BTW, these changes will break compatibility.
And we need to edit response.response
from the session
object. Don't sure how we can do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't actually written the code, give it a shot. Obviously backwards compat breaking is bad, maybe we can minimize that once we see what it looks like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need to move session
outside of request
and response
since it's an independent object not related to request or response instantly.
I'd even say it's between them.
So we can provide it as an alone object:
app.pre = function(request, response, type, session) {
...
};
app.intent("sampleIntent", {
"slots": {...},
"utterances": [...]
},
function(request, response, session) {
...
}
);
And call session
methods from existed methods of request
and response
for backwards compatibility (and mark them as deprecated).
See my comment above wrt |
And thanks for the hard work! Hang in there we'll get this to a mergeable state soon. |
Mark all moved to session object properties and methods as deprecated Update test request json Update readme
So I separate These methods work for 100% backwards compatibility, but marked as deprecated:
The same for these properties:
I passed Now it needs to do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is very close. I would move this last session argument into being available via request.session() or requet.getSession() whatever is more appropriate and then the interface doesn't need to change much. What do you think?
## session | ||
```javascript | ||
// check if you can use session (read or write) | ||
Boolean session.isAvailable() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a little strange to ask the session whether it's available. Why wouldn't this be on request.hasSession()
? And then I think we don't need to change the first function
because request.session
would be a thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to make a gettter (request.getSession()
) in request
object for getting session
object so we don't need to pass session
as a last argument to handlers?
And check the session via request.hasSession()
.
So using the session
would be:
if (request.hasSession()) {
var session = request.getSession();
session.set("fruit", "apple");
console.log(session.get("fruit"));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! This is exactly it.
Remove session from last arguments of handler functions
The code looks perfect. Just need two tests: for the loop that involves no session and another that ends up in that NO_SESSION exception being thrown. |
Lets also add an UPGRADING.md document that describes the methods that have been deprecated. For a good example see https://github.com/ruby-grape/grape/blob/master/UPGRADING.md |
Fix an error - NO_SESSION exception on start of app Add UPGRADING.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect, merging. Nice work!
Merged squashed. You'll have to fix/rewind/rebase your master branch (you should start working on branches). See http://code.dblock.org/2015/08/31/getting-out-of-your-first-git-mess.html that might be helpful. |
Woohoo! |
I cleaned up my master branch, now it has only one commit for this PR, but the commit is after merge of this PR :( |
|
The error is caused on getting
AudioPlayer
events. I got for these events:AudioPlayer.PlaybackStarted
,AudioPlayer.PlaybackNearlyFinished
andAudioPlayer.PlaybackFinished
.As Amazon wrote:
Additional info regarding to this error you can find in PR #88 and Issue #78