Skip to content

Commit

Permalink
Standardize serializer exports
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Dec 3, 2024
1 parent 6fc8e71 commit f1dbff8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ Built-in serializer functions you may use in the `serializers` option.

Serializes an `Error` object.

#### datadogSerializer
#### datadogError

Serializes an `Error` object for the purpose of sending it to Datadog, adding a `kind` property based on the error class name.

Expand Down
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

const { cloneDeep, cloneDeepWith, escapeRegExp, get, set } = require('lodash');
const { serializeError } = require('serialize-error');
const { serializeError: errorSerializer } = require('serialize-error');
const stringify = require('json-stringify-safe');
const traverse = require('traverse');

Expand Down Expand Up @@ -217,18 +217,18 @@ module.exports.anonymizer = (
* Default serializer for Datadog.
*/

function datadogSerializer(error) {
function datadogErrorSerializer(error) {
return {
...error,
kind: error.name || 'Error'
};
}

/**
* Module exports `defaultSerializers`.
* Module exports `serializers`.
*/

module.exports.defaultSerializers = {
datadogSerializer,
error: serializeError
module.exports.serializers = {
datadogError: datadogErrorSerializer,
error: errorSerializer
};
36 changes: 16 additions & 20 deletions test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
* Module dependencies.
*/

const {
anonymizer,
defaultSerializers: { datadogSerializer }
} = require('src');
const { anonymizer, serializers: builtinSerializers } = require('src');
const { generateObjectSample, generateObjectSamplePaths } = require('./benchmark/samples');
const { serializeError } = require('serialize-error');

/**
* Test `Anonymizer`.
Expand Down Expand Up @@ -534,7 +530,7 @@ describe('Anonymizer', () => {
const error = new Error('foobar');
const foobar = jest.fn(() => 'bii');
const foobiz = jest.fn(() => 'bzz');
const fooerror = jest.fn(serializeError);
const fooerror = jest.fn(builtinSerializers.error);
const whitelist = ['**'];
const serializers = [
{ path: 'bar.foo', serializer: foobiz },
Expand Down Expand Up @@ -594,7 +590,7 @@ describe('Anonymizer', () => {
}

const error = new ValidationError('foobar');
const serializer = jest.fn(datadogSerializer);
const serializer = jest.fn(builtinSerializers.datadogError);
const serializers = [{ path: 'error', serializer }];
const whitelist = ['error.foo', 'error.kind', 'error.message', 'error.name', 'error.stack'];
const anonymize = anonymizer({ whitelist }, { serializers });
Expand Down Expand Up @@ -629,7 +625,7 @@ describe('Anonymizer', () => {
}

const error = new ErrorTwo('foobar');
const serializer = jest.fn(datadogSerializer);
const serializer = jest.fn(builtinSerializers.datadogError);
const serializers = [{ path: 'error', serializer }];
const whitelist = ['error.foo', 'error.kind', 'error.message', 'error.name', 'error.stack'];
const anonymize = anonymizer({ whitelist }, { serializers });
Expand Down Expand Up @@ -657,7 +653,7 @@ describe('Anonymizer', () => {

const error = new ValidationError('foobar');
const serializer = jest.fn(value => {
const serialized = datadogSerializer(value);
const serialized = builtinSerializers.datadogError(value);

value.foo.for = 'bar';

Expand Down Expand Up @@ -691,7 +687,7 @@ describe('Anonymizer', () => {

const error = new ValidationError('foobar');
const serializer = jest.fn(value => {
const serialized = datadogSerializer(value);
const serialized = builtinSerializers.datadogError(value);

value.foo.for.bar.foo = 'bar';

Expand Down Expand Up @@ -735,10 +731,10 @@ describe('Anonymizer', () => {
expect(serializer).toHaveBeenCalledTimes(2);
});

describe('defaultSerializers', () => {
it('should serialize errors when `serializeError()` is applied', () => {
describe('built-in', () => {
it('should serialize errors when `builtinSerializers.error()` is applied', () => {
const error = new Error('foobar');
const serializer = jest.fn(serializeError);
const serializer = jest.fn(builtinSerializers.error);
const serializers = [
{ path: 'e', serializer },
{ path: 'err', serializer },
Expand Down Expand Up @@ -775,9 +771,9 @@ describe('Anonymizer', () => {
expect(result.foo).toEqual('bar');
});

it('should serialize errors when `datadogSerializer()` is applied', () => {
it('should serialize errors when `builtinSerializers.datadogError()` is applied', () => {
const error = new Error('foobar');
const serializer = jest.fn(datadogSerializer);
const serializer = jest.fn(builtinSerializers.datadogError);
const serializers = [
{ path: 'err', serializer },
{ path: 'error', serializer }
Expand Down Expand Up @@ -881,7 +877,7 @@ describe('Anonymizer', () => {
it('should run a sample with `32768` properties in less than `150` ms', () => {
const depth = 10;
const data = generateObjectSample({ depth });
const anonymize = anonymizer({ blacklist: ['*'] });
const anonymize = anonymizer({});
const startTime = process.hrtime();

anonymize(data);
Expand All @@ -897,7 +893,7 @@ describe('Anonymizer', () => {
const data = generateObjectSample({ depth });
const serializer = jest.fn(() => 'bii');
const serializers = generateObjectSamplePaths({ depth }).map(path => ({ path, serializer }));
const anonymize = anonymizer({ blacklist: ['*'] }, { serializers });
const anonymize = anonymizer({}, { serializers });
const startTime = process.hrtime();

anonymize(data);
Expand All @@ -909,12 +905,12 @@ describe('Anonymizer', () => {
expect(serializer).toHaveBeenCalledTimes(32768);
});

it('should call `serializeError` in all `32768` properties in less than `175` ms', () => {
it('should call `builtinSerializers.error` in all `32768` properties in less than `175` ms', () => {
const depth = 10;
const data = generateObjectSample({ depth, leafValue: () => new Error('foobar') });
const serializer = jest.fn(serializeError);
const serializer = jest.fn(builtinSerializers.error);
const serializers = generateObjectSamplePaths({ depth }).map(path => ({ path, serializer }));
const anonymize = anonymizer({ blacklist: ['*'] }, { serializers });
const anonymize = anonymizer({}, { serializers });
const startTime = process.hrtime();

anonymize(data);
Expand Down

0 comments on commit f1dbff8

Please sign in to comment.