Skip to content

Commit

Permalink
Merge pull request #745 from voussoir/praw-nosectionerror
Browse files Browse the repository at this point in the history
Add help message for NoSectionError
  • Loading branch information
bboe committed Mar 6, 2017
2 parents e142a50 + 8f87323 commit ebcf32e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
20 changes: 17 additions & 3 deletions praw/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .exceptions import ClientException
from .config import Config
from .const import __version__, API_PATH, USER_AGENT_FORMAT
from .const import __version__, API_PATH, USER_AGENT_FORMAT, configparser
from .objector import Objector
from . import models

Expand Down Expand Up @@ -99,8 +99,22 @@ def __init__(self, site_name=None, **config_settings):
self._core = self._authorized_core = self._read_only_core = None
self._objector = None
self._unique_counter = 0
self.config = Config(site_name or os.getenv('praw_site') or 'DEFAULT',
**config_settings)

try:
config_section = site_name or os.getenv('praw_site') or 'DEFAULT'
self.config = Config(config_section, **config_settings)
except configparser.NoSectionError as exc:
help_message = ('You provided the name of a praw.ini '
'configuration which does not exist.\n\nFor help '
'with creating a Reddit instance, visit\n'
'https://praw.readthedocs.io/en/latest/code_overvi'
'ew/reddit_instance.html\n\n'
'For help on configuring PRAW, visit\n'
'https://praw.readthedocs.io/en/latest/getting_sta'
'rted/configuration.html')
if site_name is not None:
exc.message += '\n' + help_message
raise

required_message = ('Required configuration setting {!r} missing. \n'
'This setting can be provided in a praw.ini file, '
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/test_reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mock
import pytest
from praw import __version__, Reddit
from praw.const import configparser
from praw.exceptions import ClientException

from . import UnitTest
Expand Down Expand Up @@ -117,6 +118,11 @@ def test_reddit__required_settings_set_to_none(self):
'setting \'{}\' missing.'
.format(setting))

def test_reddit__site_name_no_section(self):
with pytest.raises(configparser.NoSectionError) as excinfo:
Reddit('bad_site_name')
assert 'praw.readthedocs.io' in excinfo.value.message

def test_submission(self):
assert self.reddit.submission('2gmzqe').id == '2gmzqe'

Expand Down

0 comments on commit ebcf32e

Please sign in to comment.