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

Throw an exception on JXA failure on MacOS #57

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

jftsang
Copy link

@jftsang jftsang commented Feb 1, 2022

Covers #56 by raising a RuntimeError (instead of failing silently) when calling the JXA (on MacOS) gives a nonzero return code, rather than failing silently. Also increased test coverage of when the input or output paths do not have the expected file extensions.

Under this change, if `/usr/bin/osascript -l JavaScript ...` exits with
a nonzero code then a RuntimeError is raised (AlJohri#56). This avoids
convert() failing silently when used in a script.

Test plan: The new unit test checks that a RuntimeError is raised, if it
is running on a MacOS system where Microsoft Word is not available.

This change needs to be manually tested on a MacOS system where Microsoft Word
is available. It should not have affected behaviour on Windows.
Issue AlJohri#56. Instead of raising a RuntimeError with the message from the
JXA, which is a little cryptic, instead check for Microsoft Word before
even trying to run the JXA, and raise an EnvironmentError if not.

Test plan: Updated unit test reflects this. Needs manual testing on a
system that has Microsoft Word.
@AlJohri
Copy link
Owner

AlJohri commented Feb 2, 2022

@jftsang thanks for submitting the PR and adding our first tests!

While it would be great to serve an informative message if "Microsoft Word" is not installed, I think it might be inefficient to always check on each invocation of convert

my suggestion: put this functionality behind a flag / argument to the macos method

when using convert as a function, keep it defaulted to False
but when using the CLI, change the flag to default to True

what do you think?

@AlJohri
Copy link
Owner

AlJohri commented Feb 2, 2022

in addition, can we split the PR for MS Word not installed and missing docx file for easier review/merging? easier to tackle one new feature/bugfix at a time

@AlJohri
Copy link
Owner

AlJohri commented Feb 2, 2022

alternatively, instead of eager checking, is there a way we can catch the error either in JXA or Python when the command fails?

@AlJohri
Copy link
Owner

AlJohri commented Feb 2, 2022

one very simple approach could be just getting the error code from the JXA invocation. testing with a small sample script:

#!/usr/bin/osascript -l JavaScript

ObjC.import("stdlib");
SystemEvents = Application("System Events");

const word = Application("Microsoft Word");

if (!word.running()) {
    word.activate();
    delay(1);
    SystemEvents.processes["Microsoft Word"].visible = false;
} else {
    word.launch();
    SystemEvents.processes["Microsoft Word"].visible = false;
}

If I use "Microsoft Word", I get an exit code of 0. When I use a program I don't have installed like "Microsoft Visio" I get an exit code of 1.

@AlJohri
Copy link
Owner

AlJohri commented Feb 2, 2022

TL;DR as much as possible, I would rather not eager check and instead just propagate the JXA error

❯ ./test.jxa
./test.jxa: execution error: Error: Error: Application can't be found. (-2700)

❯ echo $?
1

if we want, we can parse the error returned and wrap it with a nicer error that explicitly tells the user "Application can't be found." means Microsoft Word isn't installed

@jftsang
Copy link
Author

jftsang commented Feb 3, 2022

adding our first tests

Great! I'll try to write some for Windows when I get my hands on a Windows machine that has Microsoft Word.

in addition, can we split the PR for MS Word not installed and missing docx file for easier review/merging? easier to tackle one new feature/bugfix at a time

This PR doesn't do anything to check missing docx, except possibly if the JXA exits with nonzero code on account of being able to launch MS Word but the docx is missing (I don't know if that's the case or not as I don't have MS Word).

if we want, we can parse the error returned and wrap it with a nicer error that explicitly tells the user "Application can't be found." means Microsoft Word isn't installed

Makes sense. In that case I'll rollback that last commit.

PR AlJohri#57, Issue AlJohri#56

This reverts commit acf494e.

Eager checking could be slow especially if convert() is being run on a
lot of different functions. Let's just rely on the JXA exiting with a
nonzero return code and giving the message "Application can't be found"
if and only if Microsoft Word cannot be launched, and raise an
EnvironmentError (or OSError) in that case. If JXA exits with a nonzero
return code with any other message, raise RuntimeError.

See discussion at AlJohri#57 (comment)
@jftsang
Copy link
Author

jftsang commented Feb 3, 2022

Makes sense. In that case I'll rollback that last commit.

...done.

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