Skip to content

Comments

grass.script: Fallback to print when g.message fails#5849

Merged
wenzeslaus merged 2 commits intoOSGeo:mainfrom
wenzeslaus:fallback-when-g_message-not-found
Jun 17, 2025
Merged

grass.script: Fallback to print when g.message fails#5849
wenzeslaus merged 2 commits intoOSGeo:mainfrom
wenzeslaus:fallback-when-g_message-not-found

Conversation

@wenzeslaus
Copy link
Member

A certain situation on Windows described in #5717 produces 'OSError: Cannot find the executable g.message' which is missing the original message. While such message is an indication of a deeper issue, the traceback misses the original message. This fallback approach show the message as the function was asked to do even when the subprocess had issues. It attaches the exception which may be confusing, but also may be quite helpful to understand the overall situation (which may not be related to the message itself).

A certain situation on Windows described in OSGeo#5717 produces 'OSError: Cannot find the executable g.message' which is missing the original message. While such message is an indication of a deeper issue, the traceback misses the original message. This fallback approach show the message as the function was asked to do even when the subprocess had issues. It attaches the exception which may be confusing, but also may be quite helpful to understand the overall situation (which may not be related to the message itself).
@wenzeslaus wenzeslaus added the enhancement New feature or request label Jun 6, 2025
@wenzeslaus wenzeslaus added this to the 8.5.0 milestone Jun 6, 2025
@wenzeslaus
Copy link
Member Author

@echoix, this reacts to a traceback in your comment in #5717. My understanding you can't test it in the same situation now when #5717 is merged. I tested locally by breaking the g.message call. This resulted in:

grass ~/grassdata/nc_basic_spm_grass7/user1/ --text
Starting GRASS...
Cleaning up temporary files... (Additionally, there was an error: Cannot find the executable g.messagex)
...
GRASS nc_basic_spm_grass7/user1:grass > r.mask -r
No existing mask to remove (no raster named MASK) (Additionally, there was an error: Cannot find the executable g.messagex)

Both your description and the traceback indicate that the message for your now would be with this PR but without #5717 or with g.message broken:

Location <...> already exists. Operation canceled. (Additionally, there was an error: Cannot find the executable g.message)

In a standard case, the message should be:

ERROR: Location <...> already exists. Operation canceled.

… results in a non-zero return code (likely to facilitate shell scripting).
@github-actions github-actions bot added Python Related code is in Python libraries labels Jun 6, 2025
@echoix
Copy link
Member

echoix commented Jun 6, 2025

I can try to revert locally the previous PR. But you already did it.

Was there some particular way you would want me to try out, and see if it helps or not?

@wenzeslaus
Copy link
Member Author

I don't think that's necessary. On Linux, it works either way, but for testing, I broke the g.message call to get the message.

Copy link
Member

@echoix echoix left a comment

Choose a reason for hiding this comment

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

I don't fully understand what the second commit message tries to say about the exit codes. But in either case, I agree that we should be able to show the error message without having to go through our binaries (when we can't find them).

Did you consider or try what the output would look like if exception chaining was used?
With this, we are kind of hiding the fact that there's an error. The g.message must do something more than printing, otherwise we would simply be using prints. So not being able to make use of g.message is still a problem

@wenzeslaus
Copy link
Member Author

I don't fully understand what the second commit message tries to say about the exit codes.

When g.message is asked to print an error message with -e, it returns a non-zero return code.

$ grass --tmp-project XY --exec g.message "Hello"; echo $?
Hello
0
$ grass --tmp-project XY --exec g.message -e "Hello"; echo $?
ERROR: Hello
1

This seems undocumented, but I guess it made sense for shell scripting.

But in either case, I agree that we should be able to show the error message without having to go through our binaries (when we can't find them).

+1

Did you consider or try what the output would look like if exception chaining was used?

The chaining would lead you to the g.message not found error, but the actual error is whatever is being printed.

With this, we are kind of hiding the fact that there's an error.

I do print the "additional" error, so that's how I'm trying to counter that.

The g.message must do something more than printing, otherwise we would simply be using prints. So not being able to make use of g.message is still a problem

It evaluates the verbosity level.

Thanks for the review. I'm merging this.

@wenzeslaus wenzeslaus merged commit 4f9f828 into OSGeo:main Jun 17, 2025
24 checks passed
@wenzeslaus wenzeslaus deleted the fallback-when-g_message-not-found branch June 17, 2025 14:10
@echoix
Copy link
Member

echoix commented Jun 17, 2025

Fine with me with the questions answered

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

Labels

enhancement New feature or request libraries Python Related code is in Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants