Exemplo n.º 1
0
def exception_handler(exc, context):
    if isinstance(exc, grpc.RpcError):
        ex_type, ex, tb = sys.exc_info()
        traceback.print_tb(tb)
        return default_exception_handler(GRPCError(
            detail=f"Error communicating with regitry: {exc.details()}"
        ), context)
    else:
        return default_exception_handler(exc, context)
Exemplo n.º 2
0
def giro_exception_handler(exc, context):
    """
    Returns the response that should be used for any given exception.
    """
    exception_response = default_exception_handler(exc, context)

    if not hasattr(exception_response, 'data'):
        return exception_response

    # Hide details from production users.
    if 'detail' in exception_response.data and not settings.DEBUG:
        del exception_response.data['detail']

    # Default to unknown error.
    error_code = ErrorCodes.UNKNOWN_ERROR

    if hasattr(exc, 'err_code'):
        error_code = exc.err_code
    elif exc.__class__ in ERR_CODE_MAP:
        error_code = ERR_CODE_MAP[exc.__class__]

    exception_response.data['code'] = error_code

    if hasattr(exc, 'status_code') and exc.status_code == 500:
        logging.error(
            'Internal Server Error was raised, logging a traceback...')
        logging.error(traceback.format_exc(exc))

    return exception_response
def exception_handler(exc, context):
    if isinstance(exc, APIException):
        details = exc.get_full_details()
        details['result'] = details['code']
        del details['code']
        return Response(details, status=exc.status_code)

    return default_exception_handler(exc, context)
def exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    if isinstance(exc, BusinessException):
        response = business_exception_handler(exc, context)
    else:
        response = default_exception_handler(exc, context)

    return response
Exemplo n.º 5
0
def exception_handler(exc, context):
    response = default_exception_handler(exc, context)
    if isinstance(exc, ValidationError) and isinstance(exc.detail, list):
        content = {
            'validation_errors': exc.detail,
            'HTTP-STATUS': exc.status_code
        }
        response.content = json.dumps(content)
        response.status_code = 200
    return response
Exemplo n.º 6
0
def exception_handler(exc, context):
    # Get the standard error response
    response = default_exception_handler(exc, context)

    if response is None:
        if isinstance(exc, ProtectedError):
            response = handle_protected_error(exc, context)
        elif isinstance(exc, db.IntegrityError):
            response = Response({"detail": str(exc)}, status=status.HTTP_409_CONFLICT)
        else:
            logger.exception("Caught unhandled exception", exc_info=exc)

    return response
Exemplo n.º 7
0
def exception_handler(exc, context):
    """
    Django REST handles 4xx exceptions itself, so they don't get logged to the
    'django.request' logger by default. This exception handler logs them as if
    Django was handling them then calls the default Django REST handler. This
    makes the project logging behavior more consistent (both 4xx's and 5xx's
    are sent to the 'django.request' logger)
    """
    res = default_exception_handler(exc, context)
    if res is not None and is_client_error(res.status_code):
        request = context['request']
        logger.warn(
            '%s (params: %s) (data: %s) (response: %s)', request.path,
            request.query_params, request.data, res.data,
            extra={
                'status_code': res.status_code, 'request': request
            }
        )
    return res
Exemplo n.º 8
0
def exception_handler(exc, context):
    """Default exception handler

    Extends the functionality of REST Framework exception handler
    (:py:func:`rest_framework.views.exception_handler`) by adding statusCode
    field, name of the error and cleaning up ValidationError response data.
    """
    response = default_exception_handler(exc, context)

    if response is not None:
        name = exc.__class__.__name__
        if isinstance(exc, ValidationError):
            response.data = {'fields': response.data}
            # ValidationError is renamed to InvalidRequestData, although these
            # two errors are not the same
            name = 'InvalidRequestData'
        response.data['name'] = name
        response.data['statusCode'] = response.status_code

    return response
Exemplo n.º 9
0
def exception_handler(exc, context):
    """
    Django REST handles 4xx exceptions itself, so they don't get logged to the
    'django.request' logger by default. This exception handler logs them as if
    Django was handling them then calls the default Django REST handler. This
    makes the project logging behavior more consistent (both 4xx's and 5xx's
    are sent to the 'django.request' logger)
    """
    res = default_exception_handler(exc, context)
    if res is not None and is_client_error(res.status_code):
        request = context['request']
        logger.warn('%s (params: %s) (data: %s) (response: %s)',
                    request.path,
                    request.query_params,
                    request.data,
                    res.data,
                    extra={
                        'status_code': res.status_code,
                        'request': request
                    })
    return res
Exemplo n.º 10
0
def custom_exception_handler(exc, context):
    exc_type, exc_value, exc_traceback = sys.exc_info()
    callstack = '\n'.join(traceback.format_tb(exc_traceback))
    errorlist = []
    errorlist.append('\n\nRequest URL: %s' % str(context['request']._request))
    errorlist.append('Error type: %s' % exc_type)
    errorlist.append('Error value: %s' % exc_value)
    errorlist.append('Payload: %s' % str(context['request'].query_params))
    errorlist.append('Message: %s' % exc.message)
    errorlist.append('----Call stack----\n %s' % callstack)
    errorlist.append('------Locals------\n %s' %
                     str(context['request']._request.__dict__))
    errormsg = '\n'.join(errorlist)

    logger.error(errormsg)

    original_response = default_exception_handler(exc, context)

    if original_response is not None:
        return original_response
    return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    data=exc.message,
                    content_type='application/json')
Exemplo n.º 11
0
def exception_handler(e, context):
    response = default_exception_handler(e, context)
    if response is not None:
        response.data['status_code'] = response.status_code
    return response