Hi, iam working on app, that prints bills on bluetooth printer. And iam a little lost, how to properly handle errors in this piece of code:
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);
});
}
Whats the proper way to handle errors in this case? I want errors to propagate with concrete error messages, so i can easily handle them in my page(this print method is in service) and show alert with error message. Or is the structure of the code bad? I cant solve it even using Promise.resolve/reject. Any ideas? It would really help.