Skip to content

Commit

Permalink
wip error screen
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Mar 17, 2024
1 parent ac3355c commit 9a27d30
Showing 1 changed file with 132 additions and 12 deletions.
144 changes: 132 additions & 12 deletions src/tim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/tim
import std/json except `%*`
import std/[times, options, asyncdispatch, sequtils, macros, macrocache]
import std/[times, options, asyncdispatch,
sequtils, macros, macrocache, htmlgen]

import pkg/[watchout, httpx, websocketx]
import pkg/kapsis/cli

import tim/engine/[meta, parser, logging]
import tim/engine/compilers/html

from std/strutils import `%`, indent
from std/strutils import `%`, indent, split, parseInt, join
from std/os import `/`, sleep

from std/xmltree import escape

const
DOCKTYPE = "<!DOCKTYPE html>"
defaultLayout = "base"
Expand Down Expand Up @@ -56,10 +59,128 @@ proc jitCompiler(engine: TimEngine,
engine.getIndentSize, data
)

proc displayErrors(l: Logger) =
proc toHtml*(name, code: string): string =
let p = parseSnippet(name, code)
if likely(not p.hasErrors):
let c = newCompiler(parser.getAst(p), false)
return c.getHtml()

when not defined release:
proc showHtmlError(msg: var string, engine: TimEngine, l: Logger) =
var timErrorScreen = """
style: "
#timEngineErrorScreenWrapper {
font-family: system-ui,-apple-system,'Segoe UI',Roboto,'Helvetica Neue','Noto Sans','Liberation Sans',Arial,sans-serif,'Apple Color Emoji','Segoe UI Emoji','Segoe UI Symbol','Noto Color Emoji';
background-color: #111;
color: #fff;
position: fixed;
top:0;
left:0;
width: 100%;
height: 100%;
display: flex;
}
#timEngineErrorScreenWrapper * {
box-sizing: border-box;
}
header.timEngineErrorScreenHeader {
background: #111;
padding: 25px
}
header.timEngineErrorScreenHeader h1 {
font-size: 48px;
margin: 0;
}
.tim--error-container {
width: 100%;
padding:0 1.5rem;
margin: auto;
align-self: center;
}
.tim--error-row {
display: flex;
flex-wrap: wrap;
margin:0 -1.5rem
}
.tim--error-row>* {
flex-shrink: 0;
width: 100%;
max-width: 100%;
}
.tim--error-col-8 {
flex: 0 0 auto;
width: 56.66666667%;
}
.tim--error-col-4 {
flex: 0 0 auto;
width: 43.33333333%;
}
.tim-error-preview-code {
background: #212121;
overflow-x: auto;
font-size: 16px;
font-family: ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;
}
.tim-error-preview-code li:before {
content: attr(data-lineno)
}
.tim-error-preview-code li {
list-style: none;
min-height: 29px;
line-height: 29px;
text-wrap: nowrap;
}
.tim--error-li-msg {
font-family: ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;
font-size: 16px;
list-style: decimal;
background-color: #ea4444a1;
border-radius: 5px;
padding: 2px 10px;
margin-bottom: 10px;
}
.tim-error-preview-code li:last-child {
border-bottom: 0
}
"
section#timEngineErrorScreenWrapper > div.tim--error-container
div.tim--error-row style="align-items: center"
"""
add timErrorScreen, """
div.tim--error-col-4
header.timEngineErrorScreenHeader:
h1 style="font-weight:bold;margin-bottom:20px": "Ugh! Something broke"
"""
var rightSide: string
for e in l.errors:
let lc = e[1].text[1..^2].split(":")
var ln = parseInt(lc[0])
var txt = e[1].text
add txt, e[2].text.indent(1)
add txt, indent(e[3..^1].mapIt(it.text).join(" "), 1)
add rightSide, indent("li.tim--error-li-msg: \"$1\"", 10)
rightSide = rightSide % [txt]
add timErrorscreen, rightSide
msg = toHtml("tim-engine-error", timErrorScreen)
var htmlerror: string

template displayErrors(l: Logger) =
for err in l.errors:
display(err)
display(l.filePath)
when not defined release:
if engine.showHtmlErrors:
htmlerror.showHtmlError(engine, l)

proc compileCode(engine: TimEngine, tpl: TimTemplate,
refreshAst = false) =
Expand All @@ -84,8 +205,7 @@ proc compileCode(engine: TimEngine, tpl: TimTemplate,
engine.writeHtmlTail(tpl, c.getTail)
else: discard
else: c.logger.displayErrors()
else:
p.logger.displayErrors()
else: p.logger.displayErrors()

var sync: Thread[(Port, int)]
var lastModified, prevModified: Time
Expand Down Expand Up @@ -166,9 +286,6 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil,
case tpl.getType
of ttView, ttLayout:
engine.compileCode(tpl)
# if engine.errors.len > 0:
# for err in engine.errors:
# echo err
else: discard

# Callback `onChange`
Expand All @@ -179,10 +296,6 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil,
case tpl.getType()
of ttView, ttLayout:
engine.compileCode(tpl)
# if engine.errors.len > 0:
# for err in engine.errors:
# echo err
# else:
lastModified = getTime()
else:
engine.resolveDependants(tpl.getDeps.toSeq)
Expand Down Expand Up @@ -213,6 +326,7 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil,
template layoutWrapper(getViewBlock) {.dirty.} =
result = DOCKTYPE
var layoutTail: string
var displayError: bool
if not layout.jitEnabled:
# when requested layout is pre-rendered
# will use the static HTML version from disk
Expand All @@ -226,7 +340,12 @@ template layoutWrapper(getViewBlock) {.dirty.} =
getViewBlock
layoutTail = jitLayout.getTail()
else:
displayError = true
jitLayout.logger.displayErrors()
when not defined release:
if displayError:
add result, htmlerror
htmlerror = ""
add result, layoutTail

proc render*(engine: TimEngine, viewName: string,
Expand Down Expand Up @@ -254,6 +373,7 @@ proc render*(engine: TimEngine, viewName: string,
add result, indent(jitView.getHtml(), layout.getViewIndent)
else:
jitView.logger.displayErrors()
displayError = true
else:
raise newException(TimError, "No layouts available")
else:
Expand Down

0 comments on commit 9a27d30

Please sign in to comment.