Ejemplo n.º 1
0
    def run(self,
            event,
            context=None,
            handler=None,
            event_source=None,
            debug=False):
        if context is None:
            context = FlexerContext()
        if context.state is None:
            context.state = FlexerLocalState()

        logger.info('Running handler "%s"', handler)
        try:
            module_name, func_name = self._parse_handler(handler)
            if module_name not in self.modules:
                self.modules[module_name] = self._import_module(module_name)

            func = self._get_handler_from_module(module_name, func_name)

        except FlexerException as e:
            return FlexerResult(value=None,
                                logs=e.stack_trace,
                                error=e.to_dict())

        value, error, stdout = None, None, ''
        headers = {}
        f = sys.stderr if debug else StringIO()

        try:
            with RedirectStdStreams(stdout=f, stderr=f):
                try:
                    value = func(event, context)
                    headers = context.response_headers

                    # if a cmp object type is returned,
                    # encode result and add header
                    if hasattr(value, "cmp_response"):
                        headers['x-cmp-response'] = type(value).__name__
                        value = value.cmp_response()

                except BaseException:
                    del func
                    error = self._format_exception_info(sys.exc_info())

            if not debug:
                stdout = f.getvalue()

        finally:
            if not debug:
                f.close()

            logger.info('Handler completed "%s"', handler)

        if error:
            stdout += error['stack_trace']

        return FlexerResult(value=value,
                            logs=stdout,
                            error=error,
                            headers=headers)
Ejemplo n.º 2
0
    def run(self,
            event,
            context=None,
            handler=None,
            event_source=None,
            debug=False):
        if context is None:
            context = FlexerContext()
        if context.state is None:
            context.state = FlexerLocalState()

        logger.info('Running handler "%s"', handler)
        try:
            module_name, func_name = self._parse_handler(handler)
            if module_name not in self.modules:
                self.modules[module_name] = self._import_module(module_name)

            func = self._get_handler_from_module(module_name, func_name)

        except FlexerException as e:
            return FlexerResult(value=None,
                                logs=e.stack_trace,
                                error=e.to_dict())

        value, error, stdout = None, None, ''
        headers = {}
        f = sys.stderr if debug else StringIO.StringIO()

        try:
            with RedirectStdStreams(stdout=f, stderr=f):
                try:
                    value = func(event, context)
                    headers = context.response_headers

                    # if a cmp object type is returned,
                    # encode result and add header
                    if hasattr(value, "cmp_response"):
                        headers['x-cmp-response'] = type(value).__name__
                        value = value.cmp_response()

                except:
                    del func
                    error = self._format_exception_info(sys.exc_info())

            if not debug:
                stdout = f.getvalue()

        finally:
            if not debug:
                f.close()

            logger.info('Handler completed "%s"', handler)

        if error:
            stdout += error['stack_trace']
        else:
            schema = self._get_validation_schema(event_source, handler)
            if schema:
                validator = Draft4Validator(schema)
                errors = []
                for e in sorted(validator.iter_errors(value), key=str):
                    errors.append(e.message + ' in ' + str(list(e.path)))

                if errors:
                    error = {
                        'exc_message': json.dumps(errors),
                        'exc_type': 'ValidationError'
                    }
                    stdout += json.dumps(errors)

        return FlexerResult(value=value,
                            logs=stdout,
                            error=error,
                            headers=headers)