Skip to content

Commit

Permalink
Renamed parametrize to parameterize
Browse files Browse the repository at this point in the history
  • Loading branch information
AleshaOleg committed Oct 13, 2023
1 parent 7b27a1e commit d1dce8e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { parameterize } from '@sentry/utils';

const x = 'first';
const y = 'second';

Sentry.captureMessage(parameterize`This is a log statement with ${x} and ${y} params`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest } from '../../../../utils/helpers';

sentryTest('should capture a paramaterized representation of the message', async ({ getLocalTestPath, page }) => {
const url = await getLocalTestPath({ testDir: __dirname });

const eventData = await getFirstSentryEnvelopeRequest<Event>(page, url);

expect(eventData.logentry).toStrictEqual({
message: 'This is a log statement with %s and %s params',
params: ['first', 'second'],
});
});
13 changes: 11 additions & 2 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { getCurrentHub } from '@sentry/core';
import type { Event, EventHint, Exception, Severity, SeverityLevel, StackFrame, StackParser } from '@sentry/types';
import type {
Event,
EventHint,
Exception,
Severity,
SeverityLevel,
StackFrame,
StackParser,
ParameterizedString,
} from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
Expand Down Expand Up @@ -265,7 +274,7 @@ export function eventFromUnknownInput(
*/
export function eventFromString(
stackParser: StackParser,
input: string & { __sentry_template_string__?: string; __sentry_template_values__?: string[] },
input: ParameterizedString,
syntheticException?: Error,
attachStacktrace?: boolean,
): Event {
Expand Down
3 changes: 2 additions & 1 deletion packages/types/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Event, EventHint } from './event';
import type { EventProcessor } from './eventprocessor';
import type { Integration, IntegrationClass } from './integration';
import type { ClientOptions } from './options';
import { ParameterizedString } from './parameterize';
import type { Scope } from './scope';
import type { SdkMetadata } from './sdkmetadata';
import type { Session, SessionAggregates } from './session';
Expand Down Expand Up @@ -157,7 +158,7 @@ export interface Client<O extends ClientOptions = ClientOptions> {

/** Creates an {@link Event} from primitive inputs to `captureMessage`. */
eventFromMessage(
message: string,
message: ParameterizedString,
// eslint-disable-next-line deprecation/deprecation
level?: Severity | SeverityLevel,
hint?: EventHint,
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ export type { HandlerDataFetch, HandlerDataXhr, SentryXhrData, SentryWrappedXMLH

export type { BrowserClientReplayOptions, BrowserClientProfilingOptions } from './browseroptions';
export type { CheckIn, MonitorConfig, SerializedCheckIn } from './checkin';
export type { ParameterizedString } from './parameterize';
4 changes: 4 additions & 0 deletions packages/types/src/parameterize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ParameterizedString = string & {
__sentry_template_string__?: string;
__sentry_template_values__?: string[];
};
9 changes: 5 additions & 4 deletions packages/utils/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
SeverityLevel,
StackFrame,
StackParser,
ParameterizedString,
} from '@sentry/types';

import { isError, isPlainObject } from './is';
Expand Down Expand Up @@ -119,7 +120,7 @@ export function eventFromUnknownInput(
*/
export function eventFromMessage(
stackParser: StackParser,
input: string & { __sentry_template_string__?: string; __sentry_template_values__?: string[] },
message: ParameterizedString,
// eslint-disable-next-line deprecation/deprecation
level: Severity | SeverityLevel = 'info',
hint?: EventHint,
Expand All @@ -136,15 +137,15 @@ export function eventFromMessage(
event.exception = {
values: [
{
value: input,
value: message,
stacktrace: { frames },
},
],
};
}
}

const { __sentry_template_string__, __sentry_template_values__ } = input;
const { __sentry_template_string__, __sentry_template_values__ } = message;

if (__sentry_template_string__ && __sentry_template_values__) {
event.logentry = {
Expand All @@ -154,6 +155,6 @@ export function eventFromMessage(
return event;
}

event.message = input;
event.message = message;
return event;
}
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ export * from './url';
export * from './userIntegrations';
export * from './cache';
export * from './eventbuilder';
export * from './parametrize';
export * from './parameterize';
export * from './anr';
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
export interface ParamerizedString extends String {
__sentry_template_string__: string;
__sentry_template_values__: string[];
}
import { ParameterizedString } from '@sentry/types';

/**
* Tagged template function which returns paramaterized representation of the message
* For example: parametrize`This is a log statement with ${x} and ${y} params`, would return:
* For example: parameterize`This is a log statement with ${x} and ${y} params`, would return:
* "__sentry_template_string__": "My raw message with interpreted strings like %s",
* "__sentry_template_values__": ["this"]
* @param strings An array of string values splitted between expressions
* @param values Expressions extracted from template string
* @returns String with template information in __sentry_template_string__ and __sentry_template_values__ properties
*/
export function parametrize(strings: TemplateStringsArray, ...values: string[]): ParamerizedString {
const formatted = new String(String.raw(strings, ...values)) as ParamerizedString;
export function parameterize(strings: TemplateStringsArray, ...values: string[]): ParameterizedString {
const formatted = new String(String.raw(strings, ...values)) as ParameterizedString;
formatted.__sentry_template_string__ = strings.join('\x00').replace(/%/g, '%%').replace(/\0/g, '%s');
formatted.__sentry_template_values__ = values;
return formatted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import type { ParamerizedString } from '../src/parametrize';
import { parametrize } from '../src/parametrize';
import type { ParameterizedString } from '@sentry/types';
import { parameterize } from '../src/parameterize';

describe('parametrize()', () => {
describe('parameterize()', () => {
test('works with empty string', () => {
const string = new String() as ParamerizedString;
const string = new String() as ParameterizedString;
string.__sentry_template_string__ = '';
string.__sentry_template_values__ = [];

const formatted = parametrize``;
const formatted = parameterize``;
expect(formatted.__sentry_template_string__).toEqual('');
expect(formatted.__sentry_template_values__).toEqual([]);
});

test('works as expected with template literals', () => {
const x = 'first';
const y = 'second';
const string = new String() as ParamerizedString;
const string = new String() as ParameterizedString;
string.__sentry_template_string__ = 'This is a log statement with %s and %s params';
string.__sentry_template_values__ = ['first', 'second'];

const formatted = parametrize`This is a log statement with ${x} and ${y} params`;
const formatted = parameterize`This is a log statement with ${x} and ${y} params`;
expect(formatted.__sentry_template_string__).toEqual(string.__sentry_template_string__);
expect(formatted.__sentry_template_values__).toEqual(string.__sentry_template_values__);
});
Expand Down

0 comments on commit d1dce8e

Please sign in to comment.