@@ -2,6 +2,7 @@ import type { File, Task, TaskResultPack } from '@vitest/runner'
22import type { UserConsoleLog } from '../types/general'
33import type { TestProject } from './project'
44import type { MergedBlobs } from './reporters/blob'
5+ import type { OnUnhandledErrorCallback } from './types/config'
56import { createFileTask } from '@vitest/runner/utils'
67import { TestCase , TestModule , TestSuite } from './reporters/reported-tasks'
78
@@ -23,31 +24,43 @@ export class StateManager {
2324 reportedTasksMap : WeakMap < Task , TestModule | TestCase | TestSuite > = new WeakMap ( )
2425 blobs ?: MergedBlobs
2526
26- catchError ( err : unknown , type : string ) : void {
27- if ( isAggregateError ( err ) ) {
28- return err . errors . forEach ( error => this . catchError ( error , type ) )
27+ onUnhandledError ?: OnUnhandledErrorCallback
28+
29+ constructor (
30+ options : {
31+ onUnhandledError ?: OnUnhandledErrorCallback
32+ } ,
33+ ) {
34+ this . onUnhandledError = options . onUnhandledError
35+ }
36+
37+ catchError ( error : unknown , type : string ) : void {
38+ if ( isAggregateError ( error ) ) {
39+ return error . errors . forEach ( error => this . catchError ( error , type ) )
2940 }
3041
31- if ( err === Object ( err ) ) {
32- ( err as Record < string , unknown > ) . type = type
42+ if ( typeof error === 'object' && error !== null ) {
43+ ( error as Record < string , unknown > ) . type = type
3344 }
3445 else {
35- err = { type, message : err }
46+ error = { type, message : error }
3647 }
3748
38- const _err = err as Record < string , any >
39- if ( _err && typeof _err === 'object' && _err . code === 'VITEST_PENDING' ) {
40- const task = this . idMap . get ( _err . taskId )
49+ const _error = error as Record < string , any >
50+ if ( _error && typeof _error === 'object' && _error . code === 'VITEST_PENDING' ) {
51+ const task = this . idMap . get ( _error . taskId )
4152 if ( task ) {
4253 task . mode = 'skip'
4354 task . result ??= { state : 'skip' }
4455 task . result . state = 'skip'
45- task . result . note = _err . note
56+ task . result . note = _error . note
4657 }
4758 return
4859 }
4960
50- this . errorsSet . add ( err )
61+ if ( ! this . onUnhandledError || this . onUnhandledError ( error as any ) !== false ) {
62+ this . errorsSet . add ( error )
63+ }
5164 }
5265
5366 clearErrors ( ) : void {
0 commit comments