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

Handling Unexpected token error #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ exports.handler = function (event, context) {
};

var req = http.request(options, function (res) {
res.on('data', function (result) {
result = JSON.parse(result.toString());
if (result.response === 'ok') {
context.succeed('all events are sent to Loggly');
} else {
console.log(result.response);
}
});
if (res.statusCode === 200) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 200 response and response.ok equivalent? If so, then we only need one of them to run context.succeed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevinegami, No, they are not same. res.statusCode only returns numeric status code so the first if check is to match the returned HTTP status code and the second if check is to match the returned parsing result in result variable. The result variable has nothing to do with the 200 response.

Copy link

@kevinegami kevinegami Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we can get a 200 status code but response.ok !== True? Seems odd for an API.

Copy link

@kevinegami kevinegami Nov 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand there is a status code and a response and they are NOT the same, but for a 200 status code shouldn't they be?

res.on('data', function (result) {
result = JSON.parse(result.toString());
if (result.response === 'ok') {
context.succeed('all events are sent to Loggly');
} else {
console.log(result.response);
}
});
} else {
console.log('Error occurred: ' + res.statusCode + ' "' + res.statusMessage + '"');
}
res.on('end', function () {
console.log('No more data in response.');
context.done();
Expand Down