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

docs: Added tutorial #1065

Merged
merged 5 commits into from
Nov 6, 2018
Merged

docs: Added tutorial #1065

merged 5 commits into from
Nov 6, 2018

Conversation

Doug-AWS
Copy link
Contributor

@Doug-AWS Doug-AWS commented Nov 1, 2018

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license.
More tutorial changes based on Rico's feedback (see PR #1047).

@Doug-AWS
Copy link
Contributor Author

Doug-AWS commented Nov 1, 2018

This PR shows 2 files changed.

try {
var method = event.httpMethod;

if (method == "GET") {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

var method = event.httpMethod;

if (method == "GET") {
if (event.path.length == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

}
} catch(error) {
var body = error.stack || JSON.stringify(error, null, 2);
var response = makeError(body);
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, where are you showing me what code to copy/paste to obtain this makeError function?

The code will not be complete without this.

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 replaced the catch return with:

return {
statusCode: 200,
headers: {},
body: body
};

Copy link
Contributor

@rix0rrr rix0rrr Nov 5, 2018

Choose a reason for hiding this comment

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

That ties into another issue that I raised: how will people know it's an error response?

Either set statusCode: 500 or make body a JSON object with { success: false, error: ... }.

If you do make body a JSON object, I suspect you also need to set a content-type header.

// in the source file widgets(.js) in the resources directory
// to handle requests through API Gateway
const handler = new lambda.Function(this, 'WidgetHandler', {
runtime: lambda.Runtime.NodeJS610,
Copy link
Contributor

Choose a reason for hiding this comment

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

You switched your code to using async/await (better!) but you also have to update the runtime to use a NodeJS version of 8 or higher, otherwise it's not going to work.

Is the code tested?

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 plan to replace the code in the file with literalinclude directives so I can test the code.

Currently the code is in separate files as suggested by the doc.

try {
var method = event.httpMethod;

if (method == "GET") {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep

else if (method == "DELETE") {
// DELETE /name
// Make sure we have a name
if (event.path.length == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

===

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it

var body = "NAME arg missing (/ supplied, not /name)";

return {
statusCode: 200,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

k

return {
statusCode: 200,
headers: {},
body: JSON.stringify(body)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need to stringify body here, as we KNOW it's a string. Just write:

return {
   statusCode: 400,
   body: 'Widget name missing'
};

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've fixed all of the returns.

body: JSON.stringify(body)
};
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

You just returned in the previous branch, so no need to put this in an else.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Bucket: bucketName, Key: name
}).promise();

var body = { success: true }
Copy link
Contributor

Choose a reason for hiding this comment

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

So in case of success, you're returning an object with { success: true } in it, but in case of failure you're returning a string?

Such an irregular API shape is a pain to work with. Better to return the following then in case of error:

{ 
  success: false, 
  error: 'This is my error message'
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I'll make the change(s).

} catch(error) {
var body = error.stack || JSON.stringify(error, null, 2);
return {
statusCode: 400,
Copy link
Contributor

@rix0rrr rix0rrr Nov 6, 2018

Choose a reason for hiding this comment

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

Technically speaking this should be a 500.

4xx = you did something wrong
5xx = I did something wrong

(from the point of view of the server).

In this case we don't know what went wrong, so we can't say for sure it's the client that messed up. In fact, because we didn't predict and handle error beforehand, it is "us" (the developers) that did something wrong.

But it's not super important either way, so no need to change it.

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for bearing with me.

@Doug-AWS Doug-AWS merged commit 8c47745 into master Nov 6, 2018
@Doug-AWS Doug-AWS deleted the dougsch/tutorial branch December 3, 2018 20:46
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.

2 participants