How to modify error messages in Loopback 4?

我们两清 提交于 2019-12-08 12:28:30

问题


Here's a sample error message generated by Loopback for unique item:

{
    "error": {
        "statusCode": "422",
        "name": "Error",
        "message": "ER_DUP_ENTRY: Duplicate entry 's@a.com' for key 'email'",
        "code": "ER_DUP_ENTRY"
    }
}

However, I want to modify it as:

{
    "errors": {
        "email": [
            "The field email should be unique"
        ]
    }
}

Documentation really didn't help me. Can anybody help me on this please?


回答1:


You can call the next method with the custom error object.

let error = new Error('Custom Error message.'); //Message passed as parameter.
error.name = "UNIQUE_EMAIL";
error.status = 422; //Set status code
next(error); // call the next method with error object. 

LoopBack requires the error object to have a message, status, and the name property.

Refer to there documentation: https://loopback.io/doc/en/lb3/Error-object.html



来源:https://stackoverflow.com/questions/54832216/how-to-modify-error-messages-in-loopback-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!