CodeSOD: Reasonable Lint

This post was originally published on this site

The Daily WTF

While testing their application, Nicholas found some broken error messages. Specifically, they were the embarassing “printing out JavaScript values” types of errors, so obviously something was broken on the server side.

“Oh, that can’t be,” said his senior developer. “We have a module that turns all of the errors into friendly error messages. We use it everywhere, so that can’t be the problem.”

Nicholas dug in, and found this NodeJS block, written by that senior developer.

const reasons = require(‘reasons’); const handleUploadError = function (err, res) { if (err) { var code = 500; var reason = reasons([{ message: ‘Internal Error’}]) if (err === ‘errorCondition1’) { code = 400; reason = reasons([{message: ‘Message 1’}]); } else if (err === ‘errorCondition2’) { code = 400; reason = reasons([{message: ‘Message 2’}]); } else if (err === ‘errorCondition3’) { code = 422; reason = reasons([{message: ‘Message 3’}]); // else if pattern repeated

To read the full article click on the 'post' link at the top.