有没有办法 return Djangular djangoRMI 方法调用的 HttpResponseServerError?

Is there a way to return a HttpResponseServerError for a Djangular djangoRMI method call?

我有几个使用 djangular 远程方法调用的方法。

目前,当其中一个失败时,我只是 return 一个 dict/json 错误 属性 我在 JavaScript 端检查我的应用程序。

@allow_remote_invocation
def do_a_thing(self):
    return {'error':'oops', 'description':'the thing broke'}

这很好,但有时该方法会超出 Python 的控制范围,我需要在 JavaScript 端使用 error_callback。

我最终复制了错误处理,这让我哭了一个晚上。

function call_the_thing(){
    djangoRMI.do_a_thing().then(function(data){
        if(data.error){
            handleError(data.error)
        }else{
            ....
        }
    },function(rejectReason){
        handleError(rejectReason)
    }
} 

那天我的想法是,必须有一种方法来 return 和 python 方面的错误,而不仅仅是错误数据的成功。

我希望我可以用 python dict 包装在 HttpResponseServerError 中,但我无法理解如何做比 return 更多的字符串,也不知道如何制作它对于 Djangular。

这是 return 服务器错误(如 HttpResponseServerError)的正确方法吗?还是因为djangular我需要把它当作一个民族啊我一直在做什么?

n.b。我还没来得及 spend/waste 上班 。当我这样做时,如果这里没有合适的答案,我会自己回答。但我也对什么是好的做法感兴趣 add returning JSON 感觉就像一个 hack。

JSONResponseException

它像常规的 django 异常一样抛出,但消息 kwarg 可以包含 JSON 内容。它默认为 400 的响应状态,但可以使用另一个 kwarg 进行设置。

所以现在你可以写:

@allow_remote_invocation
def do_a_thing(self):
    if not thing.do():
        return thing.do()
    message = {'error':'oops', 'description':'the thing broke'}
    status = 500
    raise JSONResponseException(message=message,status=status)