Example #1
0
def format_exception(excinfo, debug=False):
    """Extract informations that can be sent to the client."""
    error = excinfo[1]
    code = getattr(error, 'code', None)
    if code and utils.is_valid_code(code) and utils.is_client_error(code):
        faultstring = (error.faultstring if hasattr(error, 'faultstring')
                       else six.text_type(error))
        r = dict(faultcode="Client",
                 faultstring=faultstring)
        log.debug("Client-side error: %s" % r['faultstring'])
        r['debuginfo'] = None
        return r
    else:
        faultstring = six.text_type(error)
        debuginfo = "\n".join(traceback.format_exception(*excinfo))

        log.error('Server-side error: "%s". Detail: \n%s' % (
            faultstring, debuginfo))

        r = dict(faultcode="Server", faultstring=faultstring)
        if debug:
            r['debuginfo'] = debuginfo
        else:
            r['debuginfo'] = None
        return r
Example #2
0
def format_exception(excinfo, debug=False):
    """Extract informations that can be sent to the client."""
    error = excinfo[1]
    code = getattr(error, 'code', None)
    if code and utils.is_valid_code(code) and utils.is_client_error(code):
        faultstring = (error.faultstring if hasattr(error, 'faultstring')
                       else six.text_type(error))
        r = dict(faultcode="Client",
                 faultstring=faultstring)
        log.debug("Client-side error: %s" % r['faultstring'])
        r['debuginfo'] = None
        return r
    else:
        faultstring = six.text_type(error)
        debuginfo = "\n".join(traceback.format_exception(*excinfo))

        log.error('Server-side error: "%s". Detail: \n%s' % (
            faultstring, debuginfo))

        r = dict(faultcode="Server", faultstring=faultstring)
        if debug:
            r['debuginfo'] = debuginfo
        else:
            r['debuginfo'] = None
        return r
Example #3
0
        def callfunction(self, *args, **kwargs):
            args, kwargs = wsme.rest.args.get_args(
                funcdef, args, kwargs, cherrypy.request.params, None,
                cherrypy.request.body,
                cherrypy.request.headers['Content-Type'])
            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = cherrypy.request
            try:
                result = f(self, *args, **kwargs)
            except Exception:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    if isinstance(orig_exception, cherrypy.HTTPError):
                        orig_code = getattr(orig_exception, 'status', None)
                    else:
                        orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                cherrypy.response.status = 500
                if data['faultcode'] == 'client':
                    cherrypy.response.status = 400
                elif orig_code and is_valid_code(orig_code):
                    cherrypy.response.status = orig_code

                accept = cherrypy.request.headers.get('Accept', "").lower()
                accept = util.simplify_http_accept_header(accept)

                decorators = {'text/xml': wsme.rest.xml.encode_error}
                return decorators.get(accept,
                                      wsme.rest.json.encode_error)(None, data)

            return dict(datatype=funcdef.return_type, result=result)
Example #4
0
File: pecan.py Project: Kjir/wsme
        def callfunction(self, *args, **kwargs):
            return_type = funcdef.return_type

            try:
                args, kwargs = wsme.rest.args.get_args(
                    funcdef, args, kwargs, pecan.request.params, None,
                    pecan.request.body, pecan.request.content_type
                )
                if funcdef.pass_request:
                    kwargs[funcdef.pass_request] = pecan.request
                result = f(self, *args, **kwargs)

                # NOTE: Support setting of status_code with default 201
                pecan.response.status = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    pecan.response.status = result.status_code

                    # NOTE(lucasagomes): If the return code is 204
                    # (No Response) we have to make sure that we are not
                    # returning anything in the body response and the
                    # content-length is 0
                    if result.status_code == 204:
                        return_type = None
                    elif not isinstance(result.return_type,
                                        wsme.types.UnsetType):
                        return_type = result.return_type

                    result = result.obj

            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(
                        exception_info,
                        pecan.conf.get('wsme', {}).get('debug', False)
                    )
                finally:
                    del exception_info

                if orig_code and is_valid_code(orig_code):
                    pecan.response.status = orig_code
                else:
                    pecan.response.status = 500

                return data

            if return_type is None:
                pecan.request.pecan['content_type'] = None
                pecan.response.content_type = None
                return ''

            return dict(
                datatype=return_type,
                result=result
            )
Example #5
0
        def callfunction(self, *args, **kwargs):
            return_type = funcdef.return_type

            try:
                args, kwargs = wsme.rest.args.get_args(
                    funcdef, args, kwargs, pecan.request.params, None,
                    pecan.request.body, pecan.request.content_type)
                if funcdef.pass_request:
                    kwargs[funcdef.pass_request] = pecan.request
                result = f(self, *args, **kwargs)

                # NOTE: Support setting of status_code with default 201
                pecan.response.status = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    pecan.response.status = result.status_code

                    # NOTE(lucasagomes): If the return code is 204
                    # (No Response) we have to make sure that we are not
                    # returning anything in the body response and the
                    # content-length is 0
                    if result.status_code == 204:
                        return_type = None
                    elif not isinstance(result.return_type,
                                        wsme.types.UnsetType):
                        return_type = result.return_type

                    result = result.obj

            except Exception:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(
                        exception_info,
                        pecan.conf.get('wsme', {}).get('debug', False))
                finally:
                    del exception_info

                if orig_code and is_valid_code(orig_code):
                    pecan.response.status = orig_code
                else:
                    pecan.response.status = 500

                return data

            if return_type is None:
                pecan.request.pecan['content_type'] = None
                pecan.response.content_type = None
                return ''

            return dict(datatype=return_type, result=result)
Example #6
0
        def wrapper(*args, **kwargs):
            if ismethod:
                self, args = args[0], args[1:]
            args, kwargs = wsme.rest.args.get_args(
                funcdef, args, kwargs,
                flask.request.args, flask.request.form,
                flask.request.data,
                flask.request.mimetype
            )

            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = flask.request

            dataformat = get_dataformat()

            try:
                if ismethod:
                    args = [self] + list(args)
                result = f(*args, **kwargs)

                # NOTE: Support setting of status_code with default 20
                status_code = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    status_code = result.status_code
                    result = result.obj

                res = flask.make_response(
                    dataformat.encode_result(
                        result,
                        funcdef.return_type
                    )
                )
                res.mimetype = dataformat.content_type
                res.status_code = status_code
            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                res = flask.make_response(dataformat.encode_error(None, data))
                if data['faultcode'] == 'client':
                    res.status_code = 400
                elif orig_code and is_valid_code(orig_code):
                    res.status_code = orig_code
                else:
                    res.status_code = 500
            return res
Example #7
0
        def callfunction(self, *args, **kwargs):
            try:
                args, kwargs = wsme.rest.args.get_args(
                    funcdef, args, kwargs, pecan.request.params, None,
                    pecan.request.body, pecan.request.content_type
                )
                if funcdef.pass_request:
                    kwargs[funcdef.pass_request] = pecan.request
                result = f(self, *args, **kwargs)

                # NOTE: Support setting of status_code with default 201
                pecan.response.status = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    pecan.response.status = result.status_code
                    result = result.obj
                # for download file in rest api
                if result == pecan.response:
                    return result
            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(
                        exception_info,
                        pecan.conf.get('wsme', {}).get('debug', False)
                    )
                finally:
                    del exception_info

                if orig_code and is_valid_code(orig_code):
                    pecan.response.status = orig_code
                else:
                    pecan.response.status = 500

                return data

            if funcdef.return_type is None:
                pecan.request.pecan['content_type'] = None
                pecan.response.content_type = None
                return ''

            return dict(
                datatype=funcdef.return_type,
                result=result
            )
Example #8
0
        def wrapper(*args, **kwargs):
            if ismethod:
                self, args = args[0], args[1:]
            args, kwargs = wsme.rest.args.get_args(funcdef, args, kwargs,
                                                   flask.request.args,
                                                   flask.request.form,
                                                   flask.request.data,
                                                   flask.request.mimetype)

            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = flask.request

            dataformat = get_dataformat()

            try:
                if ismethod:
                    args = [self] + list(args)
                result = f(*args, **kwargs)

                # NOTE: Support setting of status_code with default 20
                status_code = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    status_code = result.status_code
                    result = result.obj

                res = flask.make_response(
                    dataformat.encode_result(result, funcdef.return_type))
                res.mimetype = dataformat.content_type
                res.status_code = status_code
            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                res = flask.make_response(dataformat.encode_error(None, data))
                if data['faultcode'] == 'client':
                    res.status_code = 400
                elif orig_code and is_valid_code(orig_code):
                    res.status_code = orig_code
                else:
                    res.status_code = 500
            return res
Example #9
0
        def callfunction(self, *args, **kwargs):
            try:
                args, kwargs = wsme.rest.args.get_args(
                    funcdef, args, kwargs, pecan.request.params, None,
                    pecan.request.body, pecan.request.content_type
                )
                if funcdef.pass_request:
                    kwargs[funcdef.pass_request] = pecan.request
                result = f(self, *args, **kwargs)

                # NOTE: Support setting of status_code with default 201
                pecan.response.status = funcdef.status_code
                if isinstance(result, wsme.api.Response):
                    pecan.response.status = result.status_code
                    result = result.obj

            except:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(
                        exception_info,
                        pecan.conf.get('wsme', {}).get('debug', False)
                    )
                finally:
                    del exception_info

                if orig_code and is_valid_code(orig_code):
                    pecan.response.status = orig_code
                else:
                    pecan.response.status = 500

                return data

            if funcdef.return_type is None:
                pecan.request.pecan['content_type'] = None
                pecan.response.content_type = None
                return ''

            return dict(
                datatype=funcdef.return_type,
                result=result
            )
Example #10
0
        def callfunction(self, *args, **kwargs):
            args, kwargs = wsme.rest.args.get_args(
                funcdef, args, kwargs,
                cherrypy.request.params, None,
                cherrypy.request.body,
                cherrypy.request.headers['Content-Type']
            )
            if funcdef.pass_request:
                kwargs[funcdef.pass_request] = cherrypy.request
            try:
                result = f(self, *args, **kwargs)
            except Exception:
                try:
                    exception_info = sys.exc_info()
                    orig_exception = exception_info[1]
                    if isinstance(orig_exception, cherrypy.HTTPError):
                        orig_code = getattr(orig_exception, 'status', None)
                    else:
                        orig_code = getattr(orig_exception, 'code', None)
                    data = wsme.api.format_exception(exception_info)
                finally:
                    del exception_info

                cherrypy.response.status = 500
                if data['faultcode'] == 'client':
                    cherrypy.response.status = 400
                elif orig_code and is_valid_code(orig_code):
                    cherrypy.response.status = orig_code

                accept = cherrypy.request.headers.get('Accept', "").lower()
                accept = util.simplify_http_accept_header(accept)

                decorators = {'text/xml': wsme.rest.xml.encode_error}
                return decorators.get(
                    accept,
                    wsme.rest.json.encode_error
                )(None, data)

            return dict(
                datatype=funcdef.return_type,
                result=result
            )
Example #11
0
 def test_validator_with_invalid_str_code(self):
     invalid_str_code = '404'
     self.assertFalse(utils.is_valid_code(invalid_str_code),
                      "Invalid status code not detected")
Example #12
0
 def test_validator_with_valid_code(self):
     valid_code = 404
     self.assertTrue(utils.is_valid_code(valid_code),
                     "Valid status code not detected")
Example #13
0
 def test_validator_with_invalid_str_code(self):
     invalid_str_code = '404'
     self.assertFalse(
         utils.is_valid_code(invalid_str_code),
         "Invalid status code not detected"
     )
Example #14
0
 def test_validator_with_valid_code(self):
     valid_code = 404
     self.assertTrue(
         utils.is_valid_code(valid_code),
         "Valid status code not detected"
     )
Example #15
0
 def test_validator_with_invalid_int_code(self):
     invalid_int_code = 648
     assert (
         not utils.is_valid_code(invalid_int_code),
         "Invalid status code not detected"
     )