Skip to content

Commit 758ca6e

Browse files
authored
Merge pull request pypa#248 from pypa/bug/228
Rework handling and display of exceptions
2 parents c324237 + 903b448 commit 758ca6e

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

tests/test_main.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
from twine import __main__ as dunder_main
14+
15+
import pretend
16+
17+
18+
def test_exception_handling(monkeypatch):
19+
replaced_dispatch = pretend.raiser(KeyError('foo'))
20+
monkeypatch.setattr(dunder_main, 'dispatch', replaced_dispatch)
21+
assert dunder_main.main() == 'KeyError: foo'

twine/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121

2222

2323
def main():
24-
return dispatch(sys.argv[1:])
24+
try:
25+
return dispatch(sys.argv[1:])
26+
except Exception as exc:
27+
return '{0}: {1}'.format(
28+
exc.__class__.__name__,
29+
exc.args[0],
30+
)
2531

2632

2733
if __name__ == "__main__":

twine/commands/register.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import argparse
1717
import os.path
18-
import sys
1918

2019
from twine import exceptions as exc
2120
from twine.package import PackageFile
@@ -137,7 +136,4 @@ def main(args):
137136
args = parser.parse_args(args)
138137

139138
# Call the register function with the args from the command line
140-
try:
141-
register(**vars(args))
142-
except Exception as exc:
143-
sys.exit("{exc.__class__.__name__}: {exc}".format(exc=exc))
139+
register(**vars(args))

twine/commands/upload.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,7 @@ def main(args):
251251
args = parser.parse_args(args)
252252

253253
# Call the upload function with the arguments from the command line
254-
try:
255-
upload(**vars(args))
256-
except Exception as exc:
257-
sys.exit("{exc.__class__.__name__}: {exc}".format(exc=exc))
254+
upload(**vars(args))
258255

259256

260257
if __name__ == "__main__":

0 commit comments

Comments
 (0)