From 1057d104521675085a5d0b656bf0141b4a253cdb Mon Sep 17 00:00:00 2001 From: Damjan Cvetko Date: Thu, 16 Mar 2023 12:59:45 +0100 Subject: [PATCH] fix: Ignore Exception patterns --- src/ignore.ts | 9 +++++++ src/phpDebug.ts | 3 ++- src/test/adapter.ts | 2 +- src/test/ignore.ts | 42 ++++++++++++++++++++++++++++++++ testproject/ignore_exception.php | 6 +++-- 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/ignore.ts create mode 100644 src/test/ignore.ts diff --git a/src/ignore.ts b/src/ignore.ts new file mode 100644 index 00000000..f8443442 --- /dev/null +++ b/src/ignore.ts @@ -0,0 +1,9 @@ +export function shouldIgnoreException(name: string, patterns: string[]): boolean { + return patterns.some(pattern => name.match(convertPattern(pattern))) +} + +function convertPattern(pattern: string): string { + const esc = pattern.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d') + const proc = esc.replace(/\\\*\\\*/g, '.*').replace(/\\\*/g, '[^\\\\]*') + return '^' + proc + '$' +} diff --git a/src/phpDebug.ts b/src/phpDebug.ts index 812c2603..f9aab57e 100644 --- a/src/phpDebug.ts +++ b/src/phpDebug.ts @@ -18,6 +18,7 @@ import { ProxyConnect } from './proxyConnect' import { randomUUID } from 'crypto' import { getConfiguredEnvironment } from './envfile' import { XdebugCloudConnection } from './cloud' +import { shouldIgnoreException } from './ignore' if (process.env['VSCODE_NLS_CONFIG']) { try { @@ -676,7 +677,7 @@ class PhpDebugSession extends vscode.DebugSession { )) || // ignore exception class name (this._args.ignoreExceptions && - this._args.ignoreExceptions.some(glob => minimatch(response.exception.name, glob))) + shouldIgnoreException(response.exception.name, this._args.ignoreExceptions)) ) { const response = await connection.sendRunCommand() await this._checkStatus(response) diff --git a/src/test/adapter.ts b/src/test/adapter.ts index a1cfc78e..15b73ad9 100644 --- a/src/test/adapter.ts +++ b/src/test/adapter.ts @@ -215,7 +215,7 @@ describe('PHP Debug Adapter', () => { it('should not break on exception that matches the ignore pattern', async () => { const program = path.join(TEST_PROJECT, 'ignore_exception.php') - await client.launch({ program, ignoreExceptions: ['IgnoreException'] }) + await client.launch({ program, ignoreExceptions: ['NS1\\NS2\\IgnoreException'] }) await client.setExceptionBreakpointsRequest({ filters: ['*'] }) await Promise.all([client.configurationDoneRequest(), client.waitForEvent('terminated')]) }) diff --git a/src/test/ignore.ts b/src/test/ignore.ts new file mode 100644 index 00000000..9b3a3abb --- /dev/null +++ b/src/test/ignore.ts @@ -0,0 +1,42 @@ +import { assert } from 'chai' +import { describe, it } from 'mocha' +import { shouldIgnoreException } from '../ignore' + +describe('ignoreExceptions', () => { + it('should match exact', () => { + assert.isTrue(shouldIgnoreException('BaseException', ['BaseException'])) + }) + it('should no match exact', () => { + assert.isFalse(shouldIgnoreException('BaseException', ['SomeOtherException'])) + }) + it('should match wildcard end exact', () => { + assert.isTrue(shouldIgnoreException('BaseException', ['BaseException*'])) + }) + it('should match wildcard end extra', () => { + assert.isTrue(shouldIgnoreException('BaseExceptionMore', ['BaseException*'])) + }) + it('should match namespaced exact', () => { + assert.isTrue(shouldIgnoreException('NS1\\BaseException', ['NS1\\BaseException'])) + }) + it('should match namespaced wildcard exact', () => { + assert.isTrue(shouldIgnoreException('NS1\\BaseException', ['NS1\\BaseException*'])) + }) + it('should match namespaced wildcard extra', () => { + assert.isTrue(shouldIgnoreException('NS1\\BaseExceptionMore', ['NS1\\BaseException*'])) + }) + it('should match namespaced wildcard whole level', () => { + assert.isTrue(shouldIgnoreException('NS1\\BaseException', ['NS1\\*'])) + }) + it('should not match namespaced wildcard more levels', () => { + assert.isFalse(shouldIgnoreException('NS1\\NS2\\BaseException', ['NS1\\*'])) + }) + it('should match namespaced wildcard in middle', () => { + assert.isTrue(shouldIgnoreException('NS1\\NS2\\BaseException', ['NS1\\*\\BaseException'])) + }) + it('should match namespaced wildcard multiple', () => { + assert.isTrue(shouldIgnoreException('NS1\\NS2\\NS3\\BaseException', ['NS1\\*\\*\\BaseException'])) + }) + it('should match namespaced wildcard levels', () => { + assert.isTrue(shouldIgnoreException('NS1\\NS2\\NS3\\BaseException', ['NS1\\**\\BaseException'])) + }) +}) diff --git a/testproject/ignore_exception.php b/testproject/ignore_exception.php index a50a068d..b1571387 100644 --- a/testproject/ignore_exception.php +++ b/testproject/ignore_exception.php @@ -1,11 +1,13 @@