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

Error handling in break_time #2074

Closed
wants to merge 3 commits into from
Closed

Error handling in break_time #2074

wants to merge 3 commits into from

Conversation

swapsha96
Copy link
Contributor

@swapsha96 swapsha96 commented Apr 12, 2017

Following code returns ValueError and is not handled properly:
from sunpy.time import break_time
break_time('garbage')

Following result is obtained on the terminal:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sunpy/time/time.py", line 283, in break_time
    return parse_time(t, time_format).strftime("%Y%m%d_%H%M%S")
  File "sunpy/time/time.py", line 199, in parse_time
    raise ValueError("{tstr!s} is not a valid time string!".format(tstr=time_string))
ValueError: garbage is not a valid time string!

This PR uses try-catch to handle the error input in break_time() function. pass is used as it is used in line 186 of same file.

@pep8speaks
Copy link

pep8speaks commented Apr 12, 2017

Hello @swapsha96! Thanks for updating the PR.

Line 121:1: E302 expected 2 blank lines, found 1

Comment last updated on April 12, 2017 at 11:33 Hours UTC

@swapsha96
Copy link
Contributor Author

Thank you @pep8speaks ! I removed unnecessary blank lines. Is it now good to go?

@wafels
Copy link
Member

wafels commented Apr 12, 2017 via email

@swapsha96
Copy link
Contributor Author

@wafels Generally, error handling using try-catch from user side is discouraged. User is supposed handle value returned from a function. Errors like ValueError, NameError, SyntaxError can lead to application crash and thus, function must return appropriate error and user can handle it using if-else condition.

@wafels
Copy link
Member

wafels commented Apr 12, 2017 via email

@swapsha96
Copy link
Contributor Author

@wafels Is this PR alright to be merged? I am ready to find and fix such errors.

@wafels
Copy link
Member

wafels commented Apr 12, 2017 via email

@dpshelio
Copy link
Member

I don't agree to fail quietly and return None if the user is not using the function correctly.

@ayshih
Copy link
Member

ayshih commented May 1, 2017

I don't like this PR. Not raising exceptions means that:

  • Downstream code can't tell the difference between None as a legitimate return value and None as an error value.
  • Downstream code can't tell if a error condition occurred.
  • If an error condition is detected by None as a return value, which requires a guarantee that None is not a legitimate return value, downstream code can't tell the difference between different types of error conditions.

Also, returning None is possibly worse than raising an exception because downstream code that doesn't check specifically for None may cause unintended behavior with such return. For example, in this case, imagine someone tried to generate a filename using a None return value. Raising an exception gives direct visibility to the source of the problem.

If this is in fact will be the preferred coding style, presumably we should change parse_time to not raise exceptions in the first place.

@ayshih
Copy link
Member

ayshih commented May 1, 2017

As a side note, can someone remind me why this function is called break_time? It seems like a bizarre, non-intuitive name.

@swapsha96 swapsha96 closed this May 4, 2017
@swapsha96 swapsha96 deleted the breaktime branch May 4, 2017 17:07
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.

None yet

5 participants