Skip to content

Commit 29e6c16

Browse files
committed
Allow customzing css classes for the ErrorsViewStackTracePrinter
1 parent ab9fdf3 commit 29e6c16

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

grails-bootstrap/src/main/groovy/org/grails/exceptions/reporting/DefaultStackTracePrinter.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.codehaus.groovy.control.MultipleCompilationErrorsException
2323
*/
2424
class DefaultStackTracePrinter implements StackTracePrinter {
2525

26-
String prettyPrint(Throwable t) {
26+
String prettyPrint(Throwable t, Map attrs = [:]) {
2727
if (t == null) return ''
2828
if (!t.stackTrace) return 'No stack trace available'
2929
final sw = new StringWriter()

grails-core/src/main/groovy/org/grails/core/exceptions/DefaultErrorsPrinter.groovy

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class DefaultErrorsPrinter extends DefaultStackTracePrinter implements CodeSnipp
4747
res == null ? te.className : res.getFilename()
4848
}
4949

50-
String prettyPrintCodeSnippet(Throwable exception) {
50+
String prettyPrintCodeSnippet(Throwable exception, Map attrs = [:]) {
5151
if (exception == null) {
5252
return ''
5353
}
@@ -86,7 +86,7 @@ class DefaultErrorsPrinter extends DefaultStackTracePrinter implements CodeSnipp
8686
if (lineNumbersShown[res.filename].contains(lineNumber)) continue // don't repeat the same lines twice
8787

8888
lineNumbersShown[res.filename] << lineNumber
89-
pw.print formatCodeSnippetStart(res, lineNumber)
89+
pw.print formatCodeSnippetStart(res, lineNumber, attrs)
9090
def input = null
9191
try {
9292
input = res.inputStream
@@ -101,10 +101,10 @@ class DefaultErrorsPrinter extends DefaultStackTracePrinter implements CodeSnipp
101101
if (currentLineNumber in range) {
102102
boolean isErrorLine = currentLineNumber == lineNumber
103103
if (isErrorLine) {
104-
pw.print formatCodeSnippetErrorLine(currentLineNumber, currentLine)
104+
pw.print formatCodeSnippetErrorLine(currentLineNumber, currentLine, attrs)
105105
}
106106
else {
107-
pw.print formatCodeSnippetLine(currentLineNumber, currentLine)
107+
pw.print formatCodeSnippetLine(currentLineNumber, currentLine, attrs)
108108
}
109109
}
110110
else if (currentLineNumber > last) {
@@ -198,18 +198,18 @@ class DefaultErrorsPrinter extends DefaultStackTracePrinter implements CodeSnipp
198198
""
199199
}
200200

201-
String formatCodeSnippetStart(Resource resource, int lineNumber) {
201+
String formatCodeSnippetStart(Resource resource, int lineNumber, Map attrs = [:]) {
202202
"""Around line $lineNumber of $resource.filename
203203
"""
204204

205205
}
206206

207-
protected String formatCodeSnippetLine(int currentLineNumber, currentLine) {
207+
protected String formatCodeSnippetLine(int currentLineNumber, currentLine, Map attrs = [:]) {
208208
"""${currentLineNumber}: ${currentLine}
209209
"""
210210
}
211211

212-
protected String formatCodeSnippetErrorLine(int currentLineNumber, currentLine) {
212+
protected String formatCodeSnippetErrorLine(int currentLineNumber, currentLine, Map attrs = [:]) {
213213
"""${currentLineNumber}: ${currentLine}
214214
"""
215215
}

grails-web-common/src/main/groovy/org/grails/web/errors/ErrorsViewStackTracePrinter.groovy

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,23 @@ class ErrorsViewStackTracePrinter extends DefaultErrorsPrinter {
3838
}
3939

4040
@Override
41-
String prettyPrint(Throwable t) {
41+
String prettyPrint(Throwable t, Map attrs = [:]) {
4242
if (t instanceof GrailsWrappedRuntimeException) {
4343
t = t.cause
4444
}
45-
return super.prettyPrint(t)
45+
return super.prettyPrint(t, attrs)
4646
}
4747

4848
@Override
49-
String prettyPrintCodeSnippet(Throwable t) {
49+
String prettyPrintCodeSnippet(Throwable t, Map attrs = [:]) {
5050
if (t instanceof GrailsWrappedRuntimeException) {
5151
t = t.cause
5252
}
53-
return super.prettyPrintCodeSnippet(t)
53+
return super.prettyPrintCodeSnippet(t, attrs)
5454
}
5555

5656
@Override
57-
String formatCodeSnippetStart(Resource resource, int lineNumber) {
57+
String formatCodeSnippetStart(Resource resource, int lineNumber, Map attrs = [:]) {
5858
String path = resource.filename
5959
// try calc better path
6060
try {
@@ -67,8 +67,8 @@ class ErrorsViewStackTracePrinter extends DefaultErrorsPrinter {
6767
path = resource.filename
6868
}
6969
"""\
70-
<h2>Around line ${lineNumber} of <span class="filename">${path}</span></h2>
71-
<pre class="snippet">"""
70+
<h2>Around line ${lineNumber} of <span class="${attrs['filenameClass'] ?: 'filename'}">${path}</span></h2>
71+
<pre class="${attrs['snippetClass'] ?: 'snippet'}">"""
7272
}
7373

7474
@Override
@@ -77,13 +77,13 @@ class ErrorsViewStackTracePrinter extends DefaultErrorsPrinter {
7777
}
7878

7979
@Override
80-
protected String formatCodeSnippetLine(int currentLineNumber, Object currentLine) {
81-
"""<code class="line"><span class="lineNumber">${currentLineNumber}:</span>${currentLine.encodeAsHTML()}</code>"""
80+
protected String formatCodeSnippetLine(int currentLineNumber, Object currentLine, Map attrs = [:]) {
81+
"""<code class="${attrs['lineClass'] ?: 'line'}"><span class="${attrs['lineNumberClass'] ?: 'lineNumber'}">${currentLineNumber}:</span>${currentLine.encodeAsHTML()}</code>"""
8282
}
8383

8484
@Override
85-
protected String formatCodeSnippetErrorLine(int currentLineNumber, Object currentLine) {
86-
"""<code class="line error"><span class="lineNumber">${currentLineNumber}:</span>${currentLine.encodeAsHTML()}</code>"""
85+
protected String formatCodeSnippetErrorLine(int currentLineNumber, Object currentLine, Map attrs = [:]) {
86+
"""<code class="${attrs['lineErrorClass'] ?: 'line error'}"><span class="${attrs['lineNumberClass'] ?: 'lineNumber'}">${currentLineNumber}:</span>${currentLine.encodeAsHTML()}</code>"""
8787
}
8888

8989
@Override

0 commit comments

Comments
 (0)