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

Updated to match errors lesson #1

Open
wants to merge 3 commits into
base: solution
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions app/routes/cat_routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, abort, make_response

cat_bp = Blueprint("cat", __name__,url_prefix="/cats")

Expand Down Expand Up @@ -27,10 +27,10 @@ def handle_cat(cat_id):
try:
cat_id = int(cat_id)
except:
return "Bad data", 400
return abort(make_response({"message": f"Cat {cat_id} invalid"}, 400))

for cat in cats:
if cat.id == cat_id:
return vars(cat)

return "Not found", 404
return abort(make_response({"message": f" Cat {cat_id} Not found"}, 404))
8 changes: 4 additions & 4 deletions app/routes/dog_routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, abort, make_response

dog_bp = Blueprint("dog", __name__,url_prefix="/dogs")

Expand All @@ -8,12 +8,12 @@ def __init__(self, id, name, breed, tricks=None):
self.name = name
self.breed = breed
if not tricks:
tricks = []
self.tricks = []
audreyandoy marked this conversation as resolved.
Show resolved Hide resolved
Copy link

@kelsey-steven-ada kelsey-steven-ada Apr 21, 2022

Choose a reason for hiding this comment

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

If tricks is None, we'll assign self.tricks to an empty list here, but then immediately overwrite it with None when the line below self.tricks = tricks runs. Maybe we replace all the code for self.tricks assignment with null-coalescing?

self.tricks = tricks or []

self.tricks = tricks

def to_json(self):
if not self.tricks:
tricks = "No Tricks"
tricks = []

Choose a reason for hiding this comment

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

❤️

else:
audreyandoy marked this conversation as resolved.
Show resolved Hide resolved
tricks = self.tricks
audreyandoy marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -62,4 +62,4 @@ def handle_dog(dog_id):
# }
return dog.to_json()

return {"error": "Dog not found"}, 404
return abort(make_response({"message": f"Dog {dog_id} Not found"}, 404))