Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

handle-errors.js 1.7 KB

You have to be logged in to leave a comment. Sign In
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  1. const { liquid } = require('../lib/render-content')
  2. const layouts = require('../lib/layouts')
  3. const FailBot = require('../lib/failbot')
  4. const loadSiteData = require('../lib/site-data')
  5. function shouldLogException (error) {
  6. const IGNORED_ERRORS = [
  7. // avoid sending CSRF token errors (from bad-actor POST requests)
  8. 'EBADCSRFTOKEN'
  9. ]
  10. if (IGNORED_ERRORS.includes(error.code)) {
  11. return false
  12. }
  13. // We should log this exception
  14. return true
  15. }
  16. module.exports = async function handleError (error, req, res, next) {
  17. // if the error is thrown before req.context is created (say, in the Page class),
  18. // set req.context.site here so we can pass data/ui.yml text to the 500 layout
  19. if (!req.context) {
  20. const site = await loadSiteData()
  21. req.context = { site: site[req.language || 'en'].site }
  22. }
  23. // display error on the page in development, but not in production
  24. if (process.env.NODE_ENV !== 'production' && req.context) {
  25. req.context.error = error
  26. }
  27. // Special handling for when a middleware calls `next(404)`
  28. if (error === 404) {
  29. return res
  30. .status(404)
  31. .send(await liquid.parseAndRender(layouts['error-404'], req.context))
  32. }
  33. // If the error contains a status code, just send that back. This is usually
  34. // from a middleware like `express.json()` or `csrf`.
  35. if (error.statusCode || error.status) {
  36. return res.sendStatus(error.statusCode || error.status)
  37. }
  38. if (process.env.NODE_ENV !== 'test') {
  39. console.error('500 error!', req.path)
  40. console.error(error)
  41. if (shouldLogException(error)) {
  42. await FailBot.report(error, {
  43. path: req.path
  44. })
  45. }
  46. }
  47. res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context))
  48. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...