Skip to content

Commit

Permalink
fix: error stack parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ggurkal committed Oct 16, 2022
1 parent 1a1217c commit 1a0c2f9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
59 changes: 45 additions & 14 deletions lib/internals/getCallerInfo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('getCallerInfo', () => {
? `Error
at Object.getCallerInfo (/unix-example-path/node_modules/next-api-decorators/dist/internals/getCallerInfo.js:9:30)
at createHandler (/unix-example-path/node_modules/next-api-decorators/dist/createHandler.js:30:51)
at eval (webpack-internal:///(api)/./${path}.ts:91:144)
at Object.(api)/./${path}.ts (/unix-example-path/.next/server/${path}.js:32:1)
at eval (webpack-internal:///(api)/./${path}.js:91:144)
at Object.(api)/./${path}.js (/unix-example-path/.next/server/${path}.js:32:1)
at __webpack_require__ (/unix-example-path/.next/server/webpack-api-runtime.js:33:42)
at __webpack_exec__ (/unix-example-path/.next/server/${path}.js:42:39)
at /unix-example-path/.next/server/${path}.js:43:28
Expand All @@ -28,8 +28,8 @@ describe('getCallerInfo', () => {
: `Error
at Object.getCallerInfo (/unix-example-path/node_modules/next-api-decorators/dist/internals/getCallerInfo.js:9:30)
at createHandler (/unix-example-path/node_modules/next-api-decorators/dist/createHandler.js:30:51)
at eval (webpack-internal:///./${path}.ts:91:144)
at Object../${path}.ts (/unix-example-path/.next/server/${path}.js:32:1)
at eval (webpack-internal:///./${path}.js:91:144)
at Object../${path}.js (/unix-example-path/.next/server/${path}.js:32:1)
at __webpack_require__ (/unix-example-path/.next/server/webpack-api-runtime.js:33:42)
at __webpack_exec__ (/unix-example-path/.next/server/${path}.js:42:39)
at /unix-example-path/.next/server/${path}.js:43:28
Expand All @@ -56,7 +56,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/unix-example-path/.next/server/pages/api/tags/[id]', '[[...params]].js']);
expect(dir).toStrictEqual(['/pages/api/tags/[id]', '[[...params]].js']);

spyError.mockRestore();
});
Expand All @@ -66,7 +66,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/unix-example-path/.next/server/pages/api/tags/[id]', '[[...params]].js']);
expect(dir).toStrictEqual(['/pages/api/tags/[id]', '[[...params]].js']);

spyError.mockRestore();
});
Expand All @@ -76,7 +76,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'win32' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['C:/win-example-path/.next/server/pages/api/tags/[id]', '[[...params]].js']);
expect(dir).toStrictEqual(['/pages/api/tags/[id]', '[[...params]].js']);

spyError.mockRestore();
});
Expand All @@ -86,7 +86,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'win32' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['C:/win-example-path/.next/server/pages/api/tags/[id]', '[[...params]].js']);
expect(dir).toStrictEqual(['/pages/api/tags/[id]', '[[...params]].js']);

spyError.mockRestore();
});
Expand All @@ -96,7 +96,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/unix-example-path/.next/server/pages/api', '[[...user]].js']);
expect(dir).toStrictEqual(['/pages/api', '[[...user]].js']);

spyError.mockRestore();
});
Expand All @@ -106,7 +106,7 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/unix-example-path/.next/server/pages/api', '[[...user]].js']);
expect(dir).toStrictEqual(['/pages/api', '[[...user]].js']);

spyError.mockRestore();
});
Expand All @@ -130,10 +130,41 @@ describe('getCallerInfo', () => {
Object.defineProperty(process, 'platform', { value: 'win32' });

const dir = getCallerInfo();
expect(dir).toStrictEqual([
'C:/Users/exampleuser/project/route-matching/.next/server/pages/api/users/deep',
'[[...params]].js'
]);
expect(dir).toStrictEqual(['/pages/api/users/deep', '[[...params]].js']);

spyError.mockRestore();
});

test('Issue #482 - Development error', () => {
const spyError = mockError(
'pages/api/deep/nested/route/[[...params]]',
undefined,
'Error\n' +
' at Object.getCallerInfo (/unix-example-path/node_modules/next-api-decorators/dist/internals/getCallerInfo.js:9:22)\n' +
' at createHandler (/unix-example-path/node_modules/next-api-decorators/dist/createHandler.js:30:51)\n' +
' at eval (webpack-internal:///(api)/./pages/api/deep/nested/route/[[...params]].js:35:132)'
);
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/pages/api/deep/nested/route', '[[...params]].js']);

spyError.mockRestore();
});

test('Issue #482 - Production error', () => {
const spyError = mockError(
'pages/api/deep/nested/route/[[...params]]',
undefined,
'Error\n' +
' at Object.getCallerInfo (/unix-example-path/node_modules/next-api-decorators/dist/internals/getCallerInfo.js:9:22)\n' +
' at createHandler (/unix-example-path/node_modules/next-api-decorators/dist/createHandler.js:30:51)\n' +
' at /unix-example-path/.next/server/pages/api/deep/nested/route/[[...params]].js:58:132'
);
Object.defineProperty(process, 'platform', { value: 'darwin' });

const dir = getCallerInfo();
expect(dir).toStrictEqual(['/pages/api/deep/nested/route', '[[...params]].js']);

spyError.mockRestore();
});
Expand Down
27 changes: 9 additions & 18 deletions lib/internals/getCallerInfo.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import { basename, dirname } from 'path';
import { basename, dirname, join } from 'path';

export function getCallerInfo(): [directoryPath: string | undefined, fileName: string | undefined] {
let directoryPath: string | undefined;
let fileName: string | undefined;

let errorStack = new Error().stack;
/* istanbul ignore else */
if (errorStack && process.platform === 'win32') {
errorStack = errorStack.replace(/\\/g, '/');
}

const parenthesisRegExp = /\(([^)]+)\)$/;
const pathInError = errorStack
?.split('\n')
.find(line => parenthesisRegExp.test(line) && line.includes('/.next/server/pages/api'));

const errorLine = errorStack?.split('\n').find(line => line.includes('/pages/api/'));
const fileInfo = errorLine?.split(/:\d+:\d+/);
/* istanbul ignore else */
if (pathInError) {
const [, pathWithRowCol] = parenthesisRegExp.exec(pathInError) ?? [];
/* istanbul ignore else */
if (pathWithRowCol) {
const fullPath = pathWithRowCol.replace(/:(\d+):(\d+)$/, '');
directoryPath = dirname(fullPath);
fileName = basename(fullPath);
}
if (!fileInfo?.length) {
return [undefined, undefined];
}

return [directoryPath, fileName];
const fileName = fileInfo[0].trim().split('/pages/api/');

return [join('/pages/api', dirname(fileName[fileName.length - 1])), basename(fileName[fileName.length - 1])];
}
2 changes: 1 addition & 1 deletion lib/internals/parseRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function parseRequestUrl(req: NextApiRequest, directoryPath?: string, fil
if (directoryPath && !fileName?.startsWith('[...')) {
const pathRegExp = new RegExp(
// "pages/api/articles/index.ts" is compiled into "pages/api/articles.js" which has to be appended to the directory path for parsing
directoryPath.split('/.next/server/pages')[1].replace(/(\[[0-9a-zA-Z-]+\])/, '([0-9a-zA-Z-]+)') +
directoryPath.split('/pages')[1].replace(/(\[[0-9a-zA-Z-]+\])/, '([0-9a-zA-Z-]+)') +
(fileName && !fileName.startsWith('[...') && !fileName.startsWith('[[...')
? `/${basename(fileName, extname(fileName))}`
: '')
Expand Down

0 comments on commit 1a0c2f9

Please sign in to comment.