예제 #1
0
    def response_from_exception(self, exc):

        if isinstance(exc, HttpError):
            status_code = exc.status_code
            error_code = getattr(exc, 'error_code', 'UNEXPECTED_ERROR')
        elif isinstance(exc, self.expected_exceptions):
            status_code = getattr(exc, 'status_code', 400)
            error_code = getattr(exc, 'error_code', 'BAD_REQUEST')
        else:
            status_code, error_code = 500, 'UNEXPECTED_ERROR'

        reason = safe_for_serialization(exc)

        response = Response(
            json.dumps({
                'error_code': error_code,
                'reason': reason
            }),
            status=status_code,
            mimetype='application/json'
        )
        if self.cors_enabled:
            response = self.add_cors_headers(response)

        return response
예제 #2
0
    def worker_result(self, worker_ctx, result=None, exc_info=None):
        self._run_callback('on_before_worker_result',
                           worker_ctx=worker_ctx,
                           result=result,
                           exc_info=exc_info)

        if self.result_backend:
            call_id = worker_ctx.call_id

            if exc_info is None:
                status = 'SUCCESS'
            else:
                status = 'FAILED'

            now = datetime.datetime.now()

            start = self.logs.pop(worker_ctx)

            self.db.logging.update_one({'call_id': call_id}, {
                '$set': {
                    'status':
                    status,
                    'end':
                    now,
                    'elapsed': (now - start).seconds,
                    'exception':
                    safe_for_serialization(exc_info)
                    if exc_info is not None else None
                }
            })

        self._run_callback('on_after_worker_result',
                           worker_ctx=worker_ctx,
                           result=result,
                           exc_info=exc_info)
예제 #3
0
    def process_exception(self, request, exc, *args, **kwargs):
        if self.ignore_errors:
            return

        if isinstance(exc, Forbidden):
            return create_response({}, safe_for_serialization(exc),
                                   exc.status_code)
예제 #4
0
    def response_from_exception(self, exc):
        status_code, error_code = 500, "UNEXPECTED_ERROR"

        if isinstance(exc, self.expected_exceptions) or isinstance(
                exc, self.standard_mapped_errors_tuple):
            if type(exc) in self.standard_mapped_errors:
                status_code, error_code = self.standard_mapped_errors[type(
                    exc)]
            elif type(exc) in self.mapped_errors:
                status_code, error_code = self.mapped_errors[type(exc)]
            else:
                status_code = 400
                error_code = "BAD_REQUEST"

        response = Response(
            json.dumps({
                "error": error_code,
                "message": safe_for_serialization(exc)
            }),
            status=status_code,
            mimetype="application/json",
        )
        response = self._add_cors(response)

        return response
예제 #5
0
    def response_from_exception(self, exc):
        text = json.dumps({
            'success': False,
            'error': safe_for_serialization(exc),
        })

        response = Response(text, status=500, mimetype='application/json')
        return response
예제 #6
0
def test_safe_for_serialization_bad_str():
    class BadStr(object):
        def __str__(self):
            raise Exception('boom')

    obj = BadStr()
    safe = safe_for_serialization(obj)
    assert isinstance(safe, six.string_types)
    assert 'failed' in safe
예제 #7
0
def test_safe_for_serialization_bad_str():
    class BadStr(object):
        def __str__(self):
            raise Exception('boom')

    obj = BadStr()
    safe = safe_for_serialization(obj)
    assert isinstance(safe, six.string_types)
    assert 'failed' in safe
예제 #8
0
 def response_from_exception(self, exc):
     if isinstance(exc, HttpError):
         response = Response(
             json.dumps({"error": exc.error_code, "message": safe_for_serialization(exc)}),
             status=exc.status_code,
             mimetype="application/json",
         )
         return response
     return HttpRequestHandler.response_from_exception(self, exc)
예제 #9
0
 def response_from_exception(self, exc):
     if isinstance(exc, HttpError):
         response = Response(json.dumps({
             'error':
             exc.error_code,
             'message':
             safe_for_serialization(exc),
         }),
                             status=exc.status_code,
                             mimetype='application/json')
         return response
     return HttpRequestHandler.response_from_exception(self, exc)
예제 #10
0
 def process_exception(self, request, exc, *args, **kwargs):
     if isinstance(exc, HttpError):
         if isinstance(exc.message, (list, tuple, dict, OrderedDict)):
             _msg = exc.message
         else:
             _msg = safe_for_serialization(exc)
         response = Response(json.dumps({
             'error': exc.error_code,
             'message': _msg,
         }),
                             status=exc.status_code,
                             mimetype='application/json')
         return response
     return
예제 #11
0
    def response_from_exception(self, exc):
        status_code, error_code = 500, 'UNEXPECTED_ERROR'

        if isinstance(exc, self.expected_exceptions):
            if type(exc) in self.mapped_errors:
                status_code, error_code = self.mapped_errors[type(exc)]
            else:
                status_code = 400
                error_code = 'BAD_REQUEST'

        return Response(json.dumps({
            'error': error_code,
            'message': safe_for_serialization(exc),
        }),
                        status=status_code,
                        mimetype='application/json')
예제 #12
0
    def response_from_exception(self, exception):  # exception
        """ This method is used for get response from exception"""
        status_code, error_code = 500, 'UNEXPECTED_ERROR'  # if unexpected error

        if isinstance(
                exception,
                self.expected_exceptions):  # is instance of exception type
            if type(exception) in self.mapped_errors:  # mapped errors
                status_code, error_code = self.mapped_errors[type(
                    exception)]  # mapped status code 200 expected
            else:
                status_code = 400  # else status code 400
                error_code = 'BAD_REQUEST'  # bad request
        return json_response(data=dict(),
                             message=safe_for_serialization(exception),
                             status=status_code)
예제 #13
0
    def response_from_exception(self, exc):
        status_code, error_code = 500, 'UNEXPECTED_ERROR'

        if isinstance(exc, self.expected_exceptions):
            if type(exc) in self.mapped_errors:
                status_code, error_code = self.mapped_errors[type(exc)]
            else:
                status_code = 400
                error_code = 'BAD_REQUEST'

        return Response(
            json.dumps({
                'error': error_code,
                'message': safe_for_serialization(exc),
            }),
            status=status_code,
            mimetype='application/json'
        )
예제 #14
0
def test_safe_for_serialization(value, safe_value):
    assert safe_for_serialization(value) == safe_value
예제 #15
0
def test_safe_for_serialization(value, safe_value):
    assert safe_for_serialization(value) == safe_value