From 06429775383001d0eee370f8eab9dc2b507cfe73 Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sat, 21 Feb 2015 20:17:12 +0300 Subject: [PATCH] Add error.type2str --- lib/messages.js | 14 ++++++++++++++ tests/unit.js | 2 ++ 2 files changed, 16 insertions(+) diff --git a/lib/messages.js b/lib/messages.js index daa9d2a..ab3b86e 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -452,6 +452,20 @@ var error = exports.error = { */ FATAL: 2, + /** + * Convert error type to a human-readable string. + * @param {number} fatal - Type of the error + * @return {string} + */ + type2str: function(fatal) { + switch (fatal) { + case error.WARNING: return "warning"; + case error.ERROR: return "error"; + case error.FATAL: return "fatal"; + default: return "unknown"; + } + }, + /** * @typedef {Object} DecodeResult * @property {number} fatal - Type of the error diff --git a/tests/unit.js b/tests/unit.js index ca921b1..1acf65e 100644 --- a/tests/unit.js +++ b/tests/unit.js @@ -596,6 +596,7 @@ describe("Message types", function() { expect(res.vector).to.not.exist; expect(res.errorText).to.equal("test"); expect(res.length).to.equal(8); + expect(error.type2str(res.fatal)).to.equal("warning"); var vector = inv_vect.encode(Buffer("test")); var res = error.decode(error.encode({ @@ -609,6 +610,7 @@ describe("Message types", function() { expect(bufferEqual(res.vector, vector)).to.be.true; expect(res.errorText).to.equal("fatal error"); expect(res.length).to.equal(47); + expect(error.type2str(res.fatal)).to.equal("fatal"); }); }); });