Skip to content

Commit

Permalink
improve error output
Browse files Browse the repository at this point in the history
  • Loading branch information
fscherwi committed Feb 5, 2019
1 parent 684f5f0 commit ca366c6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
23 changes: 1 addition & 22 deletions src/connected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,11 @@ let ipcon;
let connectedList = '';
const data = [];

function errorOutput(code) {
switch (code) {
case 11:
return 'ALREADY CONNECTED';
case 12:
return 'NOT CONNECTED';
case 13:
return 'CONNECT FAILED';
case 21:
return 'INVALID FUNCTION ID';
case 31:
return 'TIMEOUT';
case 41:
return 'INVALID PARAMETER';
case 42:
return 'FUNCTION NOT SUPPORTED';
default:
return 'UNKNOWN';
}
}

function tfinit(HOST, PORT) {
ipcon = new Tinkerforge.IPConnection();
ipcon.connect(HOST, PORT,
error => {
console.log('Error: ' + errorOutput(error));
console.error('Error: ' + require('./error.js').error(error));
process.exit(1);
}
);
Expand Down
20 changes: 20 additions & 0 deletions src/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports.error = function (code) {
switch (code) {
case 11:
return 'ALREADY CONNECTED';
case 12:
return 'NOT CONNECTED';
case 13:
return 'CONNECT FAILED';
case 21:
return 'INVALID FUNCTION ID';
case 31:
return 'TIMEOUT';
case 41:
return 'INVALID PARAMETER';
case 42:
return 'FUNCTION NOT SUPPORTED';
default:
return 'UNKNOWN';
}
};
34 changes: 34 additions & 0 deletions src/error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import test from 'ava';

const errorOutput = require('./error.js');

test('Error Code 11', t => {
t.is(errorOutput.error(11), 'ALREADY CONNECTED');
});

test('Error Code 12', t => {
t.is(errorOutput.error(12), 'NOT CONNECTED');
});

test('Error Code 13', t => {
t.is(errorOutput.error(13), 'CONNECT FAILED');
});

test('Error Code 21', t => {
t.is(errorOutput.error(21), 'INVALID FUNCTION ID');
});

test('Error Code 31', t => {
t.is(errorOutput.error(31), 'TIMEOUT');
});

test('Error Code 41', t => {
t.is(errorOutput.error(41), 'INVALID PARAMETER');
});

test('Error Code 42', t => {
t.is(errorOutput.error(42), 'FUNCTION NOT SUPPORTED');
});
test('Unknown Error Code', t => {
t.is(errorOutput.error(123), 'UNKNOWN');
});

0 comments on commit ca366c6

Please sign in to comment.