Skip to content

Commit

Permalink
When polib raises an exception, re-raise it as a ParseError
Browse files Browse the repository at this point in the history
so that:

1. The error message the user gets helps him/her resolve the problem
2. We don't show a 500 error to the user
3. We don't get a report in sentry

Related: https://sentry.int.transifex.com/developers/transifex/issues/25094/
  • Loading branch information
kbairak committed Mar 26, 2018
1 parent 77da560 commit 235d9ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 5 additions & 1 deletion openformats/formats/po.py
Expand Up @@ -30,7 +30,11 @@ def parse(self, content, is_source=False):
stringset = []
self.is_source = is_source
self.order_generator = itertools.count()
po = polib.pofile(content)
try:
po = polib.pofile(content)
except Exception, e:
raise ParseError("Error while validating PO file syntax: {}".
format(e.message))
self.only_values = False
self.only_keys = False
self.new_po = copy.copy(po)
Expand Down
13 changes: 9 additions & 4 deletions openformats/tests/formats/po/test_po.py
@@ -1,12 +1,12 @@
import unittest
import itertools

from openformats.strings import OpenString
from openformats.exceptions import ParseError
from openformats.formats.po import PoHandler
from openformats.strings import OpenString
from openformats.tests.formats.common import CommonFormatTestMixin
from openformats.tests.utils.strings import (
strip_leading_spaces, generate_random_string
)
from openformats.tests.utils.strings import (strip_leading_spaces,
generate_random_string)


class PoTestCase(CommonFormatTestMixin, unittest.TestCase):
Expand Down Expand Up @@ -462,3 +462,8 @@ def test_incomplete_plurals(self):
u"Incomplete plural forms found on the entry with msgid `p1` "
u"and msgid_plural `p2`."
)

def test_invalid_po_raised_as_parseerror(self):
source = "blyargh"
with self.assertRaises(ParseError):
self.handler.parse(source)

0 comments on commit 235d9ee

Please sign in to comment.