Prettify Your Error Code
Here's an interesting thing. The best part is that I can't attribute myself with credit for the majority of the body of work involved. In fact, I had some help from an eagle-eyed Marky Roden who spotted a contributing issue, and an astounding three answers from Sven Hasselbach on a StackOverflow question on a key component subject.
Custom Error XPage
With that help I was able to finish putting together a modified custom error XPage, based on the XSnippet by Tony McGuckin; this version includes some place holders for custom theming (e.g.- logo, styling) and most importantly, includes code syntax highlighting! I was able to add Google Code Prettify and get it to work when XPages throws a runtime error both from a full refresh event (that was the easy part), but also with a partial refresh.
That last bit is difficult as a partial refresh invokes a dojo xhr POST against the current XPage and, when getting a runtime error, returns a network response of 500 and returns a:
- default error 500 page
- XPage runtime error page
- custom error XPage
should eliminate some script based attacks via injects. In any case, we're getting around this by using
onload as an attribute on our
img tag. This fires the JS we jam in there, which will append the same linked JS file for Google Code Prettify to the
head tag, which will then execute.
We Might As Well Make It Look Good
Why are we doing this? Well, if you're going to have an error page show off your code, you might as well have it look good.
Check this baby out, a couple notes on implementation below.
As you can see, there's now an
xp:text tag (one of the only two, the other being
xp:panel) at the bottom, just after the normal
script tag, that lets you set and overwrite the
tagName, to become a an
img tag, complete with the
onload event to add the
script tag to the
head. This only renders if it's detected as being in a partial refresh, making a full refresh behave normally. An elegantly simple solution to a surprisingly complex situation.
I have more to share, but I can guarantee the pace over the next few weeks won't be anything like this month. In fact, I have some prep to do for a certain shindig in Atlanta in August (MWLUG 2015!). I hope to see you there!