Skip to content

Commit 09f7452

Browse files
committed
Removed unneeded try/catch blocks
1 parent c245dc4 commit 09f7452

File tree

2 files changed

+50
-40
lines changed

2 files changed

+50
-40
lines changed

service/common/error_handlers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
######################################################################
2-
# Copyright 2016, 2022 John J. Rofrano. All Rights Reserved.
2+
# Copyright 2016, 2024 John J. Rofrano. All Rights Reserved.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -22,12 +22,19 @@
2222
from flask import jsonify
2323
from flask import current_app as app
2424
from service.common import status
25+
from service.models import DatabaseConnectionError
2526

2627
######################################################################
2728
# Error Handlers
2829
######################################################################
2930

3031

32+
@app.errorhandler(DatabaseConnectionError)
33+
def request_validation_error(error):
34+
"""Handles Value Errors from bad data"""
35+
return service_unavailable(error)
36+
37+
3138
@app.errorhandler(status.HTTP_404_NOT_FOUND)
3239
def not_found(error):
3340
"""Handles resources not found with 404_NOT_FOUND"""

service/routes.py

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,10 @@
1919
2020
This service keeps track of named counters
2121
"""
22-
23-
import os
2422
from flask import jsonify, abort, url_for
2523
from flask import current_app as app
2624
from service.common import status # HTTP Status Codes
27-
from .models import Counter, DatabaseConnectionError
28-
29-
DEBUG = os.getenv("DEBUG", "False") == "True"
30-
PORT = os.getenv("PORT", "8080")
25+
from service.models import Counter
3126

3227

3328
######################################################################
@@ -46,6 +41,10 @@ def index():
4641
status.HTTP_200_OK,
4742
)
4843

44+
############################################################
45+
# R E S T A P I M E T H O D S
46+
############################################################
47+
4948

5049
############################################################
5150
# List counters
@@ -54,11 +53,10 @@ def index():
5453
def list_counters():
5554
"""List counters"""
5655
app.logger.info("Request to list all counters...")
57-
try:
58-
counters = Counter.all()
59-
except DatabaseConnectionError as err:
60-
abort(status.HTTP_503_SERVICE_UNAVAILABLE, err)
6156

57+
counters = Counter.all()
58+
59+
app.logger.info("Returning %d counters...", len(counters))
6260
return jsonify(counters)
6361

6462

@@ -68,15 +66,12 @@ def list_counters():
6866
@app.route("/counters/<name>", methods=["GET"])
6967
def read_counters(name):
7068
"""Read a counter"""
71-
app.logger.info("Request to Read counter: %s...", name)
69+
app.logger.info("Request to Read counter: '%s'...", name)
7270

73-
try:
74-
counter = Counter.find(name)
75-
except DatabaseConnectionError as err:
76-
abort(status.HTTP_503_SERVICE_UNAVAILABLE, err)
71+
counter = Counter.find(name)
7772

7873
if not counter:
79-
abort(status.HTTP_404_NOT_FOUND, f"Counter {name} does not exist")
74+
error(status.HTTP_404_NOT_FOUND, f"Counter '{name}' does not exist")
8075

8176
app.logger.info("Returning: %d...", counter.value)
8277
return jsonify(counter.serialize())
@@ -88,17 +83,16 @@ def read_counters(name):
8883
@app.route("/counters/<name>", methods=["POST"])
8984
def create_counters(name):
9085
"""Create a counter"""
91-
app.logger.info("Request to Create counter...")
92-
try:
93-
counter = Counter.find(name)
94-
if counter is not None:
95-
return jsonify(code=status.HTTP_409_CONFLICT, error="Counter already exists"), status.HTTP_409_CONFLICT
86+
app.logger.info("Request to Create counter: '%s'...", name)
9687

97-
counter = Counter(name)
98-
except DatabaseConnectionError as err:
99-
abort(status.HTTP_503_SERVICE_UNAVAILABLE, err)
88+
counter = Counter.find(name)
89+
if counter is not None:
90+
error(status.HTTP_409_CONFLICT, f"Counter '{name}' already exists")
91+
92+
counter = Counter(name)
10093

10194
location_url = url_for("read_counters", name=name, _external=True)
95+
app.logger.info("Counter '%s' created", name)
10296
return (
10397
jsonify(counter.serialize()),
10498
status.HTTP_201_CREATED,
@@ -112,16 +106,15 @@ def create_counters(name):
112106
@app.route("/counters/<name>", methods=["PUT"])
113107
def update_counters(name):
114108
"""Update a counter"""
115-
app.logger.info("Request to Update counter...")
116-
try:
117-
counter = Counter.find(name)
118-
if counter is None:
119-
return jsonify(code=status.HTTP_404_NOT_FOUND, error=f"Counter {name} does not exist"), status.HTTP_404_NOT_FOUND
109+
app.logger.info("Request to Update counter: '%s'...", name)
110+
111+
counter = Counter.find(name)
112+
if counter is None:
113+
error(status.HTTP_404_NOT_FOUND, f"Counter '{name}' does not exist")
120114

121-
count = counter.increment()
122-
except DatabaseConnectionError as err:
123-
abort(status.HTTP_503_SERVICE_UNAVAILABLE, err)
115+
count = counter.increment()
124116

117+
app.logger.info("Counter '%s' updated to %d", name, count)
125118
return jsonify(name=name, counter=count)
126119

127120

@@ -131,12 +124,22 @@ def update_counters(name):
131124
@app.route("/counters/<name>", methods=["DELETE"])
132125
def delete_counters(name):
133126
"""Delete a counter"""
134-
app.logger.info("Request to Delete counter...")
135-
try:
136-
counter = Counter.find(name)
137-
if counter:
138-
del counter.value
139-
except DatabaseConnectionError as err:
140-
abort(status.HTTP_503_SERVICE_UNAVAILABLE, err)
127+
app.logger.info("Request to Delete counter: '%s'...", name)
128+
129+
counter = Counter.find(name)
130+
if counter:
131+
del counter.value
132+
app.logger.info("Counter '%s' deleted", name)
141133

142134
return "", status.HTTP_204_NO_CONTENT
135+
136+
137+
############################################################
138+
# U T I L I T Y F U N C T I O N S
139+
############################################################
140+
141+
142+
def error(status_code, reason):
143+
"""Logs the error and then aborts"""
144+
app.logger.error(reason)
145+
abort(status_code, reason)

0 commit comments

Comments
 (0)