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

[#403][#410] Improve error messages for pgagroal-cli #411

Merged
merged 1 commit into from
Mar 15, 2024

Conversation

decarv
Copy link
Contributor

@decarv decarv commented Mar 9, 2024

This commit improves the error messages for pgagroal-cli and pgagroal-admin commands by modifying the parse_command function.

It also tries to simplify adding new commands. Now adding a command with the structure " [subcommand] [arg] [arg] ..." requires the addition of the command enum and its struct in the command_table.

This means that the command parsing now involves the interpretation of a command_table defined in each cli.c and admin.c files.

@jesperpedersen jesperpedersen added the enhancement Improvement to an existing feature label Mar 10, 2024
Copy link
Collaborator

@fluca1978 fluca1978 left a comment

Choose a reason for hiding this comment

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

This is a very good job and it almost reflect what I had in mind.
I think there is really a little more to do to mark this as complete.

src/admin.c Outdated Show resolved Hide resolved
src/admin.c Outdated Show resolved Hide resolved
src/include/utils.h Outdated Show resolved Hide resolved
src/include/utils.h Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
src/libpgagroal/utils.c Outdated Show resolved Hide resolved
@decarv
Copy link
Contributor Author

decarv commented Mar 11, 2024

Thank you for your feedback, @fluca1978.

My main goal with this PR was to seek your insights regarding my implementation and identify areas for improvement based on your vision.

Given your response suggests I'm on the right track, I'd like to discuss additional ideas (below) aimed at simplifying our new command handling approach.

As for the issues you highlighted above, I will address them later this evening.

1. Refining Command Execution through Callbacks
I propose shifting from directly assigning action, mode, and log traces to utilizing a callback function activated upon command execution. This function would be the already existing implementation of the actions flush, enabledb, ..., but with a different signature, thereby streamlining the command structure and execution process. The callback could be defined as:

int (*action)(struct pgagroal_parsed_command* cmd) // returns exit_error

The pgagroal_parsed_command struct would then include all necessary information for action execution, which currently varies in signature but shares common elements like SSL* ssl, int socket, bool verbose, char output_format, etc.

With this, eliminating the pgagroal_{cli|action}_command_code seems beneficial, as it appears to add complexity without clear value. We could determine the command count using sizeof(array)/sizeof(array[0]).

Initially, I hesitated to implement these changes as they would necessitate further modifications to the existing code. What do you think?

2. Simplifying Error Handling
I suggest removing the error_message field from the command struct, as I believe directly outputting error messages to stderr would be more straightforward and efficient. Is retaining error_message essential in your view?

@fluca1978
Copy link
Collaborator

Thank you for your feedback, @fluca1978.

My main goal with this PR was to seek your insights regarding my implementation and identify areas for improvement based on your vision.

Given your response suggests I'm on the right track, I'd like to discuss additional ideas (below) aimed at simplifying our new command handling approach.

As for the issues you highlighted above, I will address them later this evening.

1. Refining Command Execution through Callbacks I propose shifting from directly assigning action, mode, and log traces to utilizing a callback function activated upon command execution. This function would be the already existing implementation of the actions flush, enabledb, ..., but with a different signature, thereby streamlining the command structure and execution process. The callback could be defined as:

int (*action)(struct pgagroal_parsed_command* cmd) // returns exit_error

The pgagroal_parsed_command struct would then include all necessary information for action execution, which currently varies in signature but shares common elements like SSL* ssl, int socket, bool verbose, char output_format, etc.

This is clearly an optimal solution, even if I think it is probably too much for the needs of the project.
I would suffice with this first patch, but if you have time and will to implement a callback based structure, I'm fine with it.

2. Simplifying Error Handling I suggest removing the error_message field from the command struct, as I believe directly outputting error messages to stderr would be more straightforward and efficient. Is retaining error_message essential in your view?

It's not essential, it could be beneficial only if we are not sure at which branch the error is raised and we need to try another branch, otherwise it is fine to output it directly having clear that this also means exiting from the parsing loop, which may be too early (?).
Again, I'm almost fine with the status you reached in 6bb59bb but if you have more time and will, let's try to improve your implementation!

@decarv decarv force-pushed the improve_error_messages_2 branch from 6bb59bb to d25097a Compare March 13, 2024 01:09
@decarv
Copy link
Contributor Author

decarv commented Mar 13, 2024

While attempting to implement the callback based structure, I found myself redesigning the entire file, which wasn't the goal, so I decided to abort that approach.

Could you check the PR and let me know if it looks good to you? I'm here to help with any adjustments you need.

@fluca1978
Copy link
Collaborator

@decarv I added a few comments on e57675d, please do the review. Also, could you please reword the commit message to include a little more details about the changes? Not a complete description but something more verbose.

@decarv decarv force-pushed the improve_error_messages_2 branch from e57675d to 19f30dc Compare March 14, 2024 22:49
@fluca1978
Copy link
Collaborator

@decarv I think the last commit is fine. Thanks.

@jesperpedersen PTAL commit 19f30dc

src/include/utils.h Outdated Show resolved Hide resolved
src/include/utils.h Outdated Show resolved Hide resolved
src/include/utils.h Outdated Show resolved Hide resolved
@jesperpedersen
Copy link
Collaborator

Otherwise it looks good

This commit improves the error messages for pgagroal-cli and pgagroal-admin commands by modifying the `parse_command` function.

`parse_command` now involves the interpretation of a `command_table` of `struct pgagroal_command`, defined in each cli.c and admin.c files.

The `struct pgagroal_command` holds, beyond other things, the command, the subcommand and the accepted count of arguments.

With this information, `parse_command` is now able to display error messages when (a) the typed command is invalid, (b) the typed command requires a subcommand, (c) the typed subcommand is invalid, or when, (d) for the typed command, there are too few or too many arguments.

Now adding a command with the same invoking structure as the others (i.e., "<command> [subcommand] [arg] [arg] ...") requires inserting an entry in the `command_table` by filling the `struct pgagroal_command`.
@decarv decarv force-pushed the improve_error_messages_2 branch from 19f30dc to 11a1f47 Compare March 15, 2024 14:10
@jesperpedersen jesperpedersen merged commit 11a1f47 into agroal:master Mar 15, 2024
2 checks passed
@jesperpedersen
Copy link
Collaborator

Merged.

Thanks for your contribution !

@decarv
Copy link
Contributor Author

decarv commented Mar 15, 2024

Thank you @fluca1978 and @jesperpedersen for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants