(error: E, defect: D)
| 10302 | * @since 4.0.0 |
| 10303 | */ |
| 10304 | export function CauseReason<E extends Constraint, D extends Constraint>(error: E, defect: D): CauseReason<E, D> { |
| 10305 | const schema = declareConstructor<Cause_.Reason<E["Type"]>, Cause_.Reason<E["Encoded"]>, CauseReasonIso<E, D>>()( |
| 10306 | [error, defect], |
| 10307 | ([error, defect]) => (input, ast, options) => { |
| 10308 | if (!Cause_.isReason(input)) { |
| 10309 | return Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 10310 | } |
| 10311 | switch (input._tag) { |
| 10312 | case "Fail": |
| 10313 | return Effect.mapBothEager( |
| 10314 | SchemaParser.decodeUnknownEffect(error)(input.error, options), |
| 10315 | { |
| 10316 | onSuccess: Cause_.makeFailReason, |
| 10317 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "error", issue, input, options) |
| 10318 | } |
| 10319 | ) |
| 10320 | case "Die": |
| 10321 | return Effect.mapBothEager( |
| 10322 | SchemaParser.decodeUnknownEffect(defect)(input.defect, options), |
| 10323 | { |
| 10324 | onSuccess: Cause_.makeDieReason, |
| 10325 | onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "defect", issue, input, options) |
| 10326 | } |
| 10327 | ) |
| 10328 | case "Interrupt": |
| 10329 | return Effect.succeed(input) |
| 10330 | } |
| 10331 | }, |
| 10332 | { |
| 10333 | representation: { |
| 10334 | id: "effect/schema/CauseReason", |
| 10335 | payload: null |
| 10336 | }, |
| 10337 | toCode: ({ typeParameters }) => ({ |
| 10338 | runtime: `Schema.CauseReason(${typeParameters[0].runtime}, ${typeParameters[1].runtime})`, |
| 10339 | Type: `Cause.Failure<${typeParameters[0].Type}, ${typeParameters[1].Type}>`, |
| 10340 | importDeclarations: [`import * as Cause from "effect/Cause"`] |
| 10341 | }), |
| 10342 | expected: "Cause.Failure", |
| 10343 | toCodec: ([error, defect]) => |
| 10344 | link<Cause_.Reason<E["Encoded"]>>()( |
| 10345 | Union([ |
| 10346 | Struct({ _tag: Literal("Fail"), error }), |
| 10347 | Struct({ _tag: Literal("Die"), defect }), |
| 10348 | Struct({ _tag: Literal("Interrupt"), fiberId: UndefinedOr(Finite) }) |
| 10349 | ]), |
| 10350 | SchemaTransformation.transform({ |
| 10351 | decode: (e) => { |
| 10352 | switch (e._tag) { |
| 10353 | case "Fail": |
| 10354 | return Cause_.makeFailReason(e.error) |
| 10355 | case "Die": |
| 10356 | return Cause_.makeDieReason(e.defect) |
| 10357 | case "Interrupt": |
| 10358 | return Cause_.makeInterruptReason(e.fiberId) |
| 10359 | } |
| 10360 | }, |
| 10361 | encode: identity |
no test coverage detected