@@ -9,17 +9,25 @@ require! {
9
9
10
10
global <<< require \prelude -ls
11
11
12
- config = JSON .parse(fs .read-file -sync './config.json' ) # intentionally crashes if malformed & sync
13
-
14
- export four -oh -four -handler = (next ) ->*
15
- return unless @status is 404 # guard
16
- @status = 404 # so koa doesn't 200 or 404
17
- switch (@accepts \html \json )
18
- | \json =>
19
- @body = message : 'Page Not Found!'
20
- | otherwise => # TODO load static jade assets
21
- @type = \html
22
- @body = '404, Page Not Found!'
12
+ cwd = process .cwd!
13
+ config = require "#cwd/config.json"
14
+ html404 = fs .read-file -sync "#cwd/public/404.html" .to-string !
15
+ html50x = fs .read-file -sync "#cwd/public/50x.html" .to-string !
16
+
17
+
18
+ export error -handler = (next ) ->*
19
+ try
20
+ yield next
21
+ if @status is 404 then throw 404
22
+ catch
23
+ @status = if typeof ! e is \Number then e else e .status or 500 # default 500
24
+ switch @accepts(\html \text \json ) # -> out!
25
+ | \json =>
26
+ @body = message : if @status is 404 then 'Page Not Found!' else 'Error, Try Again!'
27
+ | otherwise =>
28
+ @type = \html
29
+ @body = if @status is 404 then html404 else html50x
30
+ @app .emit \error , e , @ # report to koa, too
23
31
24
32
# app-cache manifest needs headers
25
33
export app -cache = (next ) ->*
@@ -32,6 +40,7 @@ export app-cache = (next) ->*
32
40
@status = 404
33
41
yield next
34
42
43
+
35
44
# localize config.json for env
36
45
export config -locals = (next ) ->*
37
46
config <<< config [@locals .env] # merge in current env's config
@@ -45,6 +54,8 @@ export config-locals = (next) ->*
45
54
"#v:#{@locals.port}"
46
55
yield next
47
56
57
+
58
+ # react
48
59
export react = (next ) ->* # set body to react tree
49
60
locals = {} <<< @locals
50
61
path = url .parse (@url or '/' ) .pathname
0 commit comments