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

User-defined error handler should automatically resolve unresolved … #183

Merged
merged 8 commits into from
Feb 17, 2017

Conversation

rickwargo
Copy link
Contributor

…responses, update README to reflect behavior.

Add tests for error handler resolution and error messages.

…sponses, update README to reflect behavior.

Add tests for error handler resolution and error messages.
@@ -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.
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

Copy link
Collaborator

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)
Copy link
Collaborator

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) {
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@rickwargo rickwargo force-pushed the test-to-handle-user-error-handler branch 2 times, most recently from 9a06347 to c85c071 Compare February 16, 2017 17:49
@rickwargo rickwargo force-pushed the test-to-handle-user-error-handler branch from c85c071 to 3c1b17d Compare February 16, 2017 17:50
});

it("fails with not a valid request", function() {
testApp.error = undefined; // may be defined in other tests, must unset
Copy link
Collaborator

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.

Copy link
Contributor Author

@rickwargo rickwargo Feb 17, 2017

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) {
Copy link
Collaborator

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.

Copy link
Contributor Author

@rickwargo rickwargo Feb 17, 2017

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.

Copy link
Contributor Author

@rickwargo rickwargo left a 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)
Copy link
Collaborator

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).
Copy link
Collaborator

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"

@dblock dblock merged commit 1b2c3cf into alexa-js:master Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants