У меня проблема с перехватом ошибок в модуле fs именно writeFile.
Я пытался попробовать / перехватить ошибку перехвата и записи в журналы, но это не работает.В debbuger error не идите на блок catch, и после многократного вызова ничего не получается.
const fs = require('fs');
const writeData = (body) => {
try{
const stringData = JSON.stringify(body);
fs.writeFile('./data76/data.json', stringData, {flag: 'a+'} ,(err) =>{
if(!err){
console.log('file was writed');
}else {
console.error('Error writing new data');
throw new Error(err);
}
});
}catch(error){
console.error('THIS ERROR!!!!!');
let dataError = JSON.stringify(error);
fs.open('./logs/error.log', 'wx', (err, fd) => {
if(!err){
fs.writeFile(fd, dataError, (err) => {
if(!err){
fs.close(fd, (err) => {
if(!err){
console.log('log was written success');
}else{
console.error('file log wasn\'t closed');
}
});
}else{
console.error('log wasn\'t writed');
}
});
}else {
fs.writeFile(fd, dataError, (err) => {
if(!err){
console.log('log was writed');
}else {
console.error('log wasn\'t writed');
}
});
}
});
};
};
const body = {
"name": {
"first": "John",
"jobtitle": "Driver"
},
"uuid_company": "ab667468-d7c5-469e-b402-0bd2d71073fa",
"license_number": "AB58659",
"phone": "+19019876512",
"uuid": "08475753",
"place_id": "place_not_matter",
"birthday": "10.12.1967"
};
writeData(body);
В результате я жду ошибки catch и записываю ее в файл.Мой личный регистратор