Skip to content

Commit

Permalink
feat: reverse stacktraces in extended errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stas-nc committed Oct 2, 2024
1 parent e0582a4 commit 5560fb8
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 29 deletions.
2 changes: 1 addition & 1 deletion ilc/client/registry/errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extendError from '@namecheap/error-extender';
import { extendError } from '../../common/utils';

const errors = {};
errors.RegistryError = extendError('RegistryError');
Expand Down
3 changes: 1 addition & 2 deletions ilc/common/CacheWrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const extendError = require('@namecheap/error-extender');
const { withTimeout } = require('./utils');
const { withTimeout, extendError } = require('./utils');

const errors = {};
errors.CacheWrapperError = extendError('CacheWrapperError', { defaultData: {} });
Expand Down
2 changes: 1 addition & 1 deletion ilc/common/guard/errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const extendError = require('@namecheap/error-extender');
const { extendError } = require('../utils');

const errors = {};

Expand Down
2 changes: 1 addition & 1 deletion ilc/common/router/errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const extendError = require('@namecheap/error-extender');
const { extendError } = require('../utils');

const errors = {};
errors.RouterError = extendError('RouterError', { defaultData: {} });
Expand Down
6 changes: 6 additions & 0 deletions ilc/common/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const errorExtender = require('@namecheap/error-extender');
const deepmerge = require('deepmerge');

function appIdToNameAndSlot(appId) {
Expand Down Expand Up @@ -76,6 +77,10 @@ async function withTimeout(promise, ms, message = 'Promise timeout') {
return Promise.race([decoratedPromise, timeoutPromise]);
}

function extendError(name, options = {}) {
return errorExtender(name, { ...options, inverse: true });
}

module.exports = {
appIdToNameAndSlot,
makeAppId,
Expand All @@ -88,4 +93,5 @@ module.exports = {
addTrailingSlashToPath,
withTimeout,
TimeoutError,
extendError,
};
8 changes: 4 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"@namecheap/error-extender": "^2.2.0",
"@namecheap/error-extender": "^2.2.1",
"@namecheap/tailorx": "^8.1.0",
"@newrelic/native-metrics": "^11.0.0",
"agentkeepalive": "^4.5.0",
Expand Down
2 changes: 1 addition & 1 deletion ilc/server/errorHandler/ErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const { StatusCodes, getReasonPhrase } = require('http-status-codes');
const safeJsonStringify = require('safe-json-stringify');
const uuidv4 = require('uuid/v4');
const extendError = require('@namecheap/error-extender');
const { extendError } = require('../../common/utils');
const config = require('config');
const { readFileSync } = require('fs');
const { setErrorData } = require('../utils/helpers');
Expand Down
2 changes: 1 addition & 1 deletion ilc/server/registry/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extendError from '@namecheap/error-extender';
import { extendError } from '../../common/utils';

export const RegistryError = extendError('RegistryError', { defaultData: {} });
export const ValidationRegistryError = extendError('ValidationRegistryError', {
Expand Down
2 changes: 1 addition & 1 deletion ilc/server/tailor/errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const extendError = require('@namecheap/error-extender');
const { extendError } = require('../../common/utils');

const errors = {};
errors.TailorError = extendError('TailorError', { defaultData: {} });
Expand Down
8 changes: 4 additions & 4 deletions registry/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"typescript": "^5.6.2"
},
"dependencies": {
"@namecheap/error-extender": "^2.2.0",
"@namecheap/error-extender": "^2.2.1",
"@newrelic/native-metrics": "^11.0.0",
"axios": "^1.7.7",
"bcrypt": "^5.1.1",
Expand Down
2 changes: 1 addition & 1 deletion registry/server/errorHandler/httpErrors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extendError from '@namecheap/error-extender';
import { extendError } from '../util/extendError';

export const HttpError = extendError('HttpError', { defaultData: {} });
export const NotFoundError = extendError('NotFoundError', {
Expand Down
6 changes: 3 additions & 3 deletions registry/server/templates/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import errorExtender from '@namecheap/error-extender';
import { extendError } from '../util/extendError';

export default {
FetchIncludeError: errorExtender('FetchIncludeError', { defaultData: {} }),
InvalidTemplateError: errorExtender('InvalidTemplateError', { defaultData: {} }),
FetchIncludeError: extendError('FetchIncludeError', { defaultData: {} }),
InvalidTemplateError: extendError('InvalidTemplateError', { defaultData: {} }),
};
6 changes: 3 additions & 3 deletions registry/server/util/axiosErrorTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import errorExtender from '@namecheap/error-extender';
import type { AxiosError } from 'axios';
import { type AxiosError } from 'axios';
import { extendError } from './extendError';

const IlcAxiosError = errorExtender('AxiosError');
const IlcAxiosError = extendError('AxiosError');

export function isAxiosError(err: unknown): err is AxiosError {
return Boolean((err as AxiosError)?.isAxiosError);
Expand Down
5 changes: 5 additions & 0 deletions registry/server/util/extendError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import errorExtender, { type ExtendConfig } from '@namecheap/error-extender';

export function extendError<T = any>(name: string, options: ExtendConfig<T> = {}) {
return errorExtender(name, { ...options, inverse: true });
}
2 changes: 1 addition & 1 deletion registry/server/versioning/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import extendError from '@namecheap/error-extender';
import { extendError } from '../util/extendError';

export const VersioningError = extendError('VersioningError', { defaultData: {} });
export const NonRevertableError = extendError<{ reason: string }>('NonRevertableError', { parent: VersioningError });
Expand Down
5 changes: 2 additions & 3 deletions registry/tests/logging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import sinon, { SinonSpy } from 'sinon';
import supertest from 'supertest';

import errorExtender from '@namecheap/error-extender';

import createApplication from '../server/app';
import { getLogger } from '../server/util/logger';
import { getPluginManagerInstance, loadPlugins } from '../server/util/pluginManager';

import type { Handler } from 'express';
import { extendError } from '../server/util/extendError';

async function runScenario(handler: Handler) {
const app = await createApplication(true);
Expand Down Expand Up @@ -96,7 +95,7 @@ describe('Logging', () => {

it('should log extended error json format', async () => {
const logObjectHandler: Handler = (req, res, next) => {
const CustomError = errorExtender('Custom');
const CustomError = extendError('Custom');
try {
getLogger().error(new CustomError({ message: 'desc', data: { a: 1 }, cause: new Error('cause') }));
} catch (e) {
Expand Down

0 comments on commit 5560fb8

Please sign in to comment.