git.s-ol.nu mmm / b8a7305
keep last content and display nice errors when problems occur s-ol 6 months ago
4 changed file(s) with 64 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
1 Dockerfile
11 include_rules
22
33 !download = |> ^ DOWNLOAD %O^ curl -L https://github.com/fengari-lua/fengari-web/releases/download/@(FENGARI_VERSION)/`basename %o` > %o |>
4 !sassc = |> ^ SCSS %b^ sassc -m %f %o |> | %o.map
4 !sassc = |> ^ SCSS %b^ sassc -m auto %f %o |> | %o.map
55
66 # download fengari dependencies
77 : |> !download |> root/fengari-web.js
44 import ReactiveVar, get_or_create, text, elements from require 'mmm.component'
55 import pre, div, nav, span, button, a, select, option from elements
66 import languages from require 'mmm.highlighting'
7
8 keep = (var) ->
9 last = var\get!
10 var\map (val) ->
11 last = val or last
12 last
713
814 code_cast = (lang) ->
915 {
121127 if not enabled
122128 button 'inspect', onclick: (_, e) -> @inspect\set true
123129
130 @error = ReactiveVar!
131 main\append with get_or_create 'div', 'browser-error', class: @error\map (e) -> if e then 'error-wrap' else 'error-wrap empty'
132 \append (span "an error occured while rendering this view:"), (rehydrate and .node.firstChild)
133 \append @error
134
124135 -- append or patch #browser-content
125136 main\append with get_or_create 'div', 'browser-content', class: 'content'
126 content = ReactiveVar @get_content @facet\get!
137 content = ReactiveVar if rehydrate then .node.lastChild else @get_content @facet\get!
138 \append keep content
127139 if MODE == 'CLIENT'
128140 @facet\subscribe (p) ->
129141 window\setTimeout (-> content\set @get_content p), 150
130 \append content, (rehydrate and .node.lastChild)
131142
132143 if rehydrate
133144 -- force one rerender to set onclick handlers etc
144155 default_convert = (key) => @get key.name, 'mmm/dom'
145156
146157 -- render #browser-content
147 get_content: (prop, convert=default_convert) =>
148 disp_error = (msg) -> pre msg, style: { color: '#f00' }
158 get_content: (prop, err=@error, convert=default_convert) =>
159 clear_error = ->
160 err\set! if MODE == 'CLIENT'
161 disp_error = (msg) ->
162 if MODE == 'CLIENT'
163 err\set pre msg
164 warn "ERROR rendering content: #{msg}"
165 nil
166
149167 active = @active\get!
150168
151169 return disp_error "fileder not found!" unless active
155173
156174 document.body.classList\remove 'loading' if MODE == 'CLIENT'
157175
158 if ok
159 res or disp_error "[no conversion path to #{prop.type}]"
176 if ok and res
177 clear_error!
178 res
179 elseif ok
180 div "[no conversion path to #{prop.type}]"
160181 elseif res and res.match and res\match '%[nossr%]$'
161 warn '(SSR disabled)'
162 div!
182 div "[this page could not be pre-renderer on the server]"
163183 else
164 warn "error: #{res}", trace
165 disp_error "error: #{res}\n#{trace}"
184 if trace
185 res = "#{res}\n#{trace}"
186 disp_error res
166187
167188 get_inspector: =>
168189 -- active facet in inspect tab
172193 key = active and active\find prop
173194 key = key.original if key and key.original
174195 key
196
197 @inspect_err = ReactiveVar!
175198
176199 with div class: 'view inspector'
177200 \append nav {
195218 if enabled
196219 button 'close', onclick: (_, e) -> @inspect\set false
197220 }
221 \append with div class: @inspect_err\map (e) -> if e then 'error-wrap' else 'error-wrap empty'
222 \append span "an error occured while rendering this view:"
223 \append @inspect_err
198224 \append with pre class: 'content'
199 \append @inspect_prop\map (prop) ->
200 @get_content prop, (prop) =>
225 \append keep @inspect_prop\map (prop, old) ->
226 @get_content prop, @inspect_err, (prop) =>
201227 value, key = @get prop
202228 assert key, "couldn't @get #{prop}"
203229
6767 }
6868 }
6969
70 .error-wrap {
71 flex: 0 0 auto;
72 padding: 1em 2em;
73 overflow: hidden;
74
75 background: $gray-fail;
76
77 &.empty {
78 display: none;
79 }
80
81 > span {
82 display: inline-block;
83 margin-bottom: 0.5em;
84
85 font-weight: bold;
86 color: #f00;
87 }
88
89 > pre {
90 margin: 0;
91 }
92 }
93
7094 .content {
7195 flex: 1 1 auto;
7296 overflow: auto;