@@ -216,20 +216,11 @@ function parseJSONRPCMessage<T extends JSONValue>(
216
216
'Message structure did not match a `JSONRPCMessage`' ,
217
217
) ;
218
218
}
219
-
220
- /**
221
- * Replacer function for serialising errors over RPC (used by `JSON.stringify`
222
- * in `fromError`)
223
- * Polykey errors are handled by their inbuilt `toJSON` method , so this only
224
- * serialises other errors
225
- */
226
-
227
219
/**
228
- * Serializes Error instances into RPC errors
229
- * Use this on the sending side to send exceptions
230
- * Do not send exceptions to clients you do not trust
231
- * If sending to an agent (rather than a client), set sensitive to true to
232
- * prevent sensitive information from being sent over the network
220
+ * Serializes an ErrorRPC instance into a JSONValue object suitable for RPC.
221
+ * @param {ErrorRPC<any> } error - The ErrorRPC instance to serialize.
222
+ * @param {any } [id] - Optional id for the error object in the RPC response.
223
+ * @returns {JSONValue } The serialized ErrorRPC instance.
233
224
*/
234
225
function fromError ( error : ErrorRPC < any > , id ?: any ) : JSONValue {
235
226
const data : { [ key : string ] : JSONValue } = {
@@ -267,6 +258,10 @@ const standardErrors = {
267
258
ErrorRPCRemote,
268
259
ErrorRPC,
269
260
} ;
261
+ /**
262
+ * Creates a replacer function that omits a specific key during serialization.
263
+ * @returns {Function } The replacer function.
264
+ */
270
265
const createReplacer = ( ) => {
271
266
return ( keyToRemove ) => {
272
267
return ( key , value ) => {
@@ -300,14 +295,16 @@ const createReplacer = () => {
300
295
} ;
301
296
} ;
302
297
} ;
298
+ /**
299
+ * The replacer function to customize the serialization process.
300
+ */
303
301
const replacer = createReplacer ( ) ;
304
302
305
303
/**
306
- * Reviver function for deserialising errors sent over RPC (used by
307
- * `JSON.parse` in `toError`)
308
- * The final result returned will always be an error - if the deserialised
309
- * data is of an unknown type then this will be wrapped as an
310
- * `ErrorPolykeyUnknown`
304
+ * Reviver function for deserializing errors sent over RPC.
305
+ * @param {string } key - The key in the JSON object.
306
+ * @param {any } value - The value corresponding to the key in the JSON object.
307
+ * @returns {any } The reconstructed error object or the original value.
311
308
*/
312
309
function reviver ( key : string , value : any ) : any {
313
310
// If the value is an error then reconstruct it
@@ -368,7 +365,13 @@ function reviver(key: string, value: any): any {
368
365
return value ;
369
366
}
370
367
}
371
-
368
+ /**
369
+ * Deserializes an error response object into an ErrorRPCRemote instance.
370
+ * @param {any } errorResponse - The error response object.
371
+ * @param {any } [metadata] - Optional metadata for the deserialized error.
372
+ * @returns {ErrorRPCRemote<any> } The deserialized ErrorRPCRemote instance.
373
+ * @throws {TypeError } If the errorResponse object is invalid.
374
+ */
372
375
function toError ( errorResponse : any , metadata ?: any ) : ErrorRPCRemote < any > {
373
376
if (
374
377
typeof errorResponse !== 'object' ||
0 commit comments