mirror of
https://github.com/inventree/InvenTree.git
synced 2026-08-16 02:06:29 +00:00
Improve error handling for unit registry (#12643)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"""Custom field validators for InvenTree."""
|
||||
|
||||
import tokenize
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
@@ -9,6 +11,7 @@ import pint.errors
|
||||
from moneyed import CURRENCIES
|
||||
|
||||
import InvenTree.conversion
|
||||
import InvenTree.exceptions
|
||||
from common.settings import get_global_setting
|
||||
|
||||
|
||||
@@ -24,7 +27,17 @@ def validate_physical_units(unit):
|
||||
|
||||
try:
|
||||
ureg(unit)
|
||||
except (AssertionError, AttributeError, pint.errors.UndefinedUnitError):
|
||||
except (
|
||||
AssertionError,
|
||||
AttributeError,
|
||||
pint.errors.UndefinedUnitError,
|
||||
tokenize.TokenError,
|
||||
):
|
||||
raise ValidationError(_('Invalid physical unit'))
|
||||
except Exception:
|
||||
# Pint parses unit expressions via the python tokenizer, so any
|
||||
# other unexpected exception type may be raised for malformed input
|
||||
InvenTree.exceptions.log_error('validate_physical_units', scope='validators')
|
||||
raise ValidationError(_('Invalid physical unit'))
|
||||
|
||||
|
||||
|
||||
@@ -252,7 +252,7 @@ class ParameterTests(TestCase):
|
||||
tmp.full_clean()
|
||||
|
||||
# Test that invalid units fail
|
||||
for unit in ['mmmmm', '-', 'x', int]:
|
||||
for unit in ['mmmmm', '-', 'x', int, "piao's"]:
|
||||
tmp = ParameterTemplate(name='test', units=unit)
|
||||
with self.assertRaises(django_exceptions.ValidationError):
|
||||
tmp.full_clean()
|
||||
|
||||
Reference in New Issue
Block a user