-
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
User-defined error handler should automatically resolve unresolved … #183
User-defined error handler should automatically resolve unresolved … #183
Conversation
…sponses, update README to reflect behavior. Add tests for error handler resolution and error messages.
5d973c9
to
ac861c7
Compare
@@ -500,7 +503,8 @@ response.card({ | |||
## Error Handling | |||
|
|||
Handler functions should not throw exceptions. Ideally, you should catch errors in your handlers using try/catch and respond with an appropriate output to the user. If exceptions do leak out of handlers, they will be thrown by default. Any exceptions can be handled by a generic error handler which you can define for your app. Error handlers cannot be asynchronous. | |||
|
|||
The generic error handler, below, will automatically resolve the promise (request) with `response.send()` if it does not occur in the error handler. | |||
`response.fail(message, exception)` should only be called from a controlled calling environment, e.g. your own server, not AWS Lambda. |
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.
Why would this be the case? You should be able to use response.fail
to force a failure response in any environment. Particularly this will trigger .error
and .post
.
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 it needs a better description. Calling response.fail()
in a Lambda environment produces an undesired message from Alexa.
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 agree, maybe something explaining what happens on Lambda. Can be edited later.
index.js
Outdated
@@ -357,6 +357,8 @@ alexa.app = function(name) { | |||
var handleError = function(e) { | |||
if (typeof self.error == "function") { | |||
self.error(e, request, response); | |||
if (!response.resolved) |
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.
Include braces
index.js
Outdated
} else { | ||
response.fail("Unhandled exception.", e); | ||
} | ||
if (e.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.
Indent this block by two lines instead of four.
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'm fighting with my editor and my default of 4 - missed this one.
9a06347
to
c85c071
Compare
c85c071
to
3c1b17d
Compare
}); | ||
|
||
it("fails with not a valid request", function() { | ||
testApp.error = undefined; // may be defined in other tests, must unset |
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.
The test above sets it, wrap that into a context and have a beforeEach
and afterEach
, where you set the error before and unset it after. This way you don't need to rely on the order of tests.
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.
Now creating a new testApp instance and setting the error handler before each test.
@@ -21,25 +21,74 @@ describe("Alexa", function() { | |||
|
|||
var mockRequest = mockHelper.load("unknown_type_request.json"); | |||
|
|||
it("invokes a globally defined error function", function() { | |||
it("invokes a globally defined error function that throws an error", function() { | |||
testApp.error = function(e, request, response) { |
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.
Similar to below, wrap error reset or create a new testApp instance every time.
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.
Now creating a new testApp instance and setting the error handler before each test.
…ngle testApp.error
…ngle testApp.error
…m/rickwargo/alexa-app into test-to-handle-user-error-handler
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.
Now creating a new testApp before each request and adding the appropriate error handler.
@@ -5,6 +5,11 @@ | |||
* [#174](https://github.com/alexa-js/alexa-app/pull/174): Always enfore strict header checking - [@tejashah88](https://github.com/tejashah88). | |||
* Your contribution here. | |||
|
|||
### 3.1.3 (February 16, 2017) |
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.
This should really happen when you do an actual release, but I'll let it slide ;)
@@ -5,6 +5,11 @@ | |||
* [#174](https://github.com/alexa-js/alexa-app/pull/174): Always enfore strict header checking - [@tejashah88](https://github.com/tejashah88). | |||
* Your contribution here. | |||
|
|||
### 3.1.3 (February 16, 2017) | |||
|
|||
* [#182](https://github.com/alexa-js/alexa-app/issues/182): Fix: user-defined error handler should automatically resolve unresolved responses, update README to reflect behavior - [@rickwargo](https://github.com/rickwargo). |
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.
No need to mention things like "update README to reflect behavior"
…responses, update README to reflect behavior.
Add tests for error handler resolution and error messages.