2017-08-16 47 views
0

在回送,當API的一個必要的參數被遺漏,它會返回一些象這樣的錯誤:更改環回默認的錯誤對象

{ 
    "error": { 
     "statusCode": 400, 
     "name": "Error", 
     "message": "fields is a required argument" 
    } 
} 

但我想是這樣的錯誤:

{ 
    "response status": 400, 
    "response body": { 
     "message": "fields is a required argument" 
    } 
} 

我該如何更改LoopBack中的默認錯誤結構?

回答

0

您可以嘗試類似如下:

YourModel.observe('before save', function(context, next) { 
    if (context.error) { 
     // get values from error object that you want, could be on context.error.code 
     var newError = new Error("fields is a required argument"); 
     newError.status = 400; 
     next(error); 
    } 
    //handle other cases 
    }); 

不是字符串的,當然你也可以傳遞一個對象,如果這是什麼,你正在尋找。希望這有幫助

+0

'保存之前'是當你想保存一個實例在數據庫中。但是我的問題是當一個API被調用並且所需的參數被錯過。 –