-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (55 loc) · 2.11 KB
/
Copy pathindex.js
File metadata and controls
68 lines (55 loc) · 2.11 KB
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
59
60
61
62
63
64
65
66
67
68
const bugsnagInFlight = require('@bugsnag/in-flight')
const clone = require('@bugsnag/core/lib/clone-client')
const BugsnagPluginAzureFunctions = {
name: 'azureFunctions',
load (client) {
bugsnagInFlight.trackInFlight(client)
return {
createHandler ({ flushTimeoutMs = 2000 } = {}) {
return wrapHandler.bind(null, client, flushTimeoutMs)
}
}
}
}
// Function which takes in the Azure Function handler and wraps it with
// a new handler that automatically captures unhandled errors
function wrapHandler (client, flushTimeoutMs, handler) {
// Reset the app duration between invocations, if the plugin is loaded
const appDurationPlugin = client.getPlugin('appDuration')
return async function (context, ...args) {
// Get a client to be scoped to this function invocation. If sessions are enabled, use the
// resumeSession() call to get a session client, otherwise, clone the existing client.
const functionClient = client._config.autoTrackSessions ? client.resumeSession() : clone(client)
if (appDurationPlugin) {
appDurationPlugin.reset()
}
// Attach the client to the context
context.bugsnag = functionClient
// Add global metadata attach the context
functionClient.addOnError(event => {
event.addMetadata('Azure Function context', context)
event.clearMetadata('Azure Function context', 'bugsnag')
})
try {
return await handler(context, ...args)
} catch (err) {
if (client._config.autoDetectErrors && client._config.enabledErrorTypes.unhandledExceptions) {
const handledState = {
severity: 'error',
unhandled: true,
severityReason: { type: 'unhandledException' }
}
const event = functionClient.Event.create(err, true, handledState, 'azure functions plugin', 1)
functionClient._notify(event)
}
throw err
} finally {
try {
await bugsnagInFlight.flush(flushTimeoutMs)
} catch (err) {
functionClient._logger.error(`Delivery may be unsuccessful: ${err.message}`)
}
}
}
}
module.exports = BugsnagPluginAzureFunctions