@@ -27,12 +27,14 @@ import type {
27
27
CompleteTraceDefinition ,
28
28
DraftTraceContext ,
29
29
RelationSchemasBase ,
30
+ ReportErrorFn ,
30
31
TraceContext ,
31
32
TraceDefinitionModifications ,
32
33
TraceInterruptionReason ,
33
34
TraceInterruptionReasonForInvalidTraces ,
34
35
TraceManagerUtilities ,
35
36
TraceModifications ,
37
+ TransitionDraftOptions ,
36
38
} from './types'
37
39
import { INVALID_TRACE_INTERRUPTION_REASONS } from './types'
38
40
import type {
@@ -932,8 +934,12 @@ export class Trace<
932
934
VariantsT
933
935
> {
934
936
if ( ! this . input . relatedTo ) {
935
- throw new Error (
936
- "Tried to access trace's activeInput, but the trace was never provided a 'relatedTo' input value" ,
937
+ this . traceUtilities . reportErrorFn (
938
+ new Error (
939
+ "Tried to access trace's activeInput, but the trace was never provided a 'relatedTo' input value" ,
940
+ ) ,
941
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
942
+ this as Trace < any , RelationSchemasT , any > ,
937
943
)
938
944
}
939
945
return this . input as ActiveTraceConfig <
@@ -1074,6 +1080,8 @@ export class Trace<
1074
1080
input . variant
1075
1081
} . Must be one of: ${ Object . keys ( definition . variants ) . join ( ', ' ) } `,
1076
1082
) ,
1083
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1084
+ this as Trace < any , RelationSchemasT , any > ,
1077
1085
)
1078
1086
}
1079
1087
@@ -1224,21 +1232,76 @@ export class Trace<
1224
1232
RelationSchemasT ,
1225
1233
VariantsT
1226
1234
> ,
1227
- ) {
1235
+ {
1236
+ previouslyActivatedBehavior = 'warn-and-continue' ,
1237
+ invalidRelatedToBehavior = 'warn-and-continue' ,
1238
+ } : TransitionDraftOptions = { } ,
1239
+ ) : void {
1240
+ const { isDraft } = this
1241
+ let reportPreviouslyActivated : ReportErrorFn < RelationSchemasT >
1242
+ let overwriteDraft = true
1243
+ switch ( previouslyActivatedBehavior ) {
1244
+ case 'error' :
1245
+ reportPreviouslyActivated = this . traceUtilities . reportErrorFn
1246
+ overwriteDraft = false
1247
+ break
1248
+ case 'error-and-continue' :
1249
+ reportPreviouslyActivated = this . traceUtilities . reportErrorFn
1250
+ break
1251
+ default :
1252
+ reportPreviouslyActivated = this . traceUtilities . reportWarningFn
1253
+ break
1254
+ }
1255
+
1256
+ // this is an already initialized active trace, do nothing:
1257
+ if ( ! isDraft ) {
1258
+ reportPreviouslyActivated (
1259
+ new Error (
1260
+ `You are trying to activate a trace that has already been activated before (${ this . definition . name } ).` ,
1261
+ ) ,
1262
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1263
+ this as Trace < any , RelationSchemasT , any > ,
1264
+ )
1265
+ if ( ! overwriteDraft ) {
1266
+ return
1267
+ }
1268
+ }
1269
+
1270
+ let reportValidationError : ReportErrorFn < RelationSchemasT >
1271
+ let useInvalidRelatedTo = true
1272
+ switch ( invalidRelatedToBehavior ) {
1273
+ case 'error' :
1274
+ reportValidationError = this . traceUtilities . reportErrorFn
1275
+ useInvalidRelatedTo = false
1276
+ break
1277
+ case 'error-and-continue' :
1278
+ reportValidationError = this . traceUtilities . reportErrorFn
1279
+ break
1280
+ default :
1281
+ reportValidationError = this . traceUtilities . reportWarningFn
1282
+ break
1283
+ }
1284
+
1228
1285
const { attributes } = this . input
1229
1286
1230
1287
const { relatedTo, errors } = validateAndCoerceRelatedToAgainstSchema (
1231
1288
inputAndDefinitionModifications . relatedTo ,
1232
1289
this . definition . relationSchema ,
1233
1290
)
1291
+
1234
1292
if ( errors . length > 0 ) {
1235
- this . traceUtilities . reportWarningFn (
1293
+ reportValidationError (
1236
1294
new Error (
1237
1295
`Invalid relatedTo value: ${ JSON . stringify (
1238
1296
inputAndDefinitionModifications . relatedTo ,
1239
1297
) } . ${ errors . join ( ', ' ) } `,
1240
1298
) ,
1299
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1300
+ this as Trace < any , RelationSchemasT , any > ,
1241
1301
)
1302
+ if ( ! useInvalidRelatedTo ) {
1303
+ return
1304
+ }
1242
1305
}
1243
1306
1244
1307
this . activeInput = {
@@ -1253,7 +1316,11 @@ export class Trace<
1253
1316
this . applyDefinitionModifications ( inputAndDefinitionModifications )
1254
1317
1255
1318
this . wasActivated = true
1256
- this . stateMachine . emit ( 'onMakeActive' , undefined )
1319
+
1320
+ if ( isDraft ) {
1321
+ // we might already be active in which case we would have issued a warning earlier in this method
1322
+ this . stateMachine . emit ( 'onMakeActive' , undefined )
1323
+ }
1257
1324
}
1258
1325
1259
1326
/**
0 commit comments