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
| 'use strict'
|
| const http = require('http')
| const Youch = require('../src/Youch')
|
| class HttpException extends Error {
| constructor (...args) {
| super(...args)
| this.name = this.constructor.name
| }
| }
|
| function foo () {
| const error = new HttpException('Some weird error')
| error.status = 503
| throw error
| }
|
| http.createServer((req, res) => {
| let youch = null
| try {
| foo()
| } catch (e) {
| youch = new Youch(e, req)
| }
|
| youch
| .toHTML()
| .then((response) => {
| res.writeHead(200, {'content-type': 'text/html'})
| res.write(response)
| res.end()
| }).catch((error) => {
| res.writeHead(500)
| res.write(error.message)
| res.end()
| })
| }).listen(8000, () => {
| console.log('listening to port 8000')
| })
|
|