Skip to content

Commit

Permalink
feat: Ignore exceptions based on class name (#882)
Browse files Browse the repository at this point in the history
* Add possibility to ignore exceptions based on class name

* Add ignore exception test

* Update readme and comments

* Fix test error

* Add package.json definition, renamed setting to ignoreExceptions, fix tests.

* Changelog.

---------

Co-authored-by: Damjan Cvetko <damjan.cvetko@gmail.com>
  • Loading branch information
8ctopus and zobo authored Feb 23, 2023
1 parent 75c3e1d commit 120f688
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.32.0]

- New launch setting ignoreExceptions.

## [1.31.1]

- Fix relative paths and path mappings support.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ More general information on debugging with VS Code can be found on https://code.
- `pathMappings`: A list of server paths mapping to the local source paths on your machine, see "Remote Host Debugging" below
- `log`: Whether to log all communication between VS Code and the adapter to the debug console. See _Troubleshooting_ further down.
- `ignore`: An optional array of glob patterns that errors should be ignored from (for example `**/vendor/**/*.php`)
- `ignoreExceptions`: An optional array of exception class names that should be ignored (for example `BaseException`)
- `skipFiles`: An array of glob patterns, to skip when debugging. Star patterns and negations are allowed, for example, `**/vendor/**` or `!**/vendor/my-module/**`.
- `maxConnections`: Accept only this number of parallel debugging sessions. Additional connections will be dropped and their execution will continue without debugging.
- `proxy`: DBGp Proxy settings
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@
"**/vendor/**"
]
},
"ignoreExceptions": {
"type": "array",
"items": "string",
"description": "An array of exception class names that should be ignored."
},
"log": {
"type": "boolean",
"description": "If true, will log all communication between VS Code and the adapter"
Expand Down
15 changes: 11 additions & 4 deletions src/phpDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface LaunchRequestArguments extends VSCodeDebugProtocol.LaunchReques
log?: boolean
/** Array of glob patterns that errors should be ignored from */
ignore?: string[]
/** Array of glob patterns that exceptions should be ignored from */
ignoreExceptions?: string[]
/** Array of glob patterns that debugger should not step in */
skipFiles?: string[]
/** Xdebug configuration */
Expand Down Expand Up @@ -667,15 +669,20 @@ class PhpDebugSession extends vscode.DebugSession {
if (response.exception) {
// If one of the ignore patterns matches, ignore this exception
if (
this._args.ignore &&
this._args.ignore.some(glob =>
minimatch(convertDebuggerPathToClient(response.fileUri).replace(/\\/g, '/'), glob)
)
// ignore files
(this._args.ignore &&
this._args.ignore.some(glob =>
minimatch(convertDebuggerPathToClient(response.fileUri).replace(/\\/g, '/'), glob)
)) ||
// ignore exception class name
(this._args.ignoreExceptions &&
this._args.ignoreExceptions.some(glob => minimatch(response.exception.name, glob)))
) {
const response = await connection.sendRunCommand()
await this._checkStatus(response)
return
}

stoppedEventReason = 'exception'
exceptionText = response.exception.name + ': ' + response.exception.message // this seems to be ignored currently by VS Code
} else if (this._args.stopOnEntry && !this._hasStoppedOnEntry) {
Expand Down
8 changes: 8 additions & 0 deletions src/test/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ describe('PHP Debug Adapter', () => {
await Promise.all([client.configurationDoneRequest(), client.waitForEvent('terminated')])
})

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.setExceptionBreakpointsRequest({ filters: ['*'] })
await Promise.all([client.configurationDoneRequest(), client.waitForEvent('terminated')])
})

it('should support stopping only on a notice', async () => {
await client.launch({ program })
await client.setExceptionBreakpointsRequest({ filters: ['Notice'] })
Expand Down
3 changes: 2 additions & 1 deletion testproject/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
"ignoreExceptions": ["IgnoreException"]
},
{
//"debugServer": 4711, // Uncomment for debugging the adapter
Expand Down
11 changes: 11 additions & 0 deletions testproject/ignore_exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class IgnoreException extends Exception
{}

try {
// see launch.json ignoreExceptions
throw new IgnoreException('this is an ignored exception');
} catch (Exception $e) {
//
}

0 comments on commit 120f688

Please sign in to comment.