-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Cleanup error handling/docs in TaskList #147
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
package com.google.datastore.snippets; | ||
|
||
import com.google.gcloud.datastore.Datastore; | ||
import com.google.gcloud.datastore.DatastoreException; | ||
import com.google.gcloud.datastore.DatastoreOptions; | ||
import com.google.gcloud.datastore.DateTime; | ||
import com.google.gcloud.datastore.Entity; | ||
|
@@ -51,7 +50,8 @@ public class TaskList { | |
* Adds a task entity to the Datastore. | ||
* | ||
* @param description The task description | ||
* @return The {@link Key} of the entity. | ||
* @return The {@link Key} of the entity | ||
* @throws DatastoreException if the Datastore put fails | ||
*/ | ||
Key addTask(String description) { | ||
Key key = datastore.allocateId(keyFactory.newKey()); | ||
|
@@ -70,14 +70,18 @@ Key addTask(String description) { | |
* Marks a task entity as done. | ||
* | ||
* @param id The ID of the task entity as given by {@link Key#id()} | ||
* @throws DatastoreException if the task does not exist | ||
* @return true if the task was found, false if not | ||
* @throws DatastoreException if the transaction commit fails | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For similar reason to the above (there is also get) if you want to be picky you can say "if Datastore transaction fails" |
||
*/ | ||
void markDone(long id) { | ||
boolean markDone(long id) { | ||
Transaction transaction = datastore.newTransaction(); | ||
try { | ||
Entity task = transaction.get(keyFactory.newKey(id)); | ||
transaction.put(Entity.builder(task).set("done", true).build()); | ||
if (task != null) { | ||
transaction.put(Entity.builder(task).set("done", true).build()); | ||
} | ||
transaction.commit(); | ||
return task != null; | ||
} finally { | ||
if (transaction.active()) { | ||
transaction.rollback(); | ||
|
@@ -89,6 +93,8 @@ void markDone(long id) { | |
// [START retrieve_entities] | ||
/** | ||
* Returns a list of all task entities in ascending order of creation time. | ||
* | ||
* @throws DatastoreException if the query fails | ||
*/ | ||
Iterator<Entity> listTasks() { | ||
Query<Entity> query = | ||
|
@@ -102,6 +108,7 @@ Iterator<Entity> listTasks() { | |
* Deletes a task entity. | ||
* | ||
* @param id The ID of the task entity as given by {@link Key#id()} | ||
* @throws DatastoreException if the delete fails | ||
*/ | ||
void deleteTask(long id) { | ||
datastore.delete(keyFactory.newKey(id)); | ||
|
@@ -158,10 +165,9 @@ void handleCommandLine(String commandLine) { | |
case "done": | ||
assertArgsLength(args, 2); | ||
long id = Long.parseLong(args[1]); | ||
try { | ||
markDone(id); | ||
if (markDone(id)) { | ||
System.out.println("task marked done"); | ||
} catch (DatastoreException e) { | ||
} else { | ||
System.out.printf("did not find a Task entity with ID %d%n", id); | ||
} | ||
break; | ||
|
@@ -178,7 +184,7 @@ void handleCommandLine(String commandLine) { | |
case "delete": | ||
assertArgsLength(args, 2); | ||
deleteTask(Long.parseLong(args[1])); | ||
System.out.println("task deleted"); | ||
System.out.println("task deleted (if it existed)"); | ||
break; | ||
default: | ||
throw new IllegalArgumentException("unrecognized command: " + command); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
technically it could also throw
DatastoreException
if Id allocation failed.