Skip to content

[diagnostics_channel] tracingChannel.traceCallback incorrect types #50996

Description

@Semigradsky

Affected URL(s)

https://nodejs.org/docs/latest-v18.x/api/diagnostics_channel.html#tracingchanneltracecallbackfn-position-context-thisarg-args

Description of the problem

tracingChannel.traceCallback(fn[, position[, context[, thisArg[, ...args]]]]) - almost all args are marked as optional but it can't be called only with the first argument.

const callback = ArrayPrototypeAt(args, position);
validateFunction(callback, 'callback');

^ callback is required, so we need to call it like this:

channels.traceCallback(
	function (callback) {
		// Do something
		callback(null, 'result');
	},
	undefined, // position - `-1` by default
	undefined, // context - `{}` by default
	undefined, // thisArg - any value
	callback,
)

So all arguments are required but some can be undefined.

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    on Dec 1, 2023
  2. added
    diagnostics_channelIssues and PRs related to the diagnostics_channel module.
    on Dec 5, 2023
  3. Flarna commented on Dec 5, 2023

    @Flarna
    Member

    Correct. As tracingChannel.traceCallback() is built for functions receiving at least a callback as argument it's clear that user will provide at least this callback. The defaults for position, context and thisArg aren't that helpful.

    For cases where callback is actually optional in the traced function traceCallback() can't be used as of now.

    Seems implementation is a bit inconsistent in this regard. While a check is done that callback is actually a function it's optional inside wrappedCallback.

    @Qard I think we should relax the typeof check to at least allow null/undefined. This still results in mostly useless defaults for position, context and thisArg but at least the doc is correct again.

  4. Semigradsky commented on Dec 5, 2023

    @Semigradsky
    ContributorAuthor

    By doc I guess that I can call like

    channels.traceCallback(
    	function (callback) {
    		// Do something
    		callback(null, 'result');
    	},
    	callback,
    )

    but I can't 😸

  5. Qard commented on Dec 5, 2023

    @Qard
    Member

    I think I would prefer to adjust the docs to make the other args required as it may be a bit unexpected for a missing callback to be allowed when the function being wrapped potentially would have crashed if it didn't receive a callback. The traceCallback function should probably be similarly modified to only do the wrap when a callback is actually found and let the function being called fail on its own if the callback is not available.

    A bit hard to decide. I'm not sure there's a clearly "correct" way to handle this generically. 🤔

  6. Flarna commented on Dec 6, 2023

    @Flarna
    Member

    created #51068

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    diagnostics_channelIssues and PRs related to the diagnostics_channel module.docIssues and PRs related to Node.js documentation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions