Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit b89693b

Browse files
[DEVEDSB-98] Add custom error view and dotenv configuration
1 parent 6a8c1cc commit b89693b

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

.github/workflows/maven.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
3+
4+
name: Java CI with Maven
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ${{ matrix.platform }}
16+
strategy:
17+
max-parallel: 3
18+
matrix:
19+
platform: [windows-latest, macos-latest, ubuntu-latest]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up JDK 11
24+
uses: actions/setup-java@v2
25+
with:
26+
java-version: '11'
27+
distribution: 'adopt'
28+
cache: maven
29+
- name: Build with Maven
30+
run: mvn package
31+
- name: Run tests
32+
run: mvn clean test

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
<artifactId>dotenv-java</artifactId>
4040
<version>2.2.0</version>
4141
</dependency>
42+
<dependency>
43+
<groupId>com.sparkjava</groupId>
44+
<artifactId>spark-template-thymeleaf</artifactId>
45+
<version>2.7.1</version>
46+
</dependency>
4247
</dependencies>
4348

4449
<build>

src/main/java/com/twilio/Webapp.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
import static spark.Spark.internalServerError;
1414
import static spark.Spark.notFound;
1515
import io.github.cdimascio.dotenv.Dotenv;
16+
import spark.ModelAndView;
17+
import spark.template.thymeleaf.ThymeleafTemplateEngine;
18+
import java.util.HashMap;
19+
import java.util.Map;
1620

1721
public class Webapp {
1822
private static Dotenv env = Dotenv.configure().ignoreIfMissing().load();
@@ -23,9 +27,20 @@ public static void main(String[] args) throws Exception {
2327

2428
// Log all requests and responses
2529
afterAfter(new LoggingFilter());
26-
// Using string/html to handle errors
27-
internalServerError("<html><body><h1>Something went wrong!</h1><h2>Our Engineers are on it</h2><a href='/'>Go Home</a></body></html>");
28-
notFound("<html><body><h1>Page not found</h1><a href='/'>Go Home</a></body></html>");
30+
31+
// Handle errors
32+
internalServerError((request, response) -> {
33+
Map<String, Object> model = new HashMap<String, Object>();
34+
return new ThymeleafTemplateEngine().render(
35+
new ModelAndView(model, "error")
36+
);
37+
});
38+
notFound((request, response) -> {
39+
Map<String, Object> model = new HashMap<String, Object>();
40+
return new ThymeleafTemplateEngine().render(
41+
new ModelAndView(model, "error")
42+
);
43+
});
2944

3045
// Create an access token using our Twilio credentials
3146
get("/", (request, response) -> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<body>
5+
<h1>Something went wrong! </h1>
6+
<h2>Our Engineers are on it</h2>
7+
<a href="/">Go Home</a>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)