error.fatal -> error.type
This commit is contained in:
parent
96d63f0613
commit
2bdd415835
|
@ -454,11 +454,11 @@ var error = exports.error = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert error type to a human-readable string.
|
* Convert error type to a human-readable string.
|
||||||
* @param {number} fatal - Type of the error
|
* @param {number} type - Type of the error
|
||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
type2str: function(fatal) {
|
type2str: function(type) {
|
||||||
switch (fatal) {
|
switch (type) {
|
||||||
case error.WARNING: return "warning";
|
case error.WARNING: return "warning";
|
||||||
case error.ERROR: return "error";
|
case error.ERROR: return "error";
|
||||||
case error.FATAL: return "fatal";
|
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
|
* @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
|
* @property {number} banTime - The other node informs that it will
|
||||||
* not accept further connections for this number of seconds
|
* not accept further connections for this number of seconds
|
||||||
* @property {?Buffer} vector -
|
* @property {?Buffer} vector -
|
||||||
|
@ -500,8 +497,8 @@ var error = exports.error = {
|
||||||
*/
|
*/
|
||||||
decodePayload: function(buf) {
|
decodePayload: function(buf) {
|
||||||
assert(buf.length >= 4, "Buffer is too small");
|
assert(buf.length >= 4, "Buffer is too small");
|
||||||
var decodedFatal = structs.var_int.decode(buf);
|
var decodedType = structs.var_int.decode(buf);
|
||||||
var decodedBanTime = structs.var_int.decode(decodedFatal.rest);
|
var decodedBanTime = structs.var_int.decode(decodedType.rest);
|
||||||
|
|
||||||
var decodedVectorLength = structs.var_int.decode(decodedBanTime.rest);
|
var decodedVectorLength = structs.var_int.decode(decodedBanTime.rest);
|
||||||
// NOTE(Kagami): Inventory vector should be only 32-byte in size but
|
// 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 decodedErrorText = structs.var_str.decode(rest);
|
||||||
var length = (
|
var length = (
|
||||||
decodedFatal.length +
|
decodedType.length +
|
||||||
decodedBanTime.length +
|
decodedBanTime.length +
|
||||||
decodedVectorLength.length + vectorLength +
|
decodedVectorLength.length + vectorLength +
|
||||||
decodedErrorText.length
|
decodedErrorText.length
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
fatal: decodedFatal.value,
|
type: decodedType.value,
|
||||||
banTime: decodedBanTime.value,
|
banTime: decodedBanTime.value,
|
||||||
vector: vector,
|
vector: vector,
|
||||||
errorText: decodedErrorText.str,
|
errorText: decodedErrorText.str,
|
||||||
|
@ -536,7 +533,7 @@ var error = exports.error = {
|
||||||
/**
|
/**
|
||||||
* Encode `error` message.
|
* Encode `error` message.
|
||||||
* @param {Object} opts - Error options
|
* @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
|
* ([warning]{@link module:bitmessage/messages.error.WARNING} by
|
||||||
* default)
|
* default)
|
||||||
* @param {number=} opts.banTime - Inform the other node, that you
|
* @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}.
|
* The same as [encode]{@link module:bitmessage/messages.error.encode}.
|
||||||
*/
|
*/
|
||||||
encodePayload: function(opts) {
|
encodePayload: function(opts) {
|
||||||
var fatal = opts.fatal || error.WARNING;
|
var type = opts.type || error.WARNING;
|
||||||
var banTime = opts.banTime || 0;
|
var banTime = opts.banTime || 0;
|
||||||
// TODO(Kagami): Validate vector length.
|
// TODO(Kagami): Validate vector length.
|
||||||
var vector = opts.vector || new Buffer(0);
|
var vector = opts.vector || new Buffer(0);
|
||||||
return Buffer.concat([
|
return Buffer.concat([
|
||||||
structs.var_int.encode(fatal),
|
structs.var_int.encode(type),
|
||||||
structs.var_int.encode(banTime),
|
structs.var_int.encode(banTime),
|
||||||
structs.var_int.encode(vector.length),
|
structs.var_int.encode(vector.length),
|
||||||
vector,
|
vector,
|
||||||
|
|
|
@ -599,26 +599,26 @@ describe("Message types", function() {
|
||||||
var encoded = error.encode({errorText: "test"});
|
var encoded = error.encode({errorText: "test"});
|
||||||
expect(message.decode(encoded).command).to.equal("error");
|
expect(message.decode(encoded).command).to.equal("error");
|
||||||
var res = error.decode(encoded);
|
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.banTime).to.equal(0);
|
||||||
expect(res.vector).to.not.exist;
|
expect(res.vector).to.not.exist;
|
||||||
expect(res.errorText).to.equal("test");
|
expect(res.errorText).to.equal("test");
|
||||||
expect(res.length).to.equal(8);
|
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 vector = inv_vect.encode(Buffer("test"));
|
||||||
var res = error.decode(error.encode({
|
var res = error.decode(error.encode({
|
||||||
fatal: error.FATAL,
|
type: error.FATAL,
|
||||||
banTime: 120,
|
banTime: 120,
|
||||||
vector: vector,
|
vector: vector,
|
||||||
errorText: "fatal error",
|
errorText: "fatal error",
|
||||||
}));
|
}));
|
||||||
expect(res.fatal).to.equal(2);
|
expect(res.type).to.equal(2);
|
||||||
expect(res.banTime).to.equal(120);
|
expect(res.banTime).to.equal(120);
|
||||||
expect(bufferEqual(res.vector, vector)).to.be.true;
|
expect(bufferEqual(res.vector, vector)).to.be.true;
|
||||||
expect(res.errorText).to.equal("fatal error");
|
expect(res.errorText).to.equal("fatal error");
|
||||||
expect(res.length).to.equal(47);
|
expect(res.length).to.equal(47);
|
||||||
expect(error.type2str(res.fatal)).to.equal("fatal");
|
expect(error.type2str(res.type)).to.equal("fatal");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user