|
1 | | -import fetch from 'node-fetch' |
| 1 | +import got from 'got' |
| 2 | +import { Failbot, HTTPBackend, LogBackend } from '@github/failbot' |
2 | 3 |
|
3 | | -export default class FailBot { |
4 | | - constructor({ app, haystackURL, headers }) { |
5 | | - this.app = app |
6 | | - this.headers = headers |
| 4 | +const HAYSTACK_APP = 'docs' |
7 | 5 |
|
8 | | - // Since we're using `node-fetch` we can't rely on it deconstructing the |
9 | | - // basic authentication credentials from the URL (e.g. |
10 | | - // https://user:pass@failbotdomain/path) because `node-fetch` will always |
11 | | - // strip it. See https://github.com/node-fetch/node-fetch/issues/1330 |
12 | | - // and it's not a bug. |
13 | | - // The correct thing is to extract it manually and add an `Authorization` |
14 | | - // header based on it from the URL. |
15 | | - const url = new URL(haystackURL) |
| 6 | +export function report(error, metadata) { |
| 7 | + // If there's no HAYSTACK_URL set, bail early |
| 8 | + if (!process.env.HAYSTACK_URL) return |
16 | 9 |
|
17 | | - // remove the basic auth portion of the url since it throws an error in node-fetch |
18 | | - this.haystackURL = `${url.origin}${url.pathname}` |
19 | | - |
20 | | - const { username, password } = url |
21 | | - if (username || password) { |
22 | | - this.headers.Authorization = `Basic ${Buffer.from(`${username}:${password}`).toString( |
23 | | - 'base64' |
24 | | - )}` |
25 | | - } else { |
26 | | - console.warn(`The haystack URL does not contain authentication credentials`) |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - /** |
31 | | - * Report an error to Sentry |
32 | | - * @param {Error} error |
33 | | - * @param {any} metadata |
34 | | - * @param {any} [headers] |
35 | | - */ |
36 | | - static async report(error, metadata, headers = {}) { |
37 | | - // If there's no HAYSTACK_URL set, bail early |
38 | | - if (!process.env.HAYSTACK_URL) return |
39 | | - |
40 | | - const failbot = new FailBot({ |
41 | | - app: 'docs', |
| 10 | + const backends = [ |
| 11 | + new HTTPBackend({ |
42 | 12 | haystackURL: process.env.HAYSTACK_URL, |
43 | | - headers, |
44 | | - }) |
45 | | - |
46 | | - return failbot.sendException(error, metadata) |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Create a rollup of this error by generating a base64 representation |
51 | | - * @param {Error} error |
52 | | - */ |
53 | | - createRollup(error) { |
54 | | - const stackLine = error.stack && error.stack.split('\n')[1] |
55 | | - const str = `${error.name}:${stackLine}`.replace(/=/g, '') |
56 | | - return Buffer.from(str).toString('base64') |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - * Format the error to a plain JSON object with additional data |
61 | | - * @param {Error} error |
62 | | - * @param {any} metadata |
63 | | - */ |
64 | | - formatJSON(error, metadata) { |
65 | | - return Object.assign({}, metadata, { |
66 | | - /* eslint-disable camelcase */ |
67 | | - created_at: new Date().toISOString(), |
68 | | - rollup: this.createRollup(error), |
69 | | - class: error.name, |
70 | | - message: error.message, |
71 | | - backtrace: error.stack || '', |
72 | | - js_environment: `Node.js ${process.version}`, |
73 | | - /* eslint-enable camelcase */ |
74 | | - }) |
75 | | - } |
76 | | - |
77 | | - /** |
78 | | - * Populate default context from settings. Since settings commonly comes from |
79 | | - * ENV, this allows setting defaults for the context via the environment. |
80 | | - */ |
81 | | - getFailbotContext() { |
82 | | - const failbotKeys = {} |
83 | | - |
84 | | - for (const key in process.env) { |
85 | | - if (key.startsWith('FAILBOT_CONTEXT_')) { |
86 | | - const formattedKey = key.replace(/^FAILBOT_CONTEXT_/, '').toLowerCase() |
87 | | - failbotKeys[formattedKey] = process.env[key] |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - return failbotKeys |
| 13 | + fetchFn: got, |
| 14 | + }), |
| 15 | + ] |
| 16 | + if (process.env.NODE_ENV !== 'test') { |
| 17 | + backends.push(new LogBackend({ log: console.log.bind(console) })) |
92 | 18 | } |
| 19 | + const failbot = new Failbot({ |
| 20 | + app: HAYSTACK_APP, |
| 21 | + backends: backends, |
| 22 | + }) |
| 23 | + return failbot.report(error, metadata) |
| 24 | +} |
93 | 25 |
|
94 | | - /** |
95 | | - * Send the error to Sentry |
96 | | - * @param {Error} error |
97 | | - * @param {any} metadata |
98 | | - */ |
99 | | - async sendException(error, metadata = {}) { |
100 | | - const data = Object.assign({ app: this.app }, this.getFailbotContext(), metadata) |
101 | | - const body = this.formatJSON(error, Object.assign({ app: this.app }, data)) |
102 | | - |
103 | | - return fetch(this.haystackURL, { |
104 | | - method: 'POST', |
105 | | - body: JSON.stringify(body), |
106 | | - headers: { |
107 | | - ...this.headers, |
108 | | - 'Content-Type': 'application/json', |
109 | | - }, |
110 | | - }) |
111 | | - } |
| 26 | +// Kept for legacy so you can continue to do: |
| 27 | +// |
| 28 | +// import FailBot from './lib/failbot.js' |
| 29 | +// ... |
| 30 | +// FailBot.report(myError) |
| 31 | +// |
| 32 | +export default { |
| 33 | + report, |
112 | 34 | } |
0 commit comments