Skip to content

Commit

Permalink
Add errors.classify
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua T Corbin committed Dec 11, 2015
1 parent 0ef9607 commit c00b7c7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
29 changes: 29 additions & 0 deletions errors.js
Expand Up @@ -134,3 +134,32 @@ module.exports.ZeroLengthChunk = TypedError({
type: 'bufrw.zero-length-chunk',
message: 'zero length chunk encountered'
});

module.exports.classify = classify;

function classify(err) {
switch (err.type) {
case 'bufrw.broken-reader-state':
case 'bufrw.unstable-rw':
return 'Internal';

case 'bufrw.invalid-argument':
case 'bufrw.read.invalid-switch-value':
case 'bufrw.short-buffer':
case 'bufrw.short-read':
case 'bufrw.truncated-read':
case 'bufrw.zero-length-chunk':
return 'Read';

case 'bufrw.fixed-length-mismatch':
case 'bufrw.missing.struct-field':
case 'bufrw.range-error':
case 'bufrw.short-write':
case 'bufrw.write.invalid-switch-value':
return 'Write';

// istanbul ignore next
default:
return null;
}
}
41 changes: 41 additions & 0 deletions test/errors.js
@@ -0,0 +1,41 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

'use strict';

var test = require('tape');

var errors = require('../errors');

test('all errors should be classified', function t(assert) {
var keys = Object.keys(errors);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (key[0].toUpperCase() === key[0]) {
var Type = errors[key];
var err = new Type(new Error('e'));
assert.notEquals(
errors.classify(err), null,
'expected ' + err.type + ' to be classified');
}
}

assert.end();
});
1 change: 1 addition & 0 deletions test/index.js
Expand Up @@ -20,6 +20,7 @@

'use strict';

require('./errors');
require('./interface');
require('./atoms');
require('./fixed_width_rw');
Expand Down

0 comments on commit c00b7c7

Please sign in to comment.