-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.coffee
More file actions
41 lines (33 loc) · 986 Bytes
/
errors.coffee
File metadata and controls
41 lines (33 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class NotFound extends Error
constructor: (path) ->
@.name = 'NotFound'
if path
Error.call @, "Cannot find #{path}"
@.path = path
else
Error.call @, "Not Found"
Error.captureStackTrace @, arguments.callee
# This is handy function to replace the value of an URL param
# replaceRouteParam = (req, param, replacement) ->
# segments = req.app.routes.routes.get[req._route_index].path.split('/')
# new_segments = []
# for segment in segments
# if segment is param
# new_segments.push replacement
# else if segment[0] is ':'
# new_segments.push req.params[segment[1..]]
# else
# new_segments.push segment
#
# new_segments.join '/'
module.exports = (app) ->
app.error (err, req, res, next) ->
console.log 'Error handler', err
console.log err.stack
if err instanceof NotFound
res.render '404', status: 404
else
res.render '500', status: 500
return {
NotFound: NotFound
}