Created
September 1, 2022 14:09
-
-
Save martin-martin/cb7d7ad10ea42956d110d23309d18db8 to your computer and use it in GitHub Desktop.
Potential code for answering the question (errors bubble up)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How can I make sure that the exception messages | |
# of Exceptions that occur in the sub-function `load_model()` | |
# show up in the stack trace of the `main()` function? | |
class UnsupportedInputError(Exception): pass | |
class SourceEqualsTargetError(Exception): pass | |
supported_inputs = ["a", "b", ...] | |
def load_model(source, target): | |
if source not in supported_inputs or target not in supported_inputs: | |
raise UnsupportedInputError("not supported pair") | |
elif source == target: | |
raise SourceEqualsTargetError("source and target shouldn't be same") | |
else: # Load the model | |
print("Loading model...") | |
model = "loaded model" # Replace with code logic | |
return model | |
def main(user_input): | |
model = load_model(user_input["source"], user_input["target"]) | |
# try: | |
# model = load_model(user_input["source"], user_input["target"]) | |
# except (UnsupportedInputError, SourceEqualsTargetError) as e: | |
# print(e) | |
# # return sys.exit(0) # Do you want to quit when theres an error? | |
# else: # Do something with the model | |
# print(model) | |
print("Run #1") | |
valid_input = {"source": "a", "target": "b"} | |
main(valid_input) # Should raise no error | |
print("Run #2") | |
invalid_input_1 = {"source": "a", "target": "a"} | |
main(invalid_input_1) # Should raise SourceEqualsTargetError | |
print("Run #3") | |
invalid_input_2 = {"source": "a", "target": "unsupported"} | |
main(invalid_input_2) # Should raise UnsupportedInputError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With an intermediate state to answer the question that looked like this: