Skip to content

Commit

Permalink
Add support for Java stack traces (#1520)
Browse files Browse the repository at this point in the history
There are 3 main types of elements which are highlighted:

1. The summary.
    This is the head of the stack trace and includes the name of the exception and a message.
    `Exception in thread "main" java.lang.RuntimeException: A test exception`
2. Stack frames.
    These make up the actual stack trace and include the full name of the method and the source.
    `at com.stackify.stacktrace.StackTraceExample.methodB(StackTraceExample.java:13)`
3. Omitted frames.
    This is a simple message that some frames were omitted.
    `... 27 more`

Everything besides these 3 element types will not be highlighted.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Dec 2, 2018
1 parent a69c2b6 commit 4a8219a
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@
"require": "clike",
"owner": "sherblot"
},
"javastacktrace": {
"title": "Java stack trace",
"owner": "RunDevelopment"
},
"jolie": {
"title": "Jolie",
"require": "clike",
Expand Down
93 changes: 93 additions & 0 deletions components/prism-javastacktrace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Prism.languages.javastacktrace = {

// java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...]
// Caused by: java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...]
// Caused by: com.example.myproject.MyProjectServletException
// Caused by: MidLevelException: LowLevelException
// Suppressed: Resource$CloseFailException: Resource ID = 0
'summary': {
pattern: /^[\t ]*(?:(?:Caused by:|Suppressed:|Exception in thread "[^"]*")[\t ]+)?[\w$.]+(?:\:.*)?$/m,
inside: {
'keyword': {
pattern: /^(\s*)(?:(?:Caused by|Suppressed)(?=:)|Exception in thread)/m,
lookbehind: true
},

// the current thread if the summary starts with 'Exception in thread'
'string': {
pattern: /^(\s*)"[^"]*"/,
lookbehind: true
},
'exceptions': {
pattern: /^(:?\s*)[\w$.]+(?=:|$)/,
lookbehind: true,
inside: {
'class-name': /[\w$]+(?=$|:)/,
'namespace': /[a-z]\w*/,
'punctuation': /[.:]/
}
},
'message': {
pattern: /(:\s*)\S.*/,
lookbehind: true,
alias: 'string'
},
'punctuation': /[:]/
}
},

// at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
// at org.hsqldb.jdbc.Util.throwError(Unknown Source) here could be some notes
// at Util.<init>(Unknown Source)
'stack-frame': {
pattern: /^[\t ]*at [\w$.]+(?:<init>)?\([^()]*\)/m,
inside: {
'keyword': {
pattern: /^(\s*)at/,
lookbehind: true
},
'source': [
// (Main.java:15)
// (Main.scala:15)
{
pattern: /(\()\w+.\w+:\d+(?=\))/,
lookbehind: true,
inside: {
'file': /^\w+\.\w+/,
'punctuation': /:/,
'line-number': {
pattern: /\d+/,
alias: 'number'
}
}
},
// (Unknown Source)
// (Native Method)
// (...something...)
{
pattern: /(\()[^()]*(?=\))/,
lookbehind: true,
inside: {
'keyword': /^(?:Unknown Source|Native Method)$/
}
}
],
'class-name': /[\w$]+(?=\.(?:<init>|[\w$]+)\()/,
'function': /(?:<init>|[\w$]+)(?=\()/,
'namespace': /[a-z]\w*/,
'punctuation': /[.()]/
}
},

// ... 32 more
// ... 32 common frames omitted
'more': {
pattern: /^[\t ]*\.{3} \d+ [a-z]+(?: [a-z]+)*/m,
inside: {
'punctuation': /\.{3}/,
'number': /\d+/,
'keyword': /\b[a-z]+(?: [a-z]+)*\b/
}
}

};
1 change: 1 addition & 0 deletions components/prism-javastacktrace.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions examples/prism-javastacktrace.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<h2>Full example</h2>
<pre><code>javax.servlet.ServletException: Something bad happened
at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:60)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.ExceptionHandlerFilter.doFilter(ExceptionHandlerFilter.java:28)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.example.myproject.OutputBufferFilter.doFilter(OutputBufferFilter.java:33)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: com.example.myproject.MyProjectServletException
at com.example.myproject.MyServlet.doPost(MyServlet.java:169)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.example.myproject.OpenSessionInViewFilter.doFilter(OpenSessionInViewFilter.java:30)
... 27 more
Suppressed: org.hibernate.exception.ConstraintViolationException: could not insert: [com.example.myproject.MyEntity]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2822)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:268)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy19.save(Unknown Source)
at com.example.myproject.MyEntityService.save(MyEntityService.java:59) &lt;-- relevant call (see notes below)
at com.example.myproject.MyServlet.doPost(MyServlet.java:164)
... 32 more
Caused by: java.sql.SQLException: Violation of unique constraint MY_ENTITY_UK_1: duplicate value(s) for column(s) MY_COLUMN in statement [...]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57)
... 54 more</code></pre>
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (!Prism.plugins.toolbar) {
}

// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","toml":"TOML","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","javastacktrace":"Java stack trace","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","toml":"TOML","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
Prism.plugins.toolbar.registerButton('show-language', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tests/languages/javastacktrace/more_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
... 6 more
... 6 common frames omitted

----------------------------------------------------

[
["more", [
["punctuation", "..."],
["number", "6"],
["keyword", "more"]
]],

["more", [
["punctuation", "..."],
["number", "6"],
["keyword", "common frames omitted"]
]]
]

----------------------------------------------------

Checks for the message that some frames were omitted.
71 changes: 71 additions & 0 deletions tests/languages/javastacktrace/stack-frame_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
at Main.main(Main.java:13)
at Main.main(Main.java:13) Same but with some additional notes
at com.foo.bar.Main$FooBar.main(Native Method)
at Main$FooBar.<init>(Unknown Source)

----------------------------------------------------

[
["stack-frame", [
["keyword", "at"],
["class-name", "Main"],
["punctuation", "."],
["function", "main"],
["punctuation", "("],
["source", [
["file", "Main.java"],
["punctuation", ":"],
["line-number", "13"]
]],
["punctuation", ")"]
]],

["stack-frame", [
["keyword", "at"],
["class-name", "Main"],
["punctuation", "."],
["function", "main"],
["punctuation", "("],
["source", [
["file", "Main.java"],
["punctuation", ":"],
["line-number", "13"]
]],
["punctuation", ")"]
]],
" Same but with some additional notes\n",

["stack-frame", [
["keyword", "at"],
["namespace", "com"],
["punctuation", "."],
["namespace", "foo"],
["punctuation", "."],
["namespace", "bar"],
["punctuation", "."],
["class-name", "Main$FooBar"],
["punctuation", "."],
["function", "main"],
["punctuation", "("],
["source", [
["keyword", "Native Method"]
]],
["punctuation", ")"]
]],

["stack-frame", [
["keyword", "at"],
["class-name", "Main$FooBar"],
["punctuation", "."],
["function", "<init>"],
["punctuation", "("],
["source", [
["keyword", "Unknown Source"]
]],
["punctuation", ")"]
]]
]

----------------------------------------------------

Checks for stack frames.
Loading

0 comments on commit 4a8219a

Please sign in to comment.