@wixc3/common
Advanced tools
Comparing version 15.1.0 to 15.1.1
@@ -62,9 +62,2 @@ /** | ||
export declare function isErrorLikeObject(error: unknown): error is Error; | ||
export declare const TRACE_DEFAULTS: { | ||
noTraceMessage: string; | ||
skipLines: number; | ||
filterPattern: RegExp | null; | ||
}; | ||
export declare function getStackTrace(options?: Partial<typeof TRACE_DEFAULTS>): string; | ||
export declare function errorWithTrace(message: string, trace: string, options?: ErrorOptions): Error; | ||
//# sourceMappingURL=errors.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.errorWithTrace = exports.getStackTrace = exports.TRACE_DEFAULTS = exports.isErrorLikeObject = exports.errorToPlainObject = exports.stringifyErrorStack = exports.UnreachableCaseError = exports.ErrorWithCode = exports.getErrorCode = exports.toError = void 0; | ||
exports.isErrorLikeObject = exports.errorToPlainObject = exports.stringifyErrorStack = exports.UnreachableCaseError = exports.ErrorWithCode = exports.getErrorCode = exports.toError = void 0; | ||
const objects_1 = require("./objects"); | ||
@@ -91,21 +91,2 @@ /** | ||
exports.isErrorLikeObject = isErrorLikeObject; | ||
exports.TRACE_DEFAULTS = { | ||
noTraceMessage: 'no stack trace', | ||
skipLines: 2, | ||
filterPattern: null, | ||
}; | ||
function getStackTrace(options = exports.TRACE_DEFAULTS) { | ||
var _a; | ||
const { filterPattern, noTraceMessage, skipLines } = { ...exports.TRACE_DEFAULTS, ...options }; | ||
const { stack } = new Error(); | ||
return ((_a = stack === null || stack === void 0 ? void 0 : stack.split('\n').slice(skipLines).filter((l) => (filterPattern ? filterPattern.test(l) : true)).join('\n')) !== null && _a !== void 0 ? _a : noTraceMessage); | ||
} | ||
exports.getStackTrace = getStackTrace; | ||
function errorWithTrace(message, trace, options) { | ||
var _a, _b; | ||
const err = new Error(message, options); | ||
err.stack = ((_b = (_a = err.stack) === null || _a === void 0 ? void 0 : _a.split('\n')[0]) !== null && _b !== void 0 ? _b : `Error: ${message}`) + '\n' + trace; | ||
return err; | ||
} | ||
exports.errorWithTrace = errorWithTrace; | ||
//# sourceMappingURL=errors.js.map |
@@ -62,9 +62,2 @@ /** | ||
export declare function isErrorLikeObject(error: unknown): error is Error; | ||
export declare const TRACE_DEFAULTS: { | ||
noTraceMessage: string; | ||
skipLines: number; | ||
filterPattern: RegExp | null; | ||
}; | ||
export declare function getStackTrace(options?: Partial<typeof TRACE_DEFAULTS>): string; | ||
export declare function errorWithTrace(message: string, trace: string, options?: ErrorOptions): Error; | ||
//# sourceMappingURL=errors.d.ts.map |
@@ -81,19 +81,2 @@ import { isObject } from './objects'; | ||
} | ||
export const TRACE_DEFAULTS = { | ||
noTraceMessage: 'no stack trace', | ||
skipLines: 2, | ||
filterPattern: null, | ||
}; | ||
export function getStackTrace(options = TRACE_DEFAULTS) { | ||
var _a; | ||
const { filterPattern, noTraceMessage, skipLines } = { ...TRACE_DEFAULTS, ...options }; | ||
const { stack } = new Error(); | ||
return ((_a = stack === null || stack === void 0 ? void 0 : stack.split('\n').slice(skipLines).filter((l) => (filterPattern ? filterPattern.test(l) : true)).join('\n')) !== null && _a !== void 0 ? _a : noTraceMessage); | ||
} | ||
export function errorWithTrace(message, trace, options) { | ||
var _a, _b; | ||
const err = new Error(message, options); | ||
err.stack = ((_b = (_a = err.stack) === null || _a === void 0 ? void 0 : _a.split('\n')[0]) !== null && _b !== void 0 ? _b : `Error: ${message}`) + '\n' + trace; | ||
return err; | ||
} | ||
//# sourceMappingURL=errors.js.map |
{ | ||
"name": "@wixc3/common", | ||
"version": "15.1.0", | ||
"version": "15.1.1", | ||
"description": "Common utils, usable in all environments", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -90,25 +90,1 @@ import { isObject } from './objects'; | ||
} | ||
export const TRACE_DEFAULTS = { | ||
noTraceMessage: 'no stack trace', | ||
skipLines: 2, | ||
filterPattern: null as RegExp | null, | ||
}; | ||
export function getStackTrace(options: Partial<typeof TRACE_DEFAULTS> = TRACE_DEFAULTS) { | ||
const { filterPattern, noTraceMessage, skipLines } = { ...TRACE_DEFAULTS, ...options }; | ||
const { stack } = new Error(); | ||
return ( | ||
stack | ||
?.split('\n') | ||
.slice(skipLines) | ||
.filter((l) => (filterPattern ? filterPattern.test(l) : true)) | ||
.join('\n') ?? noTraceMessage | ||
); | ||
} | ||
export function errorWithTrace(message: string, trace: string, options?: ErrorOptions) { | ||
const err = new Error(message, options); | ||
err.stack = (err.stack?.split('\n')[0] ?? `Error: ${message}`) + '\n' + trace; | ||
return err; | ||
} |
import chai, { expect } from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
import { isErrorLikeObject, getStackTrace, errorWithTrace } from '../errors'; | ||
import { deferred } from 'promise-assist'; | ||
import { isErrorLikeObject } from '../errors'; | ||
@@ -24,37 +23,1 @@ chai.use(sinonChai); | ||
}); | ||
describe('getStackTrace', () => { | ||
it('default', () => { | ||
const stack = getStackTrace(); | ||
expect(stack.split('\n')[0]).to.match(/.*errors\.unit\.ts/); | ||
}); | ||
it('removes this package internals from stacktrace', () => { | ||
const stack = getStackTrace({ skipLines: 1 }); | ||
expect(stack.split('\n')[0]).to.match(/.*errors\.ts/); | ||
}); | ||
it(`removes lines that don't match filterPattern`, () => { | ||
const stack = getStackTrace({ filterPattern: /.*errors\.unit\.ts/ }); | ||
expect(stack.split('\n')).to.have.lengthOf(1); | ||
expect(stack).to.match(/.*errors\.unit\.ts/); | ||
}); | ||
}); | ||
describe('errorWithTrace', () => { | ||
it('returns an error has the given stack trace', async () => { | ||
const trace = getStackTrace(); | ||
const err = deferred<Error>(); | ||
setTimeout(() => { | ||
try { | ||
throw errorWithTrace('err', trace, { cause: 'test' }); | ||
} catch (e) { | ||
err.resolve(e as Error); | ||
} | ||
}, 1); | ||
const { stack, message, cause } = await err.promise; | ||
expect(stack).to.match(/Error: err\n\s+at Context\.<anonymous>.*errors\.unit\.ts.*\n/); | ||
expect(message).to.equal('err'); | ||
expect(cause).to.equal('test'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
364075
6210