Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Error when pygments not installed #6

Open
fortes opened this issue Feb 3, 2014 · 14 comments
Open

Error when pygments not installed #6

fortes opened this issue Feb 3, 2014 · 14 comments

Comments

@fortes
Copy link

fortes commented Feb 3, 2014

The library throws if pygments isn't installed on the system, with no way to catch the error.

@pksunkara
Copy link
Contributor

Are you sure?

https://github.com/pksunkara/pygments.js/blob/master/pygments.js#L44 makes sure the error returned in the callback.

@fortes
Copy link
Author

fortes commented Feb 4, 2014

@pksunkara Not sure if that link is to the wrong line, but it definitely doesn't error check.

If pygments isn't installed, the following line will throw:

pyg.stdin.write(target);

Because the stdin stream won't work without a real process running. According to the Node docs, the code should check for a failed exec as follows:

var spawn = require('child_process').spawn,
    child = spawn('bad_command');

child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
  if (/^execvp\(\)/.test(data)) {
    console.log('Failed to start child process.');
  }
});

Fixing this will likely require changing the API signature of the colorize callback, which currently expects a single argument.

@pksunkara
Copy link
Contributor

Actually, the module is designed to do that.

Generally, this module is used for building apps which highlight stuff. So, if pygmentize command is not available, highlighting should throw an error and stop the program completely.

@fortes
Copy link
Author

fortes commented Mar 21, 2014

I must be missing something: How does one catch the error so the program doesn't crash? Try/catch around the colorize call doesn't work due to async.

@pksunkara
Copy link
Contributor

Can you show me code? I want to see what exactly you are trying to do.

@fortes
Copy link
Author

fortes commented Mar 21, 2014

var highlight = require("pygments").colorize;

try {
  highlight('puts "Hello World"', 'ruby', 'console', console.log);
}
catch (error) {
  console.error("Error thrown by pygments");
}

Here is the error that is thrown if pygmentize can't be found:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: write EPIPE
    at errnoException (net.js:904:11)
    at Object.afterWrite (net.js:720:19)

@pksunkara
Copy link
Contributor

Yes, this was intentional. I didn't want users to catch the error. Though, It could have a better error message.

@fortes
Copy link
Author

fortes commented Mar 22, 2014

I can't understand why you would want the program to crash. Why not allow developers to catch the error and prevent hard crashes?

@pksunkara
Copy link
Contributor

I looked at the other issues you referenced above. I understand what you are trying to do and will support returning error. Are you interesting in doing the pull request?

@pksunkara pksunkara reopened this Mar 22, 2014
@fortes
Copy link
Author

fortes commented Mar 22, 2014

Sure, however the problem is that this will change the method signature for the colorize callback. In order to fit node style, the first argument will be the error object. Currently, the callback just directly returns the highlighted code.

If you're OK with changing the signature, then I can do the pull request.

@pksunkara
Copy link
Contributor

Yeah, I will just tag it as version v1.0.0

@moroshko
Copy link

Any update on this?

@puneetratan
Copy link

Hi, I am trying to run this module and getting error
Code: var highlight = require("pygments").colorize;

var highlight = require("pygments").colorize;

try {
  highlight('puts "Hello World"', 'ruby', 'console', console.log);
}
catch (error) {
  console.error("Error thrown by pygments");
}
Error:
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: spawn pygmentize ENOENT
    at exports._errnoException (util.js:1020:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:367:16)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
    at Module.runMain (module.js:606:11)
    at run (bootstrap_node.js:389:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:504:3

Can you please help me here.

@pksunkara
Copy link
Contributor

You need to have the pygments tool installed along with this library.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants