Skip to content

Commit

Permalink
fix(hooks): Run hooks with captureError
Browse files Browse the repository at this point in the history
Summary:
The beforeSend hook was not being run if the message that was sent
to captureMesage was an instanace of an error.

Semver: patch
  • Loading branch information
TerryMooreII committed Mar 21, 2022
1 parent db85e4d commit 295cd2b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
45 changes: 21 additions & 24 deletions src/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,13 @@ const captureMessage = ({ level = 'log', message, lineContext = {} }: LogMessage
return;
}

// run the beforeSend hooks
const data: LogMessage = (getOptions().hooks || { beforeSend: [] }).beforeSend.reduce((acc: LogMessage, fn: Function) => (acc == null ? null : fn(acc)), {
level,
message,
lineContext,
});

// beforeSend stopped the log
if (data == null) {
return;
}

const logLine: LogDNALogLine = generateLogLine(data);

process(logLine);
generateLogLine({ level, message, lineContext });
};

const captureError = (error: any) => {
if (isSendingDisabled()) return;

const logLine: LogDNALogLine = generateLogLine({
generateLogLine({
level: 'error',
message: error.message,
errorContext: {
Expand All @@ -45,27 +31,38 @@ const captureError = (error: any) => {
},
disableStacktrace: !!(error.stack || error.stacktrace), // Dont generate a second stacktrace for errors since they already have it
});

process(logLine);
};

const generateLogLine = ({ level = 'log', message, lineContext = {}, errorContext = {}, disableStacktrace = false }: LogMessage): LogDNALogLine => {
const generateLogLine = ({ level = 'log', message, lineContext = {}, errorContext = null, disableStacktrace = false }: LogMessage) => {
const opts = getOptions();
return {

// run the beforeSend hooks
const data: LogMessage = (getOptions().hooks || { beforeSend: [] }).beforeSend.reduce((acc: LogMessage, fn: Function) => (acc == null ? null : fn(acc)), {
level,
message,
lineContext,
});

// beforeSend stopped the log
if (data == null) {
return;
}

process({
timestamp: Math.floor(Date.now() / 1000),
app: opts.app || window.location.host,
line: typeof message === 'string' ? message : utils.stringify(message),
level,
line: typeof data.message === 'string' ? data.message : utils.stringify(data.message),
level: data.level,
meta: {
sessionId: getSessionId(),
...getStaticContext(),
...getDynamicContext(),
stacktrace: disableStacktrace || !opts.enableStacktrace ? undefined : utils.getStackTrace(),
context: { ...getContext() },
lineContext,
lineContext: data.lineContext,
errorContext,
},
};
});
};

const internalErrorLogger = (...args: any[]) => {
Expand Down
6 changes: 3 additions & 3 deletions src/logdna.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GlobalErrorHandlerPlugin } from './plugins/global-handler';
declare module 'logdna-browser-2' {}
declare module 'logdna-browser-2' { }

interface LogDNAMethods {}
interface LogDNAMethods { }

// This is fallback to 3rd party plugin methods
// Until TS has better Module Augmentation without
Expand Down Expand Up @@ -69,7 +69,7 @@ export type LogMessage = {
level: LogLevel;
message: any;
lineContext?: LineContext;
errorContext?: ErrorContext;
errorContext?: ErrorContext | null | undefined;
disableStacktrace?: boolean;
};

Expand Down

0 comments on commit 295cd2b

Please sign in to comment.