Skip to content

Commit

Permalink
Use standard Exception classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dyve committed May 3, 2021
1 parent 7df8c27 commit 2e800ae
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 29 deletions.
5 changes: 2 additions & 3 deletions src/django_bootstrap5/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.utils.translation import gettext as _

from .css import merge_css_classes
from .exceptions import BootstrapError
from .html import render_tag
from .size import DEFAULT_SIZE, SIZE_MD, get_size_class

Expand Down Expand Up @@ -46,7 +45,7 @@ def render_button(

if button_type:
if button_type not in ("submit", "reset", "button", "link"):
raise BootstrapError(
raise ValueError(
(
'Parameter "button_type" should be "submit", "reset", "button", "link" or empty '
f'("{button_type}" given).'
Expand All @@ -57,7 +56,7 @@ def render_button(

if href:
if button_type and button_type != "link":
raise BootstrapError(f'Button of type "{button_type}" is not allowed a "href" parameter.')
raise ValueError(f'Button of type "{button_type}" is not allowed a "href" parameter.')
tag = "a"
attrs["href"] = href
attrs.setdefault("role", "button")
Expand Down
6 changes: 0 additions & 6 deletions src/django_bootstrap5/exceptions.py

This file was deleted.

9 changes: 4 additions & 5 deletions src/django_bootstrap5/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from .core import get_bootstrap_setting
from .css import merge_css_classes
from .exceptions import BootstrapError
from .forms import WRAPPER_CLASS, WRAPPER_TAG, render_field, render_form, render_label
from .size import DEFAULT_SIZE, SIZE_MD, SIZE_XS, get_size_class, parse_size
from .text import text_value
Expand Down Expand Up @@ -72,7 +71,7 @@ def parse_size(self, size):
"""Return size if it is valid, default size if size is empty, or throws exception."""
size = parse_size(size, default=DEFAULT_SIZE)
if size == SIZE_XS:
raise BootstrapError('Size "xs" is not valid for form controls.')
raise ValueError('Size "xs" is not valid for form controls.')
return size

def get_size_class(self, prefix):
Expand Down Expand Up @@ -117,7 +116,7 @@ class FormsetRenderer(BaseRenderer):

def __init__(self, formset, *args, **kwargs):
if not isinstance(formset, BaseFormSet):
raise BootstrapError('Parameter "formset" should contain a valid Django Formset.')
raise ValueError('Parameter "formset" should contain a valid Django Formset.')
self.formset = formset
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -162,7 +161,7 @@ class FormRenderer(BaseRenderer):

def __init__(self, form, *args, **kwargs):
if not isinstance(form, BaseForm):
raise BootstrapError('Parameter "form" should contain a valid Django Form.')
raise ValueError('Parameter "form" should contain a valid Django Form.')
self.form = form
super().__init__(*args, **kwargs)

Expand Down Expand Up @@ -213,7 +212,7 @@ class FieldRenderer(BaseRenderer):

def __init__(self, field, *args, **kwargs):
if not isinstance(field, BoundField):
raise BootstrapError('Parameter "field" should contain a valid Django BoundField.')
raise ValueError('Parameter "field" should contain a valid Django BoundField.')
self.field = field
super().__init__(*args, **kwargs)

Expand Down
7 changes: 3 additions & 4 deletions src/django_bootstrap5/size.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django_bootstrap5.css import merge_css_classes
from django_bootstrap5.exceptions import BootstrapError
from django_bootstrap5.text import text_value
from .css import merge_css_classes
from .text import text_value

SIZE_XS = "xs"
SIZE_SM = "sm"
Expand All @@ -15,7 +14,7 @@ def parse_size(value, default=None):
size = text_value(value or default)
if size not in SIZES:
valid_sizes = ", ".join(SIZES)
raise BootstrapError(f'Invalid value "{size}" for parameter "size" (valid values are {valid_sizes}).')
raise ValueError(f'Invalid value "{size}" for parameter "size" (valid values are {valid_sizes}).')
return size


Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_button.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand Down Expand Up @@ -65,5 +64,5 @@ def test_button_type_link(self):
link_button,
)

with self.assertRaises(BootstrapError):
with self.assertRaises(ValueError):
self.render("{% bootstrap_button 'button' button_type='button' href='#' %}")
4 changes: 1 addition & 3 deletions tests/test_bootstrap_field.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django import forms

from django_bootstrap5.exceptions import BootstrapError

from .base import BootstrapTestCase, html_39x27


Expand All @@ -20,7 +18,7 @@ class SubjectTestForm(forms.Form):

class FieldTestCase(BootstrapTestCase):
def test_illegal_field(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(ValueError):
self.render("{% bootstrap_field field %}", {"field": "illegal"})

def test_show_help(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django import forms
from django.forms import formset_factory

from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand All @@ -25,7 +24,7 @@ def clean(self):

class BootstrapFormTestCase(BootstrapTestCase):
def test_illegal_form(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(ValueError):
self.render("{% bootstrap_form form %}", {"form": "illegal"})

def test_exclude(self):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_bootstrap_formset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django import forms

from django_bootstrap5.exceptions import BootstrapError
from tests.base import BootstrapTestCase


Expand Down Expand Up @@ -51,5 +50,5 @@ def test_formset_post(self):
)

def test_illegal_formset(self):
with self.assertRaises(BootstrapError):
with self.assertRaises(ValueError):
self.render("{% bootstrap_formset formset %}", {"formset": "illegal"})
3 changes: 1 addition & 2 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.utils.safestring import mark_safe

from django_bootstrap5.components import render_alert, render_button
from django_bootstrap5.exceptions import BootstrapError


class AlertsTestCase(TestCase):
Expand Down Expand Up @@ -60,7 +59,7 @@ def test_button_with_illegal_type(self):
self.assertEqual(
render_button("button", button_type="illegal"), '<button class="btn btn-primary">button</button>'
)
except BootstrapError as e:
except ValueError as e:
self.assertEqual(
str(e),
'Parameter "button_type" should be "submit", "reset", "button", "link" or empty ("illegal" given).',
Expand Down

0 comments on commit 2e800ae

Please sign in to comment.