case_clause错误couchdb与摇篮

时间:2014-11-10 14:42:40

标签: node.js couchdb cradle

当我尝试将文档添加到couchdb时,出现以下错误:

{case_clause, {[{<<"error">>, <<"invalid value">>}, {<<"reason">>, [<<"you need to give the ticket a ticketnumber">>]}]}}}

我的validate_doc_update方法如下所示:

function (newDoc, oldDoc, usrCtx) {
    var error = { reason: [], error: '' }
    switch (newDoc.type) {
        case 'application':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the application a name")
            }
            break;
        case 'client':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the client a name")
            }
            break;
        case 'department':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the department a name")
            }
            break;
        case 'release':
            if (!newDoc.name) {
                error.error = "invalid value"
                error.reason.push("you need to give the release a name")
            }
            break;
        case 'ticket':
            if (!newDoc.ticketnumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a ticketnumber")
            }
            if (!newDoc.description) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a description")
            }
            if (newDoc.priority && !/\d*[,|.]\d*/.test(newDoc.priority)) {
                error.error = "invalid value"
                error.reason.push("the priority is invalid")
            }
            if (!newDoc.minutesperweek) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in minutes per week")
            } else if (!/\d*[,|.]?\d*/.test(newDoc.minutesperweek)) {
                error.error = "invalid value"
                error.reason.push("the impact in minutes per week is invalid")
            }
            if (!newDoc.ordervolume) {
                error.error = "invalid value"
                error.reason.push("you need to give the ticket a impact in orders per week")
            } else if (!/\d*/.test(newDoc.ordervolume)) {
                error.error = "invalid value"
                error.reason.push("the impact in orders per week is invalid")
            }
            break;
        case 'worker':
            if (!newDoc.firstname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a firstname")
            }
            if (!newDoc.lastname) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a lastname")
            }
            if (!newDoc.emailaddress) {
                error.error = "invalid value"
                error.reason.push("\r\nyou need to give the worker an emailaddress")
            } else if (!/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(newDoc.emailaddress)) {
                error.error = "invalid value"
                error.reason.push("the emailaddress is invalid")
            }
            if (!newDoc.phonenumber) {
                error.error = "invalid value"
                error.reason.push("you need to give the worker a phonenumber")
            } else if (!/\d/g.test(newDoc.phonenumber)) {
                error.error = "invalid value"
                error.reason.push("the phonenumber is invalid")
            }
            break;
    }
    if (error.error != '') {
        throw { 'error': error.error, 'reason': error.reason }
    }
}

我希望,我得到的结果是:

{ 'error': 'Invalid value', 'reason': 'you need to give the ticket a ticketnumber' }

但是我得到了我在开始时写的错误。我的问题是什么?

我使用cradle和node.js,如果重要的话。

1 个答案:

答案 0 :(得分:2)

validate_doc_update函数只会抛出禁止或未经授权的错误(分别针对403和401 HTTP响应)。尝试:

throw({forbidden: { 'error': error.error, 'reason': error.reason }});