Skip to content

Commit

Permalink
Better error messages for topic validations
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jul 21, 2017
1 parent 98cbfee commit 8226e91
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/handlers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var write = require('../write')
var fastfall = require('fastfall')
var Packet = require('aedes-packet')
var through = require('through2')
var validateSubscribeTopic = require('./validations').validateSubscribeTopic
var validateTopic = require('./validations').validateTopic
var topicActions = fastfall([
authorize,
storeSubscriptions,
Expand Down Expand Up @@ -37,7 +37,7 @@ function doSubscribe (sub, done) {

function authorize (sub, done) {
var client = this.client
var err = validateSubscribeTopic(sub.topic)
var err = validateTopic(sub.topic, 'SUBSCRIBE')
if (err) {
return done(err)
}
Expand Down
4 changes: 2 additions & 2 deletions lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

var write = require('../write')
var validateSubscribeTopic = require('./validations').validateSubscribeTopic
var validateTopic = require('./validations').validateTopic

function UnsubscribeState (client, packet, finish, granted) {
this.client = client
Expand All @@ -16,7 +16,7 @@ function handleUnsubscribe (client, packet, done) {
var err

for (var i = 0; i < unsubscriptions.length; i++) {
err = validateSubscribeTopic(unsubscriptions[i])
err = validateTopic(unsubscriptions[i], 'UNSUBSCRIBE')
if (err) {
return done(err)
}
Expand Down
10 changes: 5 additions & 5 deletions lib/handlers/validations.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
'use strict'

function validateSubscribeTopic (topic) {
function validateTopic (topic, message) {
var end = topic.length - 1
var endMinus = end - 1
var slashInPreEnd = endMinus > 0 && topic.charCodeAt(endMinus) !== 47
if (topic.length === 0) {
return new Error('impossible to subscribe to an empty topic')
return new Error('impossible to ' + message + ' to an empty topic')
}
for (var i = 0; i < topic.length; i++) {
switch (topic.charCodeAt(i)) {
case 35:
var notAtTheEnd = i !== end
if (notAtTheEnd || slashInPreEnd) {
return new Error('# is only allowed in SUBSCRIBE in the last position')
return new Error('# is only allowed in ' + message + ' in the last position')
}
break
case 43:
var pastChar = i < end - 1 && topic.charCodeAt(i + 1) !== 47
var preChar = i > 1 && topic.charCodeAt(i - 1) !== 47
if (pastChar || preChar) {
return new Error('+ is only allowed in SUBSCRIBE between /')
return new Error('+ is only allowed in ' + message + ' between /')
}
break
}
}
}

module.exports.validateSubscribeTopic = validateSubscribeTopic
module.exports.validateTopic = validateTopic

0 comments on commit 8226e91

Please sign in to comment.