【发布时间】:2019-01-30 20:28:31
【问题描述】:
我正在开发 Ionic 3 应用程序,该应用程序在蓝牙打印机上打印账单。而且我有点迷茫,如何正确处理这段代码中的错误:
print(billToPrint){
return this.bluetoothSerial.list().then((data)=>{
if(data.length==0) throw new Error("no printer connected");
console.log(data);
console.log(data[0]);
let subscription=this.bluetoothSerial.connect(data[0].address).subscribe(data=>{
console.log("subscribnuto",data);
this.bluetoothSerial.write(billToPrint).then((data)=>{
console.log("data success", data);
});
subscription.unsubscribe();
},err=>{
throw new Error("Connection to printer failed");
});
},err=>{
console.log(err);
});
}
在这种情况下处理错误的正确方法是什么?我希望错误与具体的错误消息一起传播,因此我可以在我的页面中轻松处理它们(此打印方法正在服务中)并显示带有错误消息的警报。有任何想法吗?还是代码结构不好?
【问题讨论】:
标签: javascript angular ionic-framework error-handling