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

Refactors the controls module, adding custom errors #17

Merged
merged 7 commits into from
Nov 1, 2023

Conversation

DrPaulSharp
Copy link
Collaborator

No description provided.

Copy link
Collaborator

@RabiyaF RabiyaF left a comment

Choose a reason for hiding this comment

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

Looks good to me. There is a library called termcolor which I have used to print coloured text on to the terminal. It may offer more formatting options if you are interested. But it works on exactly the same principle of using ANSI color codes under the hood.

RAT/controls.py Outdated
model = controls[procedure](**properties)
except KeyError:
members = list(Procedures.__members__.values())
allowed_values = ', '.join([repr(member.value) for member in members[:-1]]) + f' or {members[-1].value!r}'
Copy link
Contributor

Choose a reason for hiding this comment

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

str cat is not needed below should work
f"{', '.join([repr(member.value) for member in members[:-1]])} or {members[-1].value!r}"

RAT/project.py Outdated
error_string = formatted_pydantic_error(e)
# Use ANSI escape sequences to print error text in red
print('\033[31m' + error_string + '\033[0m')
traceback_string = formatted_traceback()
Copy link
Contributor

@StephenNneji StephenNneji Oct 31, 2023

Choose a reason for hiding this comment

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

I think we still need to propagate the exception with the new error message, otherwise if logging is switched off or an app has no console (typical GUI app), there will be no way for upstream packages to know there is a problem. I am not sure if there is a specific reason the exception is not re-raised

maybe

	tb = exc.__traceback__
        raise ValueError(custom_msg).with_traceback(tb) from None

or

        error_string = formatted_pydantic_error(exc, custom_msgs)
        raise ValueError(error_string) from exc

PS: I used ValueError cause I could not create ValidationError. This way I think the logging is not need

Copy link
Collaborator Author

@DrPaulSharp DrPaulSharp Oct 31, 2023

Choose a reason for hiding this comment

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

Pydantic recommends this way of handling ValidationErrors: https://docs.pydantic.dev/latest/errors/errors/ I'm inclined to agree that raising the error is a better cause of action in general, but I don't know how to do it with the error message we want - I'll have a look and see what I can find though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i.e. - it's dead easy to raise the exception with the standard error message, but that it what we want to change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind if the exception is propagated as is and the upstream modifies the message but their suggestions are for end users only and will not help if there is no console which will be the case with RASCAL. If the best solution is to reraise the exception without modification then you have to modify this code so it doesn't print the same thing twice

Copy link
Contributor

Choose a reason for hiding this comment

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

You could also make a custom RAT validation error and copy the traceback

…rror messages when raising a ValidationError
@DrPaulSharp DrPaulSharp changed the title Refactors the controls module Refactors the controls module, adding custom errors Nov 1, 2023
@DrPaulSharp DrPaulSharp merged commit 7f2ad53 into RascalSoftware:main Nov 1, 2023
6 checks passed
@DrPaulSharp DrPaulSharp deleted the controls branch November 1, 2023 17:00
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.

3 participants