error.fatal -> error.type

This commit is contained in:
Kagami Hiiragi 2015-02-24 19:18:49 +03:00
parent 96d63f0613
commit 2bdd415835
2 changed files with 16 additions and 19 deletions

View File

@ -454,11 +454,11 @@ var error = exports.error = {
/**
* Convert error type to a human-readable string.
* @param {number} fatal - Type of the error
* @param {number} type - Type of the error
* @return {string}
*/
type2str: function(fatal) {
switch (fatal) {
type2str: function(type) {
switch (type) {
case error.WARNING: return "warning";
case error.ERROR: return "error";
case error.FATAL: return "fatal";
@ -466,12 +466,9 @@ var error = exports.error = {
}
},
// XXX(Kagami): Rename `fatal` field to `type`? `fatal` used in spec
// but it's rather unintuitive.
/**
* @typedef {Object} DecodeResult
* @property {number} fatal - Type of the error
* @property {number} type - Type of the error
* @property {number} banTime - The other node informs that it will
* not accept further connections for this number of seconds
* @property {?Buffer} vector -
@ -500,8 +497,8 @@ var error = exports.error = {
*/
decodePayload: function(buf) {
assert(buf.length >= 4, "Buffer is too small");
var decodedFatal = structs.var_int.decode(buf);
var decodedBanTime = structs.var_int.decode(decodedFatal.rest);
var decodedType = structs.var_int.decode(buf);
var decodedBanTime = structs.var_int.decode(decodedType.rest);
var decodedVectorLength = structs.var_int.decode(decodedBanTime.rest);
// NOTE(Kagami): Inventory vector should be only 32-byte in size but
@ -518,13 +515,13 @@ var error = exports.error = {
var decodedErrorText = structs.var_str.decode(rest);
var length = (
decodedFatal.length +
decodedType.length +
decodedBanTime.length +
decodedVectorLength.length + vectorLength +
decodedErrorText.length
);
return {
fatal: decodedFatal.value,
type: decodedType.value,
banTime: decodedBanTime.value,
vector: vector,
errorText: decodedErrorText.str,
@ -536,7 +533,7 @@ var error = exports.error = {
/**
* Encode `error` message.
* @param {Object} opts - Error options
* @param {number=} opts.fatal - Type of the error
* @param {number=} opts.type - Type of the error
* ([warning]{@link module:bitmessage/messages.error.WARNING} by
* default)
* @param {number=} opts.banTime - Inform the other node, that you
@ -558,12 +555,12 @@ var error = exports.error = {
* The same as [encode]{@link module:bitmessage/messages.error.encode}.
*/
encodePayload: function(opts) {
var fatal = opts.fatal || error.WARNING;
var type = opts.type || error.WARNING;
var banTime = opts.banTime || 0;
// TODO(Kagami): Validate vector length.
var vector = opts.vector || new Buffer(0);
return Buffer.concat([
structs.var_int.encode(fatal),
structs.var_int.encode(type),
structs.var_int.encode(banTime),
structs.var_int.encode(vector.length),
vector,

View File

@ -599,26 +599,26 @@ describe("Message types", function() {
var encoded = error.encode({errorText: "test"});
expect(message.decode(encoded).command).to.equal("error");
var res = error.decode(encoded);
expect(res.fatal).to.equal(0);
expect(res.type).to.equal(0);
expect(res.banTime).to.equal(0);
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");
expect(error.type2str(res.type)).to.equal("warning");
var vector = inv_vect.encode(Buffer("test"));
var res = error.decode(error.encode({
fatal: error.FATAL,
type: error.FATAL,
banTime: 120,
vector: vector,
errorText: "fatal error",
}));
expect(res.fatal).to.equal(2);
expect(res.type).to.equal(2);
expect(res.banTime).to.equal(120);
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");
expect(error.type2str(res.type)).to.equal("fatal");
});
});
});