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

Exception object not displayed in echo #567

Closed
corporateuser opened this issue Jan 9, 2023 · 5 comments
Closed

Exception object not displayed in echo #567

corporateuser opened this issue Jan 9, 2023 · 5 comments

Comments

@corporateuser
Copy link

Expected Behavior

try {....} catch (e) {console.log(e); echo(e)} should print exception information twice, but echo(e) prints nothing.

Actual Behavior

echo(error) where error is exception object prints nothing

Steps to Reproduce the Problem

  1. create 123.mjs file:
#!/usr/bin/env zx
$.verbose = false;
try {
    const str = 'aa';
    await $`grep ${str}${str} 123.mjs`
} catch (error) {
    console.log(error);
    echo(error)
}
  1. Execute it with zx 123.mjs

Specifications

  • Version: 7.1.1
  • Platform: MacOS 12.6.1, node 18.12.1
@antongolub
Copy link
Collaborator

antongolub commented Jan 9, 2023

echoAPI is not suitable for raw object printing and focuses on tpl literals.

export function echo(pieces: TemplateStringsArray, ...args: any[]) {
  let msg
  const lastIdx = pieces.length - 1
  if (
    Array.isArray(pieces) &&
    pieces.every(isString) &&
    lastIdx === args.length
  ) {
    msg =
      args.map((a, i) => pieces[i] + stringify(a)).join('') + pieces[lastIdx]
  } else {
    msg = [pieces, ...args].map(stringify).join(' ')
  }
  console.log(msg)
}

So you can try smth like this instead:

echo`${error}`

@antonmedv
Copy link
Collaborator

Should we make echo(x) also work?

@zloirock
Copy link
Contributor

zloirock commented Jan 9, 2023

It should work both ways - from https://github.com/google/zx/releases/tag/5.2.0.

@corporateuser
Copy link
Author

Changing function to template literal does not change anything, it still prints nothing for Error object.

@corporateuser
Copy link
Author

Another acceptable solution could be a change of the documentation. Currently it states:

echo()
A console.log() alternative which can take ProcessOutput.

Which is not completely true.

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

No branches or pull requests

4 participants