Skip to content

Commit 39b046a

Browse files
committed
fix: docs for toError, fromError, replacer and reviver (no longer being used)
related: #10
1 parent f30da25 commit 39b046a

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/utils/utils.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,11 @@ function parseJSONRPCMessage<T extends JSONValue>(
216216
'Message structure did not match a `JSONRPCMessage`',
217217
);
218218
}
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-
227219
/**
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.
233224
*/
234225
function fromError(error: ErrorRPC<any>, id?: any): JSONValue {
235226
const data: { [key: string]: JSONValue } = {
@@ -267,6 +258,10 @@ const standardErrors = {
267258
ErrorRPCRemote,
268259
ErrorRPC,
269260
};
261+
/**
262+
* Creates a replacer function that omits a specific key during serialization.
263+
* @returns {Function} The replacer function.
264+
*/
270265
const createReplacer = () => {
271266
return (keyToRemove) => {
272267
return (key, value) => {
@@ -300,14 +295,16 @@ const createReplacer = () => {
300295
};
301296
};
302297
};
298+
/**
299+
* The replacer function to customize the serialization process.
300+
*/
303301
const replacer = createReplacer();
304302

305303
/**
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.
311308
*/
312309
function reviver(key: string, value: any): any {
313310
// If the value is an error then reconstruct it
@@ -368,7 +365,13 @@ function reviver(key: string, value: any): any {
368365
return value;
369366
}
370367
}
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+
*/
372375
function toError(errorResponse: any, metadata?: any): ErrorRPCRemote<any> {
373376
if (
374377
typeof errorResponse !== 'object' ||

0 commit comments

Comments
 (0)