Skip to content

Instantly share code, notes, and snippets.

@nfgallimore
Last active May 31, 2018 20:01
Show Gist options
  • Save nfgallimore/7e5c568afb146da50a5a24b6ab89a802 to your computer and use it in GitHub Desktop.
Save nfgallimore/7e5c568afb146da50a5a24b6ab89a802 to your computer and use it in GitHub Desktop.
import json
def handle_error(error):
print(error['debug_error_message'])
## here are the three possible structures discussed in the meeting:
error_code_is_unique_to_endpoint_and_status_code = '''{
"errors": [
{
"debug_error_message": "Username must be between 6 and 255 characters long.",
"error_id": 1
},
{
"debug_error_message": "Username cannot contain special characters.",
"error_id": 2
},
{
"debug_error_message": "Password must be between 6 and 255 characters long.",
"error_id": 3
},
{
"debug_error_message": "Password must contain special characters.",
"error_id": 4
}
]
}'''
error_code_is_unique_per_field_name_type_1 = '''{
"errors": [
{
"field_name": "username",
"field_errors": [
{
"debug_error_message": "Username must be between 6 and 255 characters long.",
"error_id": 1
},
{
"debug_error_message": "Username cannot contain special characters.",
"error_id": 2
}
]
},
{
"field_name": "password",
"field_errors": [
{
"debug_error_message": "Password must be between 6 and 255 characters long.",
"error_id": 1
},
{
"debug_error_message": "Password must contain special characters.",
"error_id": 2
}
]
}
]
}'''
error_code_is_unique_per_field_name_type_2 = '''{
"errors": [
{
"debug_error_message": "Username must be between 6 and 255 characters long.",
"error_id": 1,
"field_name": "username"
},
{
"debug_error_message": "Username cannot contain special characters.",
"error_id": 2,
"field_name": "username"
},
{
"debug_error_message": "Password must be between 6 and 255 characters long.",
"error_id": 1,
"field_name": "password"
},
{
"debug_error_message": "Password must contain special characters.",
"error_id": 2,
"field_name": "password"
}
]
}'''
# however, consider the case where a combination of two fields is not valid:
combination_type_1 = '''{
"errors": [
{
"field_name": "",
"field_errors": [
{
{
"debug_error_message": "Username and password combination is not valid",
"error_id": 1
}
}
]
}
]
}'''
combination_type_2 = '''{
"errors": [
{
"field_errors": [
{
{
"debug_error_message": "Username and password combination is not valid",
"error_id": 1,
"field_name": ""
}
}
]
}
]
}'''
# the field_name must be blank at times if we choose to include field_names
# assume that the status code is a 400
status_code = 400
# here is what it is like to parse our current model that does not have any field_names:
data = json.loads(error_code_is_unique_to_endpoint_and_status_code)
if 'errors' in data:
for error in data['errors']:
if status_code == 400:
if error['error_id'] == 1:
handle_error(error)
if error['error_id'] == 2:
handle_error(error)
if error['error_id'] == 3:
handle_error(error)
if error['error_id'] == 4:
handle_error(error)
if status_code == 409:
if error['error_id'] == 1:
handle_error(error)
# and this is what it is like to parse where the field_name is included only once at an upper level:
data = json.loads(error_code_is_unique_per_field_name_type_1)
if status_code == 400:
if 'errors' in data:
for field in data['errors']:
if field['field_name'] == 'username':
for error in field['field_errors']:
if error['error_id'] == 1:
handle_error(error)
if error['error_id'] == 2:
handle_error(error)
if field['field_name'] == 'password':
for error in field['field_errors']:
if error['error_id'] == 1:
handle_error(error)
if error['error_id'] == 2:
handle_error(error)
if status_code == 409:
if 'errors' in data:
for error in data['errors']:
handle_error(error)
# and if the field_name is included at the bottom level it is almost the same to the current model:
data = json.loads(error_code_is_unique_per_field_name_type_2)
if status_code == 400:
if 'errors' in data:
for error in data['errors']:
if error['field_name'] == 'username':
if error['error_id'] == 1:
handle_error(error)
if error['error_id'] == 2:
handle_error(error)
if error['field_name'] == 'password':
if error['error_id'] == 1:
handle_error(error)
if error['error_id'] == 2:
handle_error(error)
if status_code == 409:
if 'errors' in data:
for error in data['errors']:
handle_error(error)
# It seems that if we want to include the field_names one drawback is having a null/empty value for the field_name field at times.
# it definitely makes the parsing more complicated too.
# There's an additional if statement per field at a mininum if you use type 2
# and an additional if statement and for loop per field if you parse type 1.
# I'm not seeing the big plus side of the field_name. Remember this will only apply to 400 model validation errors
# and there are alot more error responses than just model validation
# I forget why we suggested it, it seemed like a good idea to me at the time because why not,
# it's more information, but that makes it harder to parse and work with.
# @Ryan I didn't particuarly understand what you were saying the benefits are of the field_name
# I think it would be easier for Cluj to use the current model
# @Dave I think we could put some logic into the error id's rather than them just being 1,2,3,4...
# but having them unique per field_name I'm not sure
# say we have the required field errors all return 4001 errors
# what would the error id be and the field_name be for the case where there was an invalid username and password combination.
# Such as username == password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment