1
- import type { StudioErrorReport , StudioManagerShape , StudioStatus , StudioServerDefaultShape , StudioServerShape , ProtocolManagerShape , StudioCloudApi , StudioAIInitializeOptions } from '@packages/types'
1
+ import type { StudioManagerShape , StudioStatus , StudioServerDefaultShape , StudioServerShape , ProtocolManagerShape , StudioCloudApi , StudioAIInitializeOptions } from '@packages/types'
2
2
import type { Router } from 'express'
3
3
import type { Socket } from 'socket.io'
4
- import fetch from 'cross-fetch'
5
- import pkg from '@packages/root'
6
- import os from 'os'
7
- import { agent } from '@packages/network'
8
4
import Debug from 'debug'
9
5
import { requireScript } from './require_script'
10
6
import path from 'path'
7
+ import { reportStudioError , ReportStudioErrorOptions } from './api/studio/report_studio_error'
11
8
12
9
interface StudioServer { default : StudioServerDefaultShape }
13
10
@@ -20,21 +17,26 @@ interface SetupOptions {
20
17
}
21
18
22
19
const debug = Debug ( 'cypress:server:studio' )
23
- const routes = require ( './routes' )
24
20
25
21
export class StudioManager implements StudioManagerShape {
26
22
status : StudioStatus = 'NOT_INITIALIZED'
27
23
isProtocolEnabled : boolean = false
28
24
protocolManager : ProtocolManagerShape | undefined
29
25
private _studioServer : StudioServerShape | undefined
30
- private _studioHash : string | undefined
31
26
32
- static createInErrorManager ( error : Error ) : StudioManager {
27
+ static createInErrorManager ( { cloudApi , studioHash , projectSlug , error, studioMethod , studioMethodArgs } : ReportStudioErrorOptions ) : StudioManager {
33
28
const manager = new StudioManager ( )
34
29
35
30
manager . status = 'IN_ERROR'
36
31
37
- manager . reportError ( error ) . catch ( ( ) => { } )
32
+ reportStudioError ( {
33
+ cloudApi,
34
+ studioHash,
35
+ projectSlug,
36
+ error,
37
+ studioMethod,
38
+ studioMethodArgs,
39
+ } )
38
40
39
41
return manager
40
42
}
@@ -43,13 +45,13 @@ export class StudioManager implements StudioManagerShape {
43
45
const { createStudioServer } = requireScript < StudioServer > ( script ) . default
44
46
45
47
this . _studioServer = await createStudioServer ( {
48
+ studioHash,
46
49
studioPath,
47
50
projectSlug,
48
51
cloudApi,
49
52
betterSqlite3Path : path . dirname ( require . resolve ( 'better-sqlite3/package.json' ) ) ,
50
53
} )
51
54
52
- this . _studioHash = studioHash
53
55
this . status = 'INITIALIZED'
54
56
}
55
57
@@ -77,32 +79,11 @@ export class StudioManager implements StudioManagerShape {
77
79
await this . invokeAsync ( 'destroy' , { isEssential : true } )
78
80
}
79
81
80
- private async reportError ( error : Error ) : Promise < void > {
82
+ reportError ( error : unknown , studioMethod : string , ... studioMethodArgs : unknown [ ] ) : void {
81
83
try {
82
- const payload : StudioErrorReport = {
83
- studioHash : this . _studioHash ,
84
- errors : [ {
85
- name : error . name ?? `Unknown name` ,
86
- stack : error . stack ?? `Unknown stack` ,
87
- message : error . message ?? `Unknown message` ,
88
- } ] ,
89
- }
90
-
91
- const body = JSON . stringify ( payload )
92
-
93
- await fetch ( routes . apiRoutes . studioErrors ( ) as string , {
94
- // @ts -expect-error - this is supported
95
- agent,
96
- method : 'POST' ,
97
- body,
98
- headers : {
99
- 'Content-Type' : 'application/json' ,
100
- 'x-cypress-version' : pkg . version ,
101
- 'x-os-name' : os . platform ( ) ,
102
- 'x-arch' : os . arch ( ) ,
103
- } ,
104
- } )
84
+ this . _studioServer ?. reportError ( error , studioMethod , ...studioMethodArgs )
105
85
} catch ( e ) {
86
+ // If we fail to report the error, we shouldn't try and report it again
106
87
debug ( `Error calling StudioManager.reportError: %o, original error %o` , e , error )
107
88
}
108
89
}
@@ -129,8 +110,7 @@ export class StudioManager implements StudioManagerShape {
129
110
}
130
111
131
112
this . status = 'IN_ERROR'
132
- // Call and forget this, we don't want to block the main thread
133
- this . reportError ( actualError ) . catch ( ( ) => { } )
113
+ this . reportError ( actualError , method , ...args )
134
114
}
135
115
}
136
116
@@ -156,10 +136,8 @@ export class StudioManager implements StudioManagerShape {
156
136
}
157
137
158
138
this . status = 'IN_ERROR'
159
- // Call and forget this, we don't want to block the main thread
160
- this . reportError ( actualError ) . catch ( ( ) => { } )
139
+ this . reportError ( actualError , method , ...args )
161
140
162
- // TODO: Figure out errors
163
141
return undefined
164
142
}
165
143
}
0 commit comments