Skip to content

feat: throw exceptions from validators#77

Merged
ericdeansanchez merged 4 commits into
masterfrom
exceptional-validation
Jun 3, 2020
Merged

feat: throw exceptions from validators#77
ericdeansanchez merged 4 commits into
masterfrom
exceptional-validation

Conversation

@ericdeansanchez
Copy link
Copy Markdown
Contributor

The purpose of this PR is to complete our validation implementation.
The two exceptions used throughout this PR are:

  • WidthRangeError: for invalid start or stop width values
  • WidthToleranceError: for invalid width tolerance values.

An example of the output when an exception is thrown is:

from imgix import UrlBuilder
u = UrlBuilder("a.b.c", include_library_param=False)
u.create_srcset('image.png', start=1, stop=0)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "imgix-python/imgix/urlbuilder.py", line 191, in create_srcset
    validate_min_max_tol(start, stop, tol)
  File "imgix-python/imgix/validators.py", line 153, in validate_min_max_tol
    validate_range(min_width, max_width)
  File "imgix-python/imgix/validators.py", line 105, in validate_range
    validate_max_width(max_width)
  File "imgix-python/imgix/validators.py", line 76, in validate_max_width
    raise WidthRangeError(invalid_max_error)
imgix.errors.WidthRangeError: `stop` width value must be > 0 && <= `constants.IMAGE_MAX_WIDTH`

W.r.t. the above, both start and stop are individually validated; once
they pass their individual validation steps, the range is checked (ie. that
start <= stop. This is why the above throws a WidthRangeError instead
of a WidthToleranceError.

u.create_srcset('image.png', start=100, stop=200, tol=0.001)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "imgix-python/imgix/urlbuilder.py", line 191, in create_srcset
    validate_min_max_tol(start, stop, tol)
  File "imgix-python/imgix/validators.py", line 154, in validate_min_max_tol
    validate_width_tol(tol)
  File "imgix-python/imgix/validators.py", line 133, in validate_width_tol
    raise WidthToleranceError(invalid_tol_error)
imgix.errors.WidthToleranceError: `tol`erance value must be >= 0.01

In completing this validation, lines 184-189 of imgix/urlbuilder.py have
been revised to account for 0 being considered falsy.

@ericdeansanchez ericdeansanchez merged commit 8334ddb into master Jun 3, 2020
@ericdeansanchez ericdeansanchez deleted the exceptional-validation branch June 3, 2020 15:04
@ericdeansanchez ericdeansanchez changed the title refactor: throw exceptions from validators feat: throw exceptions from validators Jun 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants