@@ -22,9 +22,6 @@ util.pool = require("@protobufjs/pool");
2222// utility to work with the low and high bits of a 64 bit value
2323util . LongBits = require ( "./longbits" ) ;
2424
25- // error subclass indicating a protocol specifc error
26- util . ProtocolError = require ( "./protocolerror" ) ;
27-
2825/**
2926 * An immuable empty array.
3027 * @memberof util
@@ -181,17 +178,20 @@ util.longFromHash = function longFromHash(hash, unsigned) {
181178
182179/**
183180 * Merges the properties of the source object into the destination object.
181+ * @memberof util
184182 * @param {Object.<string,*> } dst Destination object
185183 * @param {Object.<string,*> } src Source object
186184 * @param {boolean } [ifNotSet=false] Merges only if the key is not already set
187185 * @returns {Object.<string,*> } Destination object
188186 */
189- util . merge = function merge ( dst , src , ifNotSet ) { // used by converters
187+ function merge ( dst , src , ifNotSet ) { // used by converters
190188 for ( var keys = Object . keys ( src ) , i = 0 ; i < keys . length ; ++ i )
191189 if ( dst [ keys [ i ] ] === undefined || ! ifNotSet )
192190 dst [ keys [ i ] ] = src [ keys [ i ] ] ;
193191 return dst ;
194- } ;
192+ }
193+
194+ util . merge = merge ;
195195
196196/**
197197 * Converts the first character of a string to lower case.
@@ -202,6 +202,61 @@ util.lcFirst = function lcFirst(str) {
202202 return str . charAt ( 0 ) . toLowerCase ( ) + str . substring ( 1 ) ;
203203} ;
204204
205+ /**
206+ * Creates a custom error constructor.
207+ * @memberof util
208+ * @param {string } name Error name
209+ * @returns {function } Custom error constructor
210+ */
211+ function newError ( name ) {
212+
213+ function CustomError ( message , properties ) {
214+
215+ if ( ! ( this instanceof CustomError ) )
216+ return new CustomError ( message , properties ) ;
217+
218+ Error . call ( this , message ) ;
219+ Object . defineProperty ( this , "message" , { get : function ( ) { return message ; } } ) ;
220+ if ( Error . captureStackTrace )
221+ Error . captureStackTrace ( this , CustomError ) ;
222+ else
223+ Object . defineProperty ( this , "stack" , { value : ( new Error ( ) ) . stack || "" } ) ;
224+
225+ if ( properties )
226+ merge ( this , properties ) ;
227+ }
228+
229+ ( CustomError . prototype = Object . create ( Error . prototype ) ) . constructor = CustomError ;
230+
231+ Object . defineProperty ( CustomError . prototype , "name" , { get : function ( ) { return name ; } } ) ;
232+
233+ CustomError . prototype . toString = function toString ( ) {
234+ return this . name + ": " + this . message ;
235+ } ;
236+
237+ return CustomError ;
238+ }
239+
240+ util . newError = newError ;
241+
242+ /**
243+ * Constructs a new protocol error.
244+ * @classdesc Error subclass indicating a protocol specifc error.
245+ * @memberof util
246+ * @extends Error
247+ * @constructor
248+ * @param {string } message Error message
249+ * @param {Object.<string,*>= } properties Additional properties
250+ * @example
251+ * try {
252+ * MyMessage.decode(someBuffer); // throws if required fields are missing
253+ * } catch (e) {
254+ * if (e instanceof ProtocolError && e.instance)
255+ * console.log("decoded so far: " + JSON.stringify(e.instance));
256+ * }
257+ */
258+ util . ProtocolError = newError ( "ProtocolError" ) ;
259+
205260/**
206261 * Builds a getter for a oneof's present field name.
207262 * @param {string[] } fieldNames Field names
0 commit comments