2016-12-28 60 views
0

我跟着這個鏈接「https://docs.feathersjs.com/services/readme.html」打造由feathersjs RESTful服務。它適用於正常情況。但我不知道如何向客戶端回覆錯誤消息,如404,500等。以下是服務代碼。它通過promise.resolve返回響應。在這種情況下如何返回錯誤?如何在客戶端的feathersjs服務中響應錯誤?

class MessageService { 
    get(id, params) { 
    return Promise.resolve({ 
     id, 
     read: false, 
     text: `Feathers is great!`, 
     createdAt: new Date.getTime() 
    }); 
    } 
} 

回答

1

您可以使用'feathers-errors'模塊設置錯誤代碼。請參閱下面的示例代碼。

const feathers = require('feathers'); 
const rest = require('feathers-rest'); 
const errors = require('feathers-errors'); 
const app = feathers(); 

app.configure(rest()); 
app.use('/messages', { 
    get(id, params) { 
    console.log(id); 
    if (id === "1") { 
     return Promise.resolve({ 
     id, 
     read: false, 
     text: `Feathers is great!`, 
     createdAt: new Date().getTime() 
     }); 
    } else if (id === "2") { 
     console.log(id);  
     var badRequest = new errors.BadRequest('http 400 bad request'); 
     return Promise.reject(badRequest); 
    } else if (id === "3") {  
     console.log(id);  
     var generalError = new errors.GeneralError('Http 500 general error'); 
     return Promise.reject(generalError); 
    } 

    } 
}); 

app.listen(3030); 

feathers-errors module

相關問題