From 43d0ceafb1102562e72c17bba7585d658c4131f6 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 19 Apr 2023 15:57:53 -0700 Subject: [PATCH] Improve code flow with `enforceHTTP` --- source/summit/setup.d | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/source/summit/setup.d b/source/summit/setup.d index 5b742e1..0d7e46e 100644 --- a/source/summit/setup.d +++ b/source/summit/setup.d @@ -54,12 +54,8 @@ public final class SetupApplication : Application void setupIndex(FormError _error = FormError.init, string instanceURI = null, string description = null, string username = null, string emailAddress = null) @safe { - /* Throw internal error if not from field validation */ - if (!_error.error.empty && _error.field.empty) - { - throw new HTTPStatusException(HTTPStatus.internalServerError, _error.error); - - } + /* Throw internal error if not a field validation error */ + enforceHTTP(_error.isValid, HTTPStatus.internalServerError, _error.error); render!("setup/index.dt", _error, instanceURI, description, username, emailAddress); } @@ -128,4 +124,14 @@ private struct FormError { string error; string field; + + /** + * Returns true if the error is empty or a field validation error. + * + * Valid errors are safe to forward to a Diet template. + */ + pure @property bool isValid() @safe + { + return error.empty || !field.empty; + } }