|
1 | | -import { DateTime, Duration, Effect, Either, Option, Redacted, Schema, Stream } from 'effect' |
| 1 | +import { |
| 2 | + Cause, |
| 3 | + DateTime, |
| 4 | + Duration, |
| 5 | + Effect, |
| 6 | + Either, |
| 7 | + Exit, |
| 8 | + Option, |
| 9 | + Redacted, |
| 10 | + Schema, |
| 11 | + Stream, |
| 12 | +} from 'effect' |
2 | 13 | import * as AST from 'effect/SchemaAST' |
3 | 14 |
|
4 | 15 | type OtelPrimitive = string | number | boolean |
@@ -365,6 +376,16 @@ const primitiveFromUnknown = ({ |
365 | 376 | const effectFromEither = <A, E>(either: Either.Either<A, E>): Effect.Effect<A, E> => |
366 | 377 | Either.isRight(either) === true ? Effect.succeed(either.right) : Effect.fail(either.left) |
367 | 378 |
|
| 379 | +const runSyncOrThrow = <A, E>(effect: Effect.Effect<A, E>): A => |
| 380 | + Exit.match(Effect.runSyncExit(effect), { |
| 381 | + onSuccess: (value) => value, |
| 382 | + onFailure: (cause) => { |
| 383 | + const failure = Option.getOrUndefined(Cause.failureOption(cause)) |
| 384 | + if (failure !== undefined) throw failure |
| 385 | + throw Cause.squash(cause) |
| 386 | + }, |
| 387 | + }) |
| 388 | + |
368 | 389 | const encodeUnknown = ({ |
369 | 390 | key, |
370 | 391 | schema, |
@@ -694,13 +715,13 @@ export const OtelAttrs = { |
694 | 715 | (field) => field.attrKey === 'span.label' && field.role === 'span.label', |
695 | 716 | ), |
696 | 717 | encode, |
697 | | - encodeSync: (value) => Effect.runSync(encode(value).pipe(Effect.orDie)), |
698 | | - unsafeEncode: (value) => Effect.runSync(encode(value).pipe(Effect.orDie)), |
| 718 | + encodeSync: (value) => runSyncOrThrow(encode(value)), |
| 719 | + unsafeEncode: (value) => runSyncOrThrow(encode(value)), |
699 | 720 | } |
700 | 721 | }) |
701 | 722 | }, |
702 | 723 | defineSync<S extends Schema.Schema.AnyNoContext>(schema: S): OtelAttrs<S> { |
703 | | - return Effect.runSync(OtelAttrs.define(schema).pipe(Effect.orDie)) |
| 724 | + return runSyncOrThrow(OtelAttrs.define(schema)) |
704 | 725 | }, |
705 | 726 | } |
706 | 727 |
|
@@ -1049,8 +1070,8 @@ function defineOperation<S extends Schema.Schema.AnyNoContext>( |
1049 | 1070 | ...(options.root === undefined ? {} : { root: options.root }), |
1050 | 1071 | metadata, |
1051 | 1072 | encode, |
1052 | | - encodeSync: (value) => Effect.runSync(encode(value).pipe(Effect.orDie)), |
1053 | | - unsafeEncode: (value) => Effect.runSync(encode(value).pipe(Effect.orDie)), |
| 1073 | + encodeSync: (value) => runSyncOrThrow(encode(value)), |
| 1074 | + unsafeEncode: (value) => runSyncOrThrow(encode(value)), |
1054 | 1075 | with: withOperation as OtelOperationDefinition<S>['with'], |
1055 | 1076 | withRoot: withRootOperation as OtelOperationDefinition<S>['withRoot'], |
1056 | 1077 | withStream: withOperationStream as OtelOperationDefinition<S>['withStream'], |
|
0 commit comments