sentry-vir 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,10 @@ export * from './event-context/event-severity';
5
5
  export * from './event-context/extra-context.error';
6
6
  export * from './event-context/extra-event-context';
7
7
  export * from './init-sentry/base-sentry-init';
8
+ export * from './logging/attach-tags';
9
+ export * from './logging/handle-error';
10
+ export * from './logging/send-log';
11
+ export * from './logging/sentry-client-for-logging';
8
12
  export * from './processing/event-processor';
9
13
  export * from './processing/handle-sentry-send';
10
14
  export * from './processing/sentry-config';
11
- export * from './processing/sentry-logger';
package/dist/cjs/index.js CHANGED
@@ -21,7 +21,10 @@ __exportStar(require("./event-context/event-severity"), exports);
21
21
  __exportStar(require("./event-context/extra-context.error"), exports);
22
22
  __exportStar(require("./event-context/extra-event-context"), exports);
23
23
  __exportStar(require("./init-sentry/base-sentry-init"), exports);
24
+ __exportStar(require("./logging/attach-tags"), exports);
25
+ __exportStar(require("./logging/handle-error"), exports);
26
+ __exportStar(require("./logging/send-log"), exports);
27
+ __exportStar(require("./logging/sentry-client-for-logging"), exports);
24
28
  __exportStar(require("./processing/event-processor"), exports);
25
29
  __exportStar(require("./processing/handle-sentry-send"), exports);
26
30
  __exportStar(require("./processing/sentry-config"), exports);
27
- __exportStar(require("./processing/sentry-logger"), exports);
@@ -0,0 +1,11 @@
1
+ import type { setTags } from '@sentry/core';
2
+ /** A list of tag names as keys and their values. Set a tag to undefined to clear it. */
3
+ export type SentryTags = Parameters<typeof setTags>[0];
4
+ /**
5
+ * Set tags for all future Sentry event handling (errors and logs). This can be called before
6
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
7
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
8
+ *
9
+ * Similar to handleError and sendLog.
10
+ */
11
+ export declare function attachSentryTags(tags: SentryTags): undefined;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.attachSentryTags = void 0;
4
+ const premature_events_1 = require("./premature-events");
5
+ const sentry_client_for_logging_1 = require("./sentry-client-for-logging");
6
+ /**
7
+ * Set tags for all future Sentry event handling (errors and logs). This can be called before
8
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
9
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
10
+ *
11
+ * Similar to handleError and sendLog.
12
+ */
13
+ function attachSentryTags(tags) {
14
+ try {
15
+ if (!sentry_client_for_logging_1.sentryClientForLogging) {
16
+ (0, premature_events_1.addPrematureEvent)({ callback: attachSentryTags, inputs: [tags] });
17
+ return;
18
+ }
19
+ sentry_client_for_logging_1.sentryClientForLogging.setTags(tags);
20
+ }
21
+ catch (caught) {
22
+ console.error('Error while trying to attach Sentry tags:', caught);
23
+ return undefined;
24
+ }
25
+ }
26
+ exports.attachSentryTags = attachSentryTags;
@@ -0,0 +1,9 @@
1
+ import { EventExtraContext } from '../event-context/event-context';
2
+ /**
3
+ * Record an error to Sentry without throwing it. This can be called before
4
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
5
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
6
+ *
7
+ * Similar to attachSentryTags and sendLog.
8
+ */
9
+ export declare function handleError(error: unknown, extraContext?: EventExtraContext): string | undefined;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleError = void 0;
4
+ const event_context_1 = require("../event-context/event-context");
5
+ const event_severity_1 = require("../event-context/event-severity");
6
+ const premature_events_1 = require("./premature-events");
7
+ const sentry_client_for_logging_1 = require("./sentry-client-for-logging");
8
+ /**
9
+ * Record an error to Sentry without throwing it. This can be called before
10
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
11
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
12
+ *
13
+ * Similar to attachSentryTags and sendLog.
14
+ */
15
+ function handleError(error, extraContext) {
16
+ try {
17
+ if (!sentry_client_for_logging_1.sentryClientForLogging) {
18
+ (0, premature_events_1.addPrematureEvent)({
19
+ callback: handleError,
20
+ inputs: [
21
+ error,
22
+ extraContext,
23
+ ],
24
+ });
25
+ return undefined;
26
+ }
27
+ const scopeContext = (0, event_context_1.convertEventDetailsToSentryContext)({
28
+ extraContext,
29
+ severity: event_severity_1.EventSeverityEnum.Error,
30
+ });
31
+ const eventId = sentry_client_for_logging_1.sentryClientForLogging.captureException(error, scopeContext);
32
+ return eventId;
33
+ }
34
+ catch (caught) {
35
+ console.error('Error while trying to handle error with Sentry:', caught);
36
+ return undefined;
37
+ }
38
+ }
39
+ exports.handleError = handleError;
@@ -0,0 +1,8 @@
1
+ import { AnyFunction } from '@augment-vir/common';
2
+ /** An event that was triggered before setSentryClientForLogging was called. */
3
+ export type PrematureEvent<EntryPointFunction extends AnyFunction = AnyFunction> = {
4
+ callback: EntryPointFunction;
5
+ inputs: Parameters<EntryPointFunction>;
6
+ };
7
+ export declare function sendPrematureEvents(): void;
8
+ export declare function addPrematureEvent(prematureEvent: PrematureEvent): void;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.addPrematureEvent = exports.sendPrematureEvents = void 0;
4
+ /**
5
+ * Used to store events before the Sentry client is setup. This is exported for testing purposes
6
+ * only, you don't need to do anything with this.
7
+ */
8
+ const prematureSentryEvents = [];
9
+ function sendPrematureEvents() {
10
+ while (prematureSentryEvents.length) {
11
+ try {
12
+ const prematureEvent = prematureSentryEvents.pop();
13
+ if (!prematureEvent) {
14
+ return;
15
+ }
16
+ prematureEvent.callback(...prematureEvent.inputs);
17
+ }
18
+ catch (caught) {
19
+ console.error('error while trying to send premature sentry events:', caught);
20
+ }
21
+ }
22
+ }
23
+ exports.sendPrematureEvents = sendPrematureEvents;
24
+ function addPrematureEvent(prematureEvent) {
25
+ prematureSentryEvents.push(prematureEvent);
26
+ }
27
+ exports.addPrematureEvent = addPrematureEvent;
@@ -0,0 +1,19 @@
1
+ import { Event as SentryEvent } from '@sentry/types';
2
+ import { EventDetails, EventExtraContext } from '../event-context/event-context';
3
+ /**
4
+ * Send non-error events to Sentry. This can be called before setSentryClientForLogging has been
5
+ * called, as sentry-vir will buffer all events. However, setSentryClientForLogging must be called
6
+ * once at some point to actually clear that buffer.
7
+ *
8
+ * Similar to attachSentryTags and handleError.
9
+ */
10
+ export declare const sendLog: {
11
+ /** Sends an even to Sentry with debug severity. */
12
+ readonly debug: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
13
+ /** Sends an even to Sentry with info severity. */
14
+ readonly info: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
15
+ /** Sends an even to Sentry with warning severity. */
16
+ readonly warning: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
17
+ };
18
+ declare function sendLogToSentry(logInfo: string | Omit<SentryEvent, 'extra' | 'level'>, eventDetails: EventDetails): string | undefined;
19
+ export {};
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sendLog = void 0;
4
+ const common_1 = require("@augment-vir/common");
5
+ const event_context_1 = require("../event-context/event-context");
6
+ const event_severity_1 = require("../event-context/event-severity");
7
+ const premature_events_1 = require("./premature-events");
8
+ const sentry_client_for_logging_1 = require("./sentry-client-for-logging");
9
+ /**
10
+ * Send non-error events to Sentry. This can be called before setSentryClientForLogging has been
11
+ * called, as sentry-vir will buffer all events. However, setSentryClientForLogging must be called
12
+ * once at some point to actually clear that buffer.
13
+ *
14
+ * Similar to attachSentryTags and handleError.
15
+ */
16
+ exports.sendLog = {
17
+ /** Sends an even to Sentry with debug severity. */
18
+ [event_severity_1.EventSeverityEnum.Debug]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Debug),
19
+ /** Sends an even to Sentry with info severity. */
20
+ [event_severity_1.EventSeverityEnum.Info]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Info),
21
+ /** Sends an even to Sentry with warning severity. */
22
+ [event_severity_1.EventSeverityEnum.Warning]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Warning),
23
+ };
24
+ function wrapLogWithSeverity(severity) {
25
+ return (info, extraContext) => {
26
+ return sendLogToSentry(info, {
27
+ extraContext,
28
+ severity,
29
+ });
30
+ };
31
+ }
32
+ function sendLogToSentry(logInfo, eventDetails) {
33
+ try {
34
+ if (!sentry_client_for_logging_1.sentryClientForLogging) {
35
+ (0, premature_events_1.addPrematureEvent)({
36
+ callback: sendLogToSentry,
37
+ inputs: [
38
+ logInfo,
39
+ eventDetails,
40
+ ],
41
+ });
42
+ return undefined;
43
+ }
44
+ const scopeContext = (0, event_context_1.convertEventDetailsToSentryContext)(eventDetails);
45
+ const eventId = (0, common_1.isRuntimeTypeOf)(logInfo, 'string')
46
+ ? sentry_client_for_logging_1.sentryClientForLogging.captureMessage(logInfo, scopeContext)
47
+ : sentry_client_for_logging_1.sentryClientForLogging.captureEvent({
48
+ ...logInfo,
49
+ ...scopeContext,
50
+ });
51
+ return eventId;
52
+ }
53
+ catch (caught) {
54
+ console.error('Error while trying to send Sentry log:', caught);
55
+ return undefined;
56
+ }
57
+ }
@@ -0,0 +1,21 @@
1
+ import { MaybePromise } from '@augment-vir/common';
2
+ import { SentryDep } from '../env/execution-env';
3
+ /** The bare minimum Sentry client needed for logging events. */
4
+ export type SentryClientForLogging = Pick<SentryDep, 'captureMessage' | 'captureException' | 'captureEvent' | 'setTags'>;
5
+ /**
6
+ * Internal sentry client used for logging. Use setSentryClientForLogging to set this. Allows
7
+ * handleError, setTags, and sendLog to be called before the Sentry client has been setup yet, as
8
+ * once this is set all events fired from those functions will get sent to Sentry.
9
+ */
10
+ export declare let sentryClientForLogging: SentryClientForLogging | undefined;
11
+ /**
12
+ * Asynchronously set the Sentry client for logging. When this is called, any events that were
13
+ * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
14
+ * asynchronous Sentry client setup but synchronous log and error handling.
15
+ *
16
+ * This should be called as soon as possible after you have a return value from initSentry.
17
+ *
18
+ * This can be safely called multiple times (to overwrite the previously set Sentry client) because
19
+ * previous events won't be handled multiple times.
20
+ */
21
+ export declare function setSentryClientForLogging(client: MaybePromise<SentryClientForLogging>): Promise<void>;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setSentryClientForLogging = exports.sentryClientForLogging = void 0;
4
+ const premature_events_1 = require("./premature-events");
5
+ /**
6
+ * Asynchronously set the Sentry client for logging. When this is called, any events that were
7
+ * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
8
+ * asynchronous Sentry client setup but synchronous log and error handling.
9
+ *
10
+ * This should be called as soon as possible after you have a return value from initSentry.
11
+ *
12
+ * This can be safely called multiple times (to overwrite the previously set Sentry client) because
13
+ * previous events won't be handled multiple times.
14
+ */
15
+ async function setSentryClientForLogging(client) {
16
+ const hadClientBefore = !!exports.sentryClientForLogging;
17
+ exports.sentryClientForLogging = await client;
18
+ if (!hadClientBefore) {
19
+ (0, premature_events_1.sendPrematureEvents)();
20
+ }
21
+ }
22
+ exports.setSentryClientForLogging = setSentryClientForLogging;
@@ -5,7 +5,10 @@ export * from './event-context/event-severity';
5
5
  export * from './event-context/extra-context.error';
6
6
  export * from './event-context/extra-event-context';
7
7
  export * from './init-sentry/base-sentry-init';
8
+ export * from './logging/attach-tags';
9
+ export * from './logging/handle-error';
10
+ export * from './logging/send-log';
11
+ export * from './logging/sentry-client-for-logging';
8
12
  export * from './processing/event-processor';
9
13
  export * from './processing/handle-sentry-send';
10
14
  export * from './processing/sentry-config';
11
- export * from './processing/sentry-logger';
package/dist/esm/index.js CHANGED
@@ -5,7 +5,10 @@ export * from './event-context/event-severity';
5
5
  export * from './event-context/extra-context.error';
6
6
  export * from './event-context/extra-event-context';
7
7
  export * from './init-sentry/base-sentry-init';
8
+ export * from './logging/attach-tags';
9
+ export * from './logging/handle-error';
10
+ export * from './logging/send-log';
11
+ export * from './logging/sentry-client-for-logging';
8
12
  export * from './processing/event-processor';
9
13
  export * from './processing/handle-sentry-send';
10
14
  export * from './processing/sentry-config';
11
- export * from './processing/sentry-logger';
@@ -0,0 +1,11 @@
1
+ import type { setTags } from '@sentry/core';
2
+ /** A list of tag names as keys and their values. Set a tag to undefined to clear it. */
3
+ export type SentryTags = Parameters<typeof setTags>[0];
4
+ /**
5
+ * Set tags for all future Sentry event handling (errors and logs). This can be called before
6
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
7
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
8
+ *
9
+ * Similar to handleError and sendLog.
10
+ */
11
+ export declare function attachSentryTags(tags: SentryTags): undefined;
@@ -0,0 +1,22 @@
1
+ import { addPrematureEvent } from './premature-events';
2
+ import { sentryClientForLogging } from './sentry-client-for-logging';
3
+ /**
4
+ * Set tags for all future Sentry event handling (errors and logs). This can be called before
5
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
6
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
7
+ *
8
+ * Similar to handleError and sendLog.
9
+ */
10
+ export function attachSentryTags(tags) {
11
+ try {
12
+ if (!sentryClientForLogging) {
13
+ addPrematureEvent({ callback: attachSentryTags, inputs: [tags] });
14
+ return;
15
+ }
16
+ sentryClientForLogging.setTags(tags);
17
+ }
18
+ catch (caught) {
19
+ console.error('Error while trying to attach Sentry tags:', caught);
20
+ return undefined;
21
+ }
22
+ }
@@ -0,0 +1,9 @@
1
+ import { EventExtraContext } from '../event-context/event-context';
2
+ /**
3
+ * Record an error to Sentry without throwing it. This can be called before
4
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
5
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
6
+ *
7
+ * Similar to attachSentryTags and sendLog.
8
+ */
9
+ export declare function handleError(error: unknown, extraContext?: EventExtraContext): string | undefined;
@@ -0,0 +1,35 @@
1
+ import { convertEventDetailsToSentryContext, } from '../event-context/event-context';
2
+ import { EventSeverityEnum } from '../event-context/event-severity';
3
+ import { addPrematureEvent } from './premature-events';
4
+ import { sentryClientForLogging } from './sentry-client-for-logging';
5
+ /**
6
+ * Record an error to Sentry without throwing it. This can be called before
7
+ * setSentryClientForLogging has been called, as sentry-vir will buffer all events. However,
8
+ * setSentryClientForLogging must be called once at some point to actually clear that buffer.
9
+ *
10
+ * Similar to attachSentryTags and sendLog.
11
+ */
12
+ export function handleError(error, extraContext) {
13
+ try {
14
+ if (!sentryClientForLogging) {
15
+ addPrematureEvent({
16
+ callback: handleError,
17
+ inputs: [
18
+ error,
19
+ extraContext,
20
+ ],
21
+ });
22
+ return undefined;
23
+ }
24
+ const scopeContext = convertEventDetailsToSentryContext({
25
+ extraContext,
26
+ severity: EventSeverityEnum.Error,
27
+ });
28
+ const eventId = sentryClientForLogging.captureException(error, scopeContext);
29
+ return eventId;
30
+ }
31
+ catch (caught) {
32
+ console.error('Error while trying to handle error with Sentry:', caught);
33
+ return undefined;
34
+ }
35
+ }
@@ -0,0 +1,8 @@
1
+ import { AnyFunction } from '@augment-vir/common';
2
+ /** An event that was triggered before setSentryClientForLogging was called. */
3
+ export type PrematureEvent<EntryPointFunction extends AnyFunction = AnyFunction> = {
4
+ callback: EntryPointFunction;
5
+ inputs: Parameters<EntryPointFunction>;
6
+ };
7
+ export declare function sendPrematureEvents(): void;
8
+ export declare function addPrematureEvent(prematureEvent: PrematureEvent): void;
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Used to store events before the Sentry client is setup. This is exported for testing purposes
3
+ * only, you don't need to do anything with this.
4
+ */
5
+ const prematureSentryEvents = [];
6
+ export function sendPrematureEvents() {
7
+ while (prematureSentryEvents.length) {
8
+ try {
9
+ const prematureEvent = prematureSentryEvents.pop();
10
+ if (!prematureEvent) {
11
+ return;
12
+ }
13
+ prematureEvent.callback(...prematureEvent.inputs);
14
+ }
15
+ catch (caught) {
16
+ console.error('error while trying to send premature sentry events:', caught);
17
+ }
18
+ }
19
+ }
20
+ export function addPrematureEvent(prematureEvent) {
21
+ prematureSentryEvents.push(prematureEvent);
22
+ }
@@ -0,0 +1,19 @@
1
+ import { Event as SentryEvent } from '@sentry/types';
2
+ import { EventDetails, EventExtraContext } from '../event-context/event-context';
3
+ /**
4
+ * Send non-error events to Sentry. This can be called before setSentryClientForLogging has been
5
+ * called, as sentry-vir will buffer all events. However, setSentryClientForLogging must be called
6
+ * once at some point to actually clear that buffer.
7
+ *
8
+ * Similar to attachSentryTags and handleError.
9
+ */
10
+ export declare const sendLog: {
11
+ /** Sends an even to Sentry with debug severity. */
12
+ readonly debug: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
13
+ /** Sends an even to Sentry with info severity. */
14
+ readonly info: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
15
+ /** Sends an even to Sentry with warning severity. */
16
+ readonly warning: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
17
+ };
18
+ declare function sendLogToSentry(logInfo: string | Omit<SentryEvent, 'extra' | 'level'>, eventDetails: EventDetails): string | undefined;
19
+ export {};
@@ -0,0 +1,54 @@
1
+ import { isRuntimeTypeOf } from '@augment-vir/common';
2
+ import { convertEventDetailsToSentryContext, } from '../event-context/event-context';
3
+ import { EventSeverityEnum } from '../event-context/event-severity';
4
+ import { addPrematureEvent } from './premature-events';
5
+ import { sentryClientForLogging } from './sentry-client-for-logging';
6
+ /**
7
+ * Send non-error events to Sentry. This can be called before setSentryClientForLogging has been
8
+ * called, as sentry-vir will buffer all events. However, setSentryClientForLogging must be called
9
+ * once at some point to actually clear that buffer.
10
+ *
11
+ * Similar to attachSentryTags and handleError.
12
+ */
13
+ export const sendLog = {
14
+ /** Sends an even to Sentry with debug severity. */
15
+ [EventSeverityEnum.Debug]: wrapLogWithSeverity(EventSeverityEnum.Debug),
16
+ /** Sends an even to Sentry with info severity. */
17
+ [EventSeverityEnum.Info]: wrapLogWithSeverity(EventSeverityEnum.Info),
18
+ /** Sends an even to Sentry with warning severity. */
19
+ [EventSeverityEnum.Warning]: wrapLogWithSeverity(EventSeverityEnum.Warning),
20
+ };
21
+ function wrapLogWithSeverity(severity) {
22
+ return (info, extraContext) => {
23
+ return sendLogToSentry(info, {
24
+ extraContext,
25
+ severity,
26
+ });
27
+ };
28
+ }
29
+ function sendLogToSentry(logInfo, eventDetails) {
30
+ try {
31
+ if (!sentryClientForLogging) {
32
+ addPrematureEvent({
33
+ callback: sendLogToSentry,
34
+ inputs: [
35
+ logInfo,
36
+ eventDetails,
37
+ ],
38
+ });
39
+ return undefined;
40
+ }
41
+ const scopeContext = convertEventDetailsToSentryContext(eventDetails);
42
+ const eventId = isRuntimeTypeOf(logInfo, 'string')
43
+ ? sentryClientForLogging.captureMessage(logInfo, scopeContext)
44
+ : sentryClientForLogging.captureEvent({
45
+ ...logInfo,
46
+ ...scopeContext,
47
+ });
48
+ return eventId;
49
+ }
50
+ catch (caught) {
51
+ console.error('Error while trying to send Sentry log:', caught);
52
+ return undefined;
53
+ }
54
+ }
@@ -0,0 +1,21 @@
1
+ import { MaybePromise } from '@augment-vir/common';
2
+ import { SentryDep } from '../env/execution-env';
3
+ /** The bare minimum Sentry client needed for logging events. */
4
+ export type SentryClientForLogging = Pick<SentryDep, 'captureMessage' | 'captureException' | 'captureEvent' | 'setTags'>;
5
+ /**
6
+ * Internal sentry client used for logging. Use setSentryClientForLogging to set this. Allows
7
+ * handleError, setTags, and sendLog to be called before the Sentry client has been setup yet, as
8
+ * once this is set all events fired from those functions will get sent to Sentry.
9
+ */
10
+ export declare let sentryClientForLogging: SentryClientForLogging | undefined;
11
+ /**
12
+ * Asynchronously set the Sentry client for logging. When this is called, any events that were
13
+ * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
14
+ * asynchronous Sentry client setup but synchronous log and error handling.
15
+ *
16
+ * This should be called as soon as possible after you have a return value from initSentry.
17
+ *
18
+ * This can be safely called multiple times (to overwrite the previously set Sentry client) because
19
+ * previous events won't be handled multiple times.
20
+ */
21
+ export declare function setSentryClientForLogging(client: MaybePromise<SentryClientForLogging>): Promise<void>;
@@ -0,0 +1,24 @@
1
+ import { sendPrematureEvents } from './premature-events';
2
+ /**
3
+ * Internal sentry client used for logging. Use setSentryClientForLogging to set this. Allows
4
+ * handleError, setTags, and sendLog to be called before the Sentry client has been setup yet, as
5
+ * once this is set all events fired from those functions will get sent to Sentry.
6
+ */
7
+ export let sentryClientForLogging;
8
+ /**
9
+ * Asynchronously set the Sentry client for logging. When this is called, any events that were
10
+ * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
11
+ * asynchronous Sentry client setup but synchronous log and error handling.
12
+ *
13
+ * This should be called as soon as possible after you have a return value from initSentry.
14
+ *
15
+ * This can be safely called multiple times (to overwrite the previously set Sentry client) because
16
+ * previous events won't be handled multiple times.
17
+ */
18
+ export async function setSentryClientForLogging(client) {
19
+ const hadClientBefore = !!sentryClientForLogging;
20
+ sentryClientForLogging = await client;
21
+ if (!hadClientBefore) {
22
+ sendPrematureEvents();
23
+ }
24
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sentry-vir",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "keywords": [
5
5
  "config",
6
6
  "helper",
@@ -23,9 +23,6 @@
23
23
  "main": "dist/cjs/index.js",
24
24
  "module": "dist/esm/index.js",
25
25
  "types": "dist/esm/index.d.ts",
26
- "workspaces": [
27
- "./"
28
- ],
29
26
  "scripts": {
30
27
  "compile": "rm -rf dist && tsc --project tsconfig.json && npm i && tsc --project tsconfig.cjs.json",
31
28
  "docs": "virmator docs",
@@ -1,40 +0,0 @@
1
- import { AnyFunction, MaybePromise } from '@augment-vir/common';
2
- import type { Event as SentryEvent } from '@sentry/types';
3
- import type { SentryDep } from '../env/execution-env';
4
- import { EventDetails, EventExtraContext } from '../event-context/event-context';
5
- /** The bare minimum Sentry client needed for logging events. */
6
- export type SentryClientForLogging = Pick<SentryDep, 'captureMessage' | 'captureException' | 'captureEvent'>;
7
- /** An event that was triggered before setSentryClientForLogging was called. */
8
- export type PrematureEvent<EntryPointFunction extends AnyFunction = AnyFunction> = {
9
- entryPoint: EntryPointFunction;
10
- inputs: Parameters<EntryPointFunction>;
11
- };
12
- /**
13
- * Used to store events before the Sentry client is setup. This is exported for testing purposes
14
- * only, you don't need to do anything with this.
15
- */
16
- export declare const prematureSentryEvents: PrematureEvent[];
17
- /**
18
- * Asynchronously set the Sentry client for logging. When this is called, any events that were
19
- * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
20
- * asynchronous Sentry client setup but synchronous log and error handling.
21
- *
22
- * This should be called as soon as possible after you have a return value from initSentry.
23
- *
24
- * This can be safely called multiple times (to overwrite the previously set Sentry client) because
25
- * previous events won't be handled multiple times.
26
- */
27
- export declare function setSentryClientForLogging(client: MaybePromise<SentryClientForLogging>): Promise<void>;
28
- /** Record an error to Sentry without throwing it. */
29
- export declare function handleError(error: unknown, extraContext?: EventExtraContext): string | undefined;
30
- /** Send non-error events to Sentry. */
31
- export declare const sendLog: {
32
- /** Sends an even to Sentry with debug severity. */
33
- readonly debug: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
34
- /** Sends an even to Sentry with info severity. */
35
- readonly info: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
36
- /** Sends an even to Sentry with warning severity. */
37
- readonly warning: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
38
- };
39
- declare function sendLogToSentry(logInfo: string | Omit<SentryEvent, 'extra' | 'level'>, eventDetails: EventDetails): string | undefined;
40
- export {};
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.sendLog = exports.handleError = exports.setSentryClientForLogging = exports.prematureSentryEvents = void 0;
4
- const common_1 = require("@augment-vir/common");
5
- const event_context_1 = require("../event-context/event-context");
6
- const event_severity_1 = require("../event-context/event-severity");
7
- let sentryClientForLogging;
8
- /**
9
- * Used to store events before the Sentry client is setup. This is exported for testing purposes
10
- * only, you don't need to do anything with this.
11
- */
12
- exports.prematureSentryEvents = [];
13
- /**
14
- * Asynchronously set the Sentry client for logging. When this is called, any events that were
15
- * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
16
- * asynchronous Sentry client setup but synchronous log and error handling.
17
- *
18
- * This should be called as soon as possible after you have a return value from initSentry.
19
- *
20
- * This can be safely called multiple times (to overwrite the previously set Sentry client) because
21
- * previous events won't be handled multiple times.
22
- */
23
- async function setSentryClientForLogging(client) {
24
- const hadClientBefore = !!sentryClientForLogging;
25
- sentryClientForLogging = await client;
26
- if (!hadClientBefore) {
27
- sendPrematureEvents();
28
- }
29
- }
30
- exports.setSentryClientForLogging = setSentryClientForLogging;
31
- function sendPrematureEvents() {
32
- while (exports.prematureSentryEvents.length) {
33
- try {
34
- const prematureLog = exports.prematureSentryEvents.pop();
35
- if (!prematureLog) {
36
- return;
37
- }
38
- prematureLog.entryPoint(...prematureLog.inputs);
39
- }
40
- catch (caught) {
41
- console.error('error while trying to send sentry logs:', caught);
42
- }
43
- }
44
- }
45
- /** Record an error to Sentry without throwing it. */
46
- function handleError(error, extraContext) {
47
- try {
48
- if (!sentryClientForLogging) {
49
- exports.prematureSentryEvents.push({
50
- entryPoint: handleError,
51
- inputs: [
52
- error,
53
- extraContext,
54
- ],
55
- });
56
- return undefined;
57
- }
58
- const scopeContext = (0, event_context_1.convertEventDetailsToSentryContext)({
59
- extraContext,
60
- severity: event_severity_1.EventSeverityEnum.Error,
61
- });
62
- const eventId = sentryClientForLogging.captureException(error, scopeContext);
63
- return eventId;
64
- }
65
- catch (caught) {
66
- console.error('error while trying to handle error:', caught);
67
- return undefined;
68
- }
69
- }
70
- exports.handleError = handleError;
71
- /** Send non-error events to Sentry. */
72
- exports.sendLog = {
73
- /** Sends an even to Sentry with debug severity. */
74
- [event_severity_1.EventSeverityEnum.Debug]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Debug),
75
- /** Sends an even to Sentry with info severity. */
76
- [event_severity_1.EventSeverityEnum.Info]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Info),
77
- /** Sends an even to Sentry with warning severity. */
78
- [event_severity_1.EventSeverityEnum.Warning]: wrapLogWithSeverity(event_severity_1.EventSeverityEnum.Warning),
79
- };
80
- function wrapLogWithSeverity(severity) {
81
- return (info, extraContext) => {
82
- return sendLogToSentry(info, {
83
- extraContext,
84
- severity,
85
- });
86
- };
87
- }
88
- function sendLogToSentry(logInfo, eventDetails) {
89
- try {
90
- if (!sentryClientForLogging) {
91
- exports.prematureSentryEvents.push({
92
- entryPoint: sendLogToSentry,
93
- inputs: [
94
- logInfo,
95
- eventDetails,
96
- ],
97
- });
98
- return undefined;
99
- }
100
- const scopeContext = (0, event_context_1.convertEventDetailsToSentryContext)(eventDetails);
101
- const eventId = (0, common_1.isRuntimeTypeOf)(logInfo, 'string')
102
- ? sentryClientForLogging.captureMessage(logInfo, scopeContext)
103
- : sentryClientForLogging.captureEvent({
104
- ...logInfo,
105
- ...scopeContext,
106
- });
107
- return eventId;
108
- }
109
- catch (caught) {
110
- console.error('error while trying to send log:', caught);
111
- return undefined;
112
- }
113
- }
@@ -1,40 +0,0 @@
1
- import { AnyFunction, MaybePromise } from '@augment-vir/common';
2
- import type { Event as SentryEvent } from '@sentry/types';
3
- import type { SentryDep } from '../env/execution-env';
4
- import { EventDetails, EventExtraContext } from '../event-context/event-context';
5
- /** The bare minimum Sentry client needed for logging events. */
6
- export type SentryClientForLogging = Pick<SentryDep, 'captureMessage' | 'captureException' | 'captureEvent'>;
7
- /** An event that was triggered before setSentryClientForLogging was called. */
8
- export type PrematureEvent<EntryPointFunction extends AnyFunction = AnyFunction> = {
9
- entryPoint: EntryPointFunction;
10
- inputs: Parameters<EntryPointFunction>;
11
- };
12
- /**
13
- * Used to store events before the Sentry client is setup. This is exported for testing purposes
14
- * only, you don't need to do anything with this.
15
- */
16
- export declare const prematureSentryEvents: PrematureEvent[];
17
- /**
18
- * Asynchronously set the Sentry client for logging. When this is called, any events that were
19
- * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
20
- * asynchronous Sentry client setup but synchronous log and error handling.
21
- *
22
- * This should be called as soon as possible after you have a return value from initSentry.
23
- *
24
- * This can be safely called multiple times (to overwrite the previously set Sentry client) because
25
- * previous events won't be handled multiple times.
26
- */
27
- export declare function setSentryClientForLogging(client: MaybePromise<SentryClientForLogging>): Promise<void>;
28
- /** Record an error to Sentry without throwing it. */
29
- export declare function handleError(error: unknown, extraContext?: EventExtraContext): string | undefined;
30
- /** Send non-error events to Sentry. */
31
- export declare const sendLog: {
32
- /** Sends an even to Sentry with debug severity. */
33
- readonly debug: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
34
- /** Sends an even to Sentry with info severity. */
35
- readonly info: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
36
- /** Sends an even to Sentry with warning severity. */
37
- readonly warning: (info: Parameters<typeof sendLogToSentry>[0], extraContext?: EventExtraContext) => string | undefined;
38
- };
39
- declare function sendLogToSentry(logInfo: string | Omit<SentryEvent, 'extra' | 'level'>, eventDetails: EventDetails): string | undefined;
40
- export {};
@@ -1,108 +0,0 @@
1
- import { isRuntimeTypeOf } from '@augment-vir/common';
2
- import { convertEventDetailsToSentryContext, } from '../event-context/event-context';
3
- import { EventSeverityEnum } from '../event-context/event-severity';
4
- let sentryClientForLogging;
5
- /**
6
- * Used to store events before the Sentry client is setup. This is exported for testing purposes
7
- * only, you don't need to do anything with this.
8
- */
9
- export const prematureSentryEvents = [];
10
- /**
11
- * Asynchronously set the Sentry client for logging. When this is called, any events that were
12
- * triggered beforehand are handled. Thus, this set can be done at any time, allowing for
13
- * asynchronous Sentry client setup but synchronous log and error handling.
14
- *
15
- * This should be called as soon as possible after you have a return value from initSentry.
16
- *
17
- * This can be safely called multiple times (to overwrite the previously set Sentry client) because
18
- * previous events won't be handled multiple times.
19
- */
20
- export async function setSentryClientForLogging(client) {
21
- const hadClientBefore = !!sentryClientForLogging;
22
- sentryClientForLogging = await client;
23
- if (!hadClientBefore) {
24
- sendPrematureEvents();
25
- }
26
- }
27
- function sendPrematureEvents() {
28
- while (prematureSentryEvents.length) {
29
- try {
30
- const prematureLog = prematureSentryEvents.pop();
31
- if (!prematureLog) {
32
- return;
33
- }
34
- prematureLog.entryPoint(...prematureLog.inputs);
35
- }
36
- catch (caught) {
37
- console.error('error while trying to send sentry logs:', caught);
38
- }
39
- }
40
- }
41
- /** Record an error to Sentry without throwing it. */
42
- export function handleError(error, extraContext) {
43
- try {
44
- if (!sentryClientForLogging) {
45
- prematureSentryEvents.push({
46
- entryPoint: handleError,
47
- inputs: [
48
- error,
49
- extraContext,
50
- ],
51
- });
52
- return undefined;
53
- }
54
- const scopeContext = convertEventDetailsToSentryContext({
55
- extraContext,
56
- severity: EventSeverityEnum.Error,
57
- });
58
- const eventId = sentryClientForLogging.captureException(error, scopeContext);
59
- return eventId;
60
- }
61
- catch (caught) {
62
- console.error('error while trying to handle error:', caught);
63
- return undefined;
64
- }
65
- }
66
- /** Send non-error events to Sentry. */
67
- export const sendLog = {
68
- /** Sends an even to Sentry with debug severity. */
69
- [EventSeverityEnum.Debug]: wrapLogWithSeverity(EventSeverityEnum.Debug),
70
- /** Sends an even to Sentry with info severity. */
71
- [EventSeverityEnum.Info]: wrapLogWithSeverity(EventSeverityEnum.Info),
72
- /** Sends an even to Sentry with warning severity. */
73
- [EventSeverityEnum.Warning]: wrapLogWithSeverity(EventSeverityEnum.Warning),
74
- };
75
- function wrapLogWithSeverity(severity) {
76
- return (info, extraContext) => {
77
- return sendLogToSentry(info, {
78
- extraContext,
79
- severity,
80
- });
81
- };
82
- }
83
- function sendLogToSentry(logInfo, eventDetails) {
84
- try {
85
- if (!sentryClientForLogging) {
86
- prematureSentryEvents.push({
87
- entryPoint: sendLogToSentry,
88
- inputs: [
89
- logInfo,
90
- eventDetails,
91
- ],
92
- });
93
- return undefined;
94
- }
95
- const scopeContext = convertEventDetailsToSentryContext(eventDetails);
96
- const eventId = isRuntimeTypeOf(logInfo, 'string')
97
- ? sentryClientForLogging.captureMessage(logInfo, scopeContext)
98
- : sentryClientForLogging.captureEvent({
99
- ...logInfo,
100
- ...scopeContext,
101
- });
102
- return eventId;
103
- }
104
- catch (caught) {
105
- console.error('error while trying to send log:', caught);
106
- return undefined;
107
- }
108
- }