Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transport): show contents of invalid RPC message in TypeError #404

Merged
merged 3 commits into from
Oct 9, 2024

Conversation

andrewDoing
Copy link
Contributor

@andrewDoing andrewDoing commented Sep 28, 2024

resolved.value == null
? 'null or undefined'
: typeof resolved.value;
this.logger.debug(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be safe, let's not involve the logger. this is low-level code and we don't want to get into loops or anything like that.

  • instead of printing the type, let's print the actual resolved.value use node.util.inspect.
    • example:
      import { inspect } from 'node:util';
      const level = process.env.NVIM_NODE_LOG_LEVEL || 'debug';
      const loggerKeys = ['info', 'warn', 'error', 'debug', 'level'] as const;
      export type Logger = Pick<winston.Logger, (typeof loggerKeys)[number]>;
      function getFormat(colorize: boolean) {
      return winston.format.combine(
      winston.format.splat(),
      winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss',
      }),
      winston.format.printf(info => {
      let msg: string;
      try {
      msg =
      typeof info.message === 'object'
      ? inspect(info.message, false, 2, colorize)
      : info.message;
  • to be extra safe, let's wrap the inspect() in a try-catch.
let valstr = '?'
try {
  valstr = inspect(resolved.value, { sorted: true, maxArrayLength: 10, maxStringLength: 500, compact: true, breakLength = 500 })
} catch {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think now? I put a console.error within the new catch block since I don't like the idea of swallowing the error.

Also do I need to add unit tests to pass CI for this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put a console.error within the new catch block since I don't like the idea of swallowing the error.

That's usually a good instinct, but in this case the failure will be signaled because we'll show "?" in the thrown error. And this should be rare, so better to wait until it's a problem since this is a low-level codepath.

@justinmk
Copy link
Member

justinmk commented Oct 9, 2024

will push some updates then merge and release

@justinmk justinmk force-pushed the andrewDoing-patch-1 branch from f91377f to 19e2000 Compare October 9, 2024 15:50
@justinmk justinmk changed the title Add log and update TypeError thrown in transport.ts feat(transport): show contents of invalid RPC message in TypeError Oct 9, 2024
@justinmk justinmk force-pushed the andrewDoing-patch-1 branch from 19e2000 to 20ceff4 Compare October 9, 2024 16:06
Problem:
Exception does not give any hint about the contents of an invalid RPC
message:

    "TypeError: expected msgpack result to be array"

Solution:
Show the payload contents in the error message. Example:

    "TypeError: expected msgpack array, got: …"
@justinmk justinmk force-pushed the andrewDoing-patch-1 branch from 20ceff4 to 59136a4 Compare October 9, 2024 16:11
@justinmk justinmk merged commit add8035 into neovim:master Oct 9, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants