Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow async beforeNotify functions to modify the notice object #984

Merged
merged 17 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/core/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ export abstract class Client {
// in case they modify them
const sourceCodeData = notice && notice.backtrace ? notice.backtrace.map(trace => shallowClone(trace) as BacktraceFrame) : null
const beforeNotifyResult = runBeforeNotifyHandlers(notice, this.__beforeNotifyHandlers)
if (!preConditionError && !beforeNotifyResult) {
this.logger.debug('skipping error report: beforeNotify handlers returned false', notice)


if (!preConditionError && !beforeNotifyResult.result) {
this.logger.debug('skipping error report: one or more beforeNotify handlers returned false', notice)
preConditionError = new Error('beforeNotify handlers returned false')
}

Expand All @@ -189,11 +191,13 @@ export abstract class Client {
notice.__breadcrumbs = this.config.breadcrumbsEnabled ? breadcrumbs : []

getSourceForBacktrace(sourceCodeData, this.__getSourceFileHandler)
.then(sourcePerTrace => {
.then(async (sourcePerTrace) => {
sourcePerTrace.forEach((source, index) => {
notice.backtrace[index].source = source
})

// Make sure all of our promises have finished before sending the payload.
await Promise.allSettled(beforeNotifyResult.results)
const payload = this.__buildPayload(notice)
this.__transport
.send({
Expand Down Expand Up @@ -223,7 +227,7 @@ export abstract class Client {
this.logger.error('Error report failed: an unknown error occurred.', `message=${err.message}`)
runAfterNotifyHandlers(notice, this.__afterNotifyHandlers, err)
})
})
})

return true
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ServerlessConfig extends Config {
}

export interface BeforeNotifyHandler {
(notice?: Notice): boolean | void
(notice?: Notice): boolean | void | Promise<boolean | void>
}

export interface AfterNotifyHandler {
Expand Down
16 changes: 13 additions & 3 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,25 @@ export async function getSourceForBacktrace(backtrace: BacktraceFrame[],
return result
}

export function runBeforeNotifyHandlers(notice: Notice | null, handlers: BeforeNotifyHandler[]): boolean {
export function runBeforeNotifyHandlers(notice: Notice | null, handlers: BeforeNotifyHandler[]): { results: ReturnType<BeforeNotifyHandler>[], result: boolean } {
subzero10 marked this conversation as resolved.
Show resolved Hide resolved
subzero10 marked this conversation as resolved.
Show resolved Hide resolved
const results: ReturnType<BeforeNotifyHandler>[] = []
let result = true
for (let i = 0, len = handlers.length; i < len; i++) {
const handler = handlers[i]
if (handler(notice) === false) {

const handlerResult = handler(notice)

if (handlerResult === false) {
result = false
}

results.push(handlerResult)
}

return {
results,
result
}
return result
}

export function runAfterNotifyHandlers(notice: Notice | null, handlers: AfterNotifyHandler[], error?: Error): boolean {
subzero10 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
50 changes: 50 additions & 0 deletions packages/core/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,56 @@ describe('client', function () {
client.notify('should not report')
})
})

it('modifies the notice when an async function is provided', function () {
const funkyName = 'My funky name'

client.beforeNotify(async (notice) => {
const modifyName = () => new Promise<void>((resolve) => {
setTimeout(() => {
notice.name = funkyName
resolve()
})
})


await modifyName()
})

return new Promise<void>((resolve) => {
client.afterNotify((_err, notice) => {
expect(notice.name).toEqual(funkyName)
resolve()
})

expect(client.notify('Should report and modify notice')).toEqual(true)
})
})

it('modifies the notice when an async function is provided', function () {
KonnorRogers marked this conversation as resolved.
Show resolved Hide resolved
const funkyName = 'My funky name'

client.beforeNotify(async (notice) => {
const modifyName = () => new Promise<void>((resolve) => {
setTimeout(() => {
notice.name = funkyName
resolve()
})
})


await modifyName()
})

return new Promise<void>((resolve) => {
client.afterNotify((_err, notice) => {
expect(notice.name).toEqual(funkyName)
resolve()
})

client.notify('Should report and modify notice')
})
})
})

describe('afterNotify', function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ describe('utils', function () {
() => false,
() => true
]
expect(runBeforeNotifyHandlers(<never>{}, handlers)).toEqual(false)
expect(runBeforeNotifyHandlers(<never>{}, handlers).result).toEqual(false)
})

it('returns true when all handlers return true', function () {
const handlers = [
() => true,
() => true
]
expect(runBeforeNotifyHandlers(<never>{}, handlers)).toEqual(true)
expect(runBeforeNotifyHandlers(<never>{}, handlers).result).toEqual(true)
})

it('passes the notice to handlers', function () {
Expand Down