Skip to content

Commit

Permalink
Other: Trying out a more aggressive aproach for custom error subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Mar 11, 2017
1 parent 95cd64e commit 4b49f50
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 57 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function decoder(mtype) {
var rfield = mtype._fieldsArray[i];
if (rfield.required) gen
("if(!m.hasOwnProperty(%j))", rfield.name)
("throw util.ProtocolError(%j,m)", missing(rfield));
("throw util.ProtocolError(%j,{instance:m})", missing(rfield));
}

return gen
Expand Down
65 changes: 60 additions & 5 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ util.pool = require("@protobufjs/pool");
// utility to work with the low and high bits of a 64 bit value
util.LongBits = require("./longbits");

// error subclass indicating a protocol specifc error
util.ProtocolError = require("./protocolerror");

/**
* An immuable empty array.
* @memberof util
Expand Down Expand Up @@ -181,17 +178,20 @@ util.longFromHash = function longFromHash(hash, unsigned) {

/**
* Merges the properties of the source object into the destination object.
* @memberof util
* @param {Object.<string,*>} dst Destination object
* @param {Object.<string,*>} src Source object
* @param {boolean} [ifNotSet=false] Merges only if the key is not already set
* @returns {Object.<string,*>} Destination object
*/
util.merge = function merge(dst, src, ifNotSet) { // used by converters
function merge(dst, src, ifNotSet) { // used by converters
for (var keys = Object.keys(src), i = 0; i < keys.length; ++i)
if (dst[keys[i]] === undefined || !ifNotSet)
dst[keys[i]] = src[keys[i]];
return dst;
};
}

util.merge = merge;

/**
* Converts the first character of a string to lower case.
Expand All @@ -202,6 +202,61 @@ util.lcFirst = function lcFirst(str) {
return str.charAt(0).toLowerCase() + str.substring(1);
};

/**
* Creates a custom error constructor.
* @memberof util
* @param {string} name Error name
* @returns {function} Custom error constructor
*/
function newError(name) {

function CustomError(message, properties) {

if (!(this instanceof CustomError))
return new CustomError(message, properties);

Error.call(this, message);
Object.defineProperty(this, "message", { get: function() { return message; } });
if (Error.captureStackTrace)
Error.captureStackTrace(this, CustomError);
else
Object.defineProperty(this, "stack", { value: (new Error()).stack || "" });

if (properties)
merge(this, properties);
}

(CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;

Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });

CustomError.prototype.toString = function toString() {
return this.name + ": " + this.message;
};

return CustomError;
}

util.newError = newError;

/**
* Constructs a new protocol error.
* @classdesc Error subclass indicating a protocol specifc error.
* @memberof util
* @extends Error
* @constructor
* @param {string} message Error message
* @param {Object.<string,*>=} properties Additional properties
* @example
* try {
* MyMessage.decode(someBuffer); // throws if required fields are missing
* } catch (e) {
* if (e instanceof ProtocolError && e.instance)
* console.log("decoded so far: " + JSON.stringify(e.instance));
* }
*/
util.ProtocolError = newError("ProtocolError");

/**
* Builds a getter for a oneof's present field name.
* @param {string[]} fieldNames Field names
Expand Down
51 changes: 0 additions & 51 deletions src/util/protocolerror.js

This file was deleted.

0 comments on commit 4b49f50

Please sign in to comment.