Repository URL to install this package:
|
Version:
0.24.7.dev0 ▾
|
# Copyright (c) 2015 Docker, Inc. All rights reserved.
import logging
import json
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from rest_framework import status
logger = logging.getLogger(__name__)
class DockerHubAPIExceptionMiddleware(object):
def process_exception(self, request, exception):
if request.META.get('PATH_INFO', '').startswith('/v2/'):
if isinstance(exception, PermissionDenied):
# Swagger is under /v2, but doesn't deal with permission error well
status_code = status.HTTP_401_UNAUTHORIZED
detail = str(exception)
else:
logger.error(
'Middleware caught 500 error: %s', exception,
extra=dict(context=request.path_info,
method=request.method,
path=request.META.get('RAW_URI', request.path_info)))
status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
detail = 'We have problem processing your request. '\
'This error has been logged and we will look into it.'
response_data = {'error': True, 'detail': detail}
return HttpResponse(
json.dumps(response_data),
content_type="application/json",
status=status_code
)