GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Error page (dev mode): display filename & partial content (#133) · voidframework/voidframework@365d7b0

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • voidframework-web/src/main/java/dev/voidframework/web/http/errorhandler
    • DefaultErrorHandler.java
    • errorpage
      • DevMode500InternalServerError.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎voidframework-web/src/main/java/dev/voidframework/web/http/errorhandler/DefaultErrorHandler.java‎

Copy file name to clipboardExpand all lines: voidframework-web/src/main/java/dev/voidframework/web/http/errorhandler/DefaultErrorHandler.java
+44-6Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public Result onServerError(final Context context, final Throwable throwable) {
9494

9595
final String subHeaderError;
9696
final int lineNumberFromZero;
97-
final List<FileLine> fileLineList;
97+
final PartialFileContent partialFileContent;
9898
if (throwable.getClass() == TemplateException.RenderingFailure.class) {
9999
// Template rendering error
100100
final TemplateException.RenderingFailure renderingFailure = (TemplateException.RenderingFailure) throwable;
@@ -104,7 +104,11 @@ public Result onServerError(final Context context, final Throwable throwable) {
104104
subHeaderError = javaFilepathOptional.map(Path::toString).orElse(renderingFailure.getTemplateName())
105105
+ StringConstants.COLON
106106
+ lineNumberFromZero;
107-
fileLineList = javaFilepathOptional.map(path -> retrievePartialFileContent(path, lineNumberFromZero)).orElseGet(ArrayList::new);
107+
108+
final List<FileLine> fileLineList = javaFilepathOptional
109+
.map(path -> retrievePartialFileContent(path, lineNumberFromZero))
110+
.orElseGet(ArrayList::new);
111+
partialFileContent = new PartialFileContent(renderingFailure.getTemplateName(), fileLineList);
108112

109113
if (cause.getCause() != null) {
110114
cause = cause.getCause();
@@ -115,22 +119,46 @@ public Result onServerError(final Context context, final Throwable throwable) {
115119
subHeaderError = stackTraceElement.toString();
116120
lineNumberFromZero = stackTraceElement.getLineNumber() - 1;
117121

118-
final String javaFileName = stackTraceElement.getClassName().replace(StringConstants.DOT, File.separator).split("\\$", 2)[0] + ".java";
119-
final Optional<Path> javaFilepathOptional = resolvePossibleJavaFileLocation(javaFileName);
120-
fileLineList = javaFilepathOptional.map(path -> retrievePartialFileContent(path, lineNumberFromZero)).orElseGet(ArrayList::new);
122+
partialFileContent = extractPartialContentFromFirstAccessibleJavaFile(cause);
121123
}
122124

123125
return Result.internalServerError(
124126
DevMode500InternalServerError.render(
125127
cause.getMessage() != null ? cause.getMessage() : "Oops, something goes wrong",
126128
subHeaderError,
127129
lineNumberFromZero,
128-
fileLineList));
130+
partialFileContent));
129131
}
130132

131133
return Result.internalServerError("500 Internal Server Error");
132134
}
133135

136+
/**
137+
* Extract partial content from the first accessible Java file retrieved from the throwable.
138+
*
139+
* @param throwable The throwable to use
140+
* @return Extracted partial file content
141+
* @since 1.12.0
142+
*/
143+
private PartialFileContent extractPartialContentFromFirstAccessibleJavaFile(final Throwable throwable) {
144+
145+
for (final StackTraceElement stackTraceElement : throwable.getStackTrace()) {
146+
final String javaFileName = stackTraceElement.getClassName()
147+
.replace(StringConstants.DOT, File.separator)
148+
.split("\\$", 2)[0] + ".java";
149+
150+
final Optional<Path> pathOptional = resolvePossibleJavaFileLocation(javaFileName);
151+
if (pathOptional.isPresent()) {
152+
final List<FileLine> fileLineList = retrievePartialFileContent(
153+
pathOptional.get(),
154+
stackTraceElement.getLineNumber() - 1);
155+
return new PartialFileContent(javaFileName, fileLineList);
156+
}
157+
}
158+
159+
return null;
160+
}
161+
134162
/**
135163
* Resolve possible Java file location.
136164
*
@@ -216,6 +244,16 @@ private List<FileLine> retrievePartialFileContent(final Path javaFilePath,
216244
return fileLineList;
217245
}
218246

247+
/**
248+
* Represents a partial file content.
249+
*
250+
* @param filename The filename
251+
* @param fileLineList The lines
252+
* @since 1.12.0
253+
*/
254+
public record PartialFileContent(String filename, List<FileLine> fileLineList) {
255+
}
256+
219257
/**
220258
* Represents a single content line.
221259
*
Collapse file

‎voidframework-web/src/main/java/dev/voidframework/web/http/errorhandler/errorpage/DevMode500InternalServerError.java‎

Copy file name to clipboardExpand all lines: voidframework-web/src/main/java/dev/voidframework/web/http/errorhandler/errorpage/DevMode500InternalServerError.java
+19-15Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package dev.voidframework.web.http.errorhandler.errorpage;
22

3+
import dev.voidframework.core.constant.StringConstants;
34
import dev.voidframework.web.http.errorhandler.DefaultErrorHandler;
45

5-
import java.util.List;
6-
76
/**
87
* Generates a "500 Internal Server Error" page when development mode is enabled. The
98
* template engine is not used, as it is not guaranteed to be available or compatible.
@@ -79,7 +78,7 @@ public final class DevMode500InternalServerError {
7978
<code>At %s</code>
8079
</div>
8180
<div class="description">
82-
<h2></h2>
81+
<h2>%s</h2>
8382
<table>
8483
%s
8584
</table>
@@ -110,26 +109,31 @@ private DevMode500InternalServerError() {
110109
/**
111110
* Render template.
112111
*
113-
* @param errorMessage The error message
114-
* @param errorLocation Where the error occur
115-
* @param errorLineNumber Line number (from zero) where is located the error
116-
* @param fileLineList Partial file line content
112+
* @param errorMessage The error message
113+
* @param errorLocation Where the error occur
114+
* @param errorLineNumber Line number (from zero) where is located the error
115+
* @param partialFileContent Partial file content
117116
* @return The rendered template
118-
* @since 1.0.0
117+
* @since 1.12.0
119118
*/
120119
public static String render(final String errorMessage,
121120
final String errorLocation,
122121
final int errorLineNumber,
123-
final List<DefaultErrorHandler.FileLine> fileLineList) {
122+
final DefaultErrorHandler.PartialFileContent partialFileContent) {
123+
124+
String partialContentFileName = StringConstants.EMPTY;
124125

125126
final StringBuilder fileSnippedBuilder = new StringBuilder();
126-
for (final DefaultErrorHandler.FileLine fileLine : fileLineList) {
127-
fileSnippedBuilder.append(FILE_PATTERN_ENTRY.formatted(
128-
errorLineNumber == fileLine.number() ? CLASS_ERROR : "",
129-
fileLine.number(),
130-
fileLine.content().replace("<", "&lt;")));
127+
if (partialFileContent != null && partialFileContent.fileLineList() != null) {
128+
partialContentFileName = partialFileContent.filename();
129+
for (final DefaultErrorHandler.FileLine fileLine : partialFileContent.fileLineList()) {
130+
fileSnippedBuilder.append(FILE_PATTERN_ENTRY.formatted(
131+
errorLineNumber == fileLine.number() ? CLASS_ERROR : "",
132+
fileLine.number(),
133+
fileLine.content().replace("<", "&lt;")));
134+
}
131135
}
132136

133-
return CONTENT.formatted(errorMessage, errorLocation, fileSnippedBuilder.toString());
137+
return CONTENT.formatted(errorMessage, errorLocation, partialContentFileName, fileSnippedBuilder.toString());
134138
}
135139
}

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.