Skip to content

Commit

Permalink
feat(webUI): handle error while performing commands
Browse files Browse the repository at this point in the history
Refs: #27
  • Loading branch information
nergal-perm committed Jun 14, 2024
1 parent c075c0f commit 39f87b8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
33 changes: 27 additions & 6 deletions src/main/java/ru/ewc/checklogic/server/CommandPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.renomad.minum.web.WebFramework;
import java.util.Map;
import ru.ewc.checklogic.Computation;
import ru.ewc.decisions.api.DecitaException;

/**
* I am a configuration object for all the command-related endpoints.
Expand All @@ -44,7 +45,12 @@ public final class CommandPage implements Endpoints {
/**
* The template processor for the Command page.
*/
private final TemplateProcessor template;
private final TemplateProcessor description;

/**
* The template processor for the error page.
*/
private final TemplateProcessor error;

/**
* Ctor.
Expand All @@ -53,9 +59,12 @@ public final class CommandPage implements Endpoints {
*/
public CommandPage(final Computation computation) {
this.computation = computation;
this.template = TemplateProcessor.buildProcessor(
this.description = TemplateProcessor.buildProcessor(
WebResource.readFileFromResources("templates/command-info.html")
);
this.error = TemplateProcessor.buildProcessor(
WebResource.readFileFromResources("templates/command-error.html")
);
}

@Override
Expand All @@ -65,16 +74,28 @@ public void register(final WebFramework web) {
}

// @todo #25 Pass the filled parameters to the command
// @todo #25 Handle the errors gracefully (show some modal with error description?)
public Response executeCommand(final Request request) {
final String command = request.body().asString("command");
this.computation.perform(command);
return Response.htmlOk("OK", Map.of("HX-Redirect", "/"));
Response response;
try {
this.computation.perform(command);
response = Response.htmlOk("OK", Map.of("HX-Redirect", "/"));
} catch (final DecitaException exception) {
response = Response.htmlOk(
this.error.renderTemplate(
Map.of(
"error", exception.getMessage()
)
)
);
}
return response;
}

// @todo #25 Extract all the required parameters from the command's description
// @todo #25 Implement the check for the command's decision table
public Response commandInfo(final Request request) {
final String command = request.requestLine().queryString().getOrDefault("command", "");
return Response.htmlOk(this.template.renderTemplate(Map.of("command_name", command)));
return Response.htmlOk(this.description.renderTemplate(Map.of("command_name", command)));
}
}
31 changes: 31 additions & 0 deletions src/main/resources/templates/command-error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
~ MIT License
~
~ Copyright (c) 2024 Decision-Driven Development
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all
~ copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
-->
<div id="modal" _="on closeModal add .closing then wait for animationend then remove me">
<div class="modal-underlay" _="on click trigger closeModal"></div>
<div class="modal-content">
<h1>Error</h1>
<p>{{ error }}</p>
<button _="on click trigger closeModal">Close</button>
</div>
</div>
3 changes: 2 additions & 1 deletion src/main/resources/templates/command-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1>{{ command_name }}</h1>
<br>
<br>
<input id="command" type="hidden" name="command" value="{{ command_name }}"/>
<button _="on click trigger closeModal" hx-post="/command" hx-include="#command">Execute</button>
<button hx-post="/command" hx-include="#command"
hx-target="#modal" hx-swap="outerHTML">Execute</button>
</div>
</div>
4 changes: 2 additions & 2 deletions src/main/resources/templates/state.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
~ SOFTWARE.
-->
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title>Simple HTML Page</title>
<script src="https://unpkg.com/htmx.org@1.9.12"
integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.12"></script>
<link rel="stylesheet" href="static/main.css">
</head>
<body>
Expand All @@ -36,6 +37,5 @@ <h1>State entities</h1>
<hr>
<h1>Commands</h1>
{{ commands }}
<button hx-get="/command" hx-target="body" hx-swap="beforeend" hx-vals='{"command":"initialize"}'>Initialize</button>
</body>
</html>

2 comments on commit 39f87b8

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 39f87b8 Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 25-033921a6 disappeared from src/main/java/ru/ewc/checklogic/server/CommandPage.java), that's why I closed #27. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 39f87b8 Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 25-7b38b907 discovered in src/main/java/ru/ewc/checklogic/server/CommandPage.java) and submitted as #29. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.