예제 #1
0
    def test_response_error_default(self):

        return_400 = {
            'statusCode': '400',
            'body': json.dumps({'message': str('Error')}, indent=2),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }

        self.assertEqual(return_400, response(ValueError('Error')))
        self.assertEqual(return_400, response(ValueError('Error'), {'id': 1234}))
예제 #2
0
        def decorated(event, context):
            try:
                # Create ApiRequest layer and init context
                # Get DB Session
                resource = get_resource()
                req = Request(event=event,
                              aws_request_id=context.aws_request_id,
                              resource=resource)
                req.set_request_context()

                # Assign request to context
                context.request = req

                # Update logging level
                if DEBUG_HEADER in config.global_context and config.global_context[
                        DEBUG_HEADER] == 'true':
                    logger.setLevel(logging.DEBUG)
                else:
                    logger.setLevel(logging.INFO)

                # Call method and manage session in case of error
                err, res = func(event, context, resource)
            except Exception as e:
                logger.error(e)
                err, res = e, None
                err.status_code = '500'

            return response(err, res)
예제 #3
0
    def test_response_200(self):

        return_200 = {
            'statusCode': '200',
            'body': '{\n  "id": 1234\n}',
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }

        self.assertEqual(return_200, response(None, {'id': 1234}))
예제 #4
0
    def test_response_error_custom(self):

        return_404 = {
            'statusCode': '404',
            'body': json.dumps({'message': str('Not found')}, indent=2),
            'headers': {
                'Content-Type': 'application/json',
                'Access-Control-Allow-Origin': '*'
            }
        }

        error404 = ValueError('Not found')
        error404.status_code = '404'
        self.assertEqual(return_404, response(error404))
        def decorated(event, context):
            try:
                resource = get_resource()
                req = Request(event=event, resource=resource)
                # Assign request to context
                context.request = req
                # Call method and manage session in case of error
                err, res = func(event, context, resource)
            except Exception as e:
                logger.error(e)
                err, res = e, None
                err.status_code = '500'

            return response(err, res)
예제 #6
0
        def decorated(event, context):
            # Create ApiRequest layer and init context
            req = ApiRequest(event=event, aws_request_id=context.aws_request_id)
            req.set_request_context()

            # Store context globally
            config.global_context = req.request_context

            # Update logging level
            if DEBUG_HEADER in config.global_context and config.global_context[DEBUG_HEADER] == 'true':
                main.logger.setLevel(logging.DEBUG)
            else:
                main.logger.setLevel(logging.INFO)

            try:
                # Call method and manage session in case of error
                err, res = func(event, context)
            except Exception as e:
                logger.error("An internal server error occurred", e)
                err, res = e, None
                err.status_code = '500'

            return response(err, res)