Skip to content
Snippets Groups Projects
Commit ce9f2be9 authored by Tanner Prestegard's avatar Tanner Prestegard Committed by GraceDB
Browse files

Trying to make API exceptions nicer

parent 6c745ad4
No related branches found
No related tags found
No related merge requests found
import logging
from rest_framework.views import exception_handler from rest_framework.views import exception_handler
import logging # Set up logger
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def gracedb_exception_handler(exc, context): def gracedb_exception_handler(exc, context):
# Call REST framework's default exception handler first, # Call REST framework's default exception handler first,
# to get the standard error response. # to get the standard error response.
response = exception_handler(exc, context) response = exception_handler(exc, context)
# Combine values into one list if hasattr(exc, 'detail') and hasattr(exc.detail, 'values'):
exc_out = [item for sublist in exc.detail.values() for item in sublist] # Combine values into one list
exc_out = [item for sublist in exc.detail.values() for item in sublist]
# For only one exception, just print it rather than the list # For only one exception, just print it rather than the list
if len(exc_out) == 1: if len(exc_out) == 1:
exc_out = exc_out[0] exc_out = exc_out[0]
# Update response data # Update response data
response.data = exc_out response.data = exc_out
return response return response
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment