Reputation: 57185
This is NodeJS code but it is a general programming question on best practice.
The following code checks for the file system for HTTPS certs. If they exist it creates an HTTPS server. If the files do not exist it returns undefined. If an unexpected exception happens (which it never does and I'm not sure it ever could) it throws it.
Please assume that the throw block is best practice for the sake of this question (which I would argue it is but I don't want answers to get distracted by this issue).
Now the code which throws the exception is not being hit EVER as revealed by my code coverage tool. How should I handle this?
My options are ...
1 - Remove the code since it is never hit - but then if an unexpected exception was thrown the catch block would mask it!
2 - Keep the code there and tell the coverage tool to ignore it
3 - Do something else
Please advise best practice. Many thanks
createHttpsServer(certDir) {
if (!certDir) {
return undefined;
}
try {
let options = {
key: fs.readFileSync(`${certDir}/privkey.pem`),
cert: fs.readFileSync(`${certDir}/fullchain.pem`),
ca: fs.readFileSync(`${certDir}/chain.pem`)
};
return https.createServer(options, this.app);
} catch (err) {
if (err.code === 'ENOENT') {
return undefined;
} else {
throw err;
}
}
}
Upvotes: 0
Views: 35
Reputation: 100
Have to agree with Robert's comment on this one. I would suggest keeping the code there. "I'm not sure it ever could" is a trap that I myself have fallen for many a time.
Upvotes: 1