Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More descriptive error messages when missing a required field

commit 361378a1de05c346cb07cc20ddd20a75a60f4fa1 1 parent e5797ff
@jhurliman authored
Showing with 22 additions and 7 deletions.
  1. +12 −5 controllers/api.js
  2. +10 −2 controllers/fingerprinter.js
View
17 controllers/api.js
@@ -47,15 +47,22 @@ exports.query = function(req, res) {
exports.ingest = function(req, res) {
var code = req.body.code;
var codeVer = req.body.version;
- var track = req.body.track;
var length = req.body.length;
+ var track = req.body.track;
var artist = req.body.artist;
- if (!code || !codeVer || isNaN(parseInt(length, 10)))
- return server.respond(req, res, 500, { error: 'Missing or invalid required fields' });
-
+ if (!code)
+ return server.respond(req, res, 500, { error: 'Missing "code" field' });
+ if (!codeVer)
+ return server.respond(req, res, 500, { error: 'Missing "version" field' });
if (codeVer != config.codever)
- return server.respond(req, res, 500, { error: 'Invalid version' });
+ return server.respond(req, res, 500, { error: 'Version "' + codeVer + '" does not match required version "' + config.codever + '"' });
+ if (isNaN(parseInt(length, 10))
+ return server.respond(req, res, 500, { error: 'Missing or invalid "length" field' });
+ if (!track)
+ return server.respond(req, res, 500, { error: 'Missing "track" field' });
+ if (!artist)
+ return server.respond(req, res, 500, { error: 'Missing "artist" field' });
fingerprinter.decodeCodeString(code, function(err, fp) {
if (err || !fp.codes.length) {
View
12 controllers/fingerprinter.js
@@ -306,8 +306,16 @@ function ingest(fp, callback) {
log.info('Ingesting track "' + fp.track + '" by artist "' + fp.artist +
'", ' + fp.length + ' seconds, ' + fp.codes.length + ' codes (' + fp.codever + ')');
- if (!fp.codes.length || typeof fp.length !== 'number' || !fp.codever || !fp.track || !fp.artist)
- return callback('Missing required fields', null);
+ if (!fp.codes.length)
+ return callback('Missing "codes" array', null);
+ if (typeof fp.length !== 'number')
+ return callback('Missing or invalid "length" field', null);
+ if (!fp.codever)
+ return callback('Missing or invalid "version" field', null);
+ if (!fp.track)
+ return callback('Missing or invalid "track" field', null);
+ if (!fp.artist)
+ return callback('Missing or invalid "artist" field', null);
fp = cutFPLength(fp, MAX_DURATION);

0 comments on commit 361378a

Please sign in to comment.
Something went wrong with that request. Please try again.